Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataclasses #426

Merged
merged 9 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ $ pip install --user --upgrade --pre libtmux

<!-- Maintainers and contributors: Insert change notes for the next release above -->

### Breaking changes

- Finding objects / relations

- 0.16 and below: `session._windows()`, `session.list_windows()`, etc.

0.17 and after: {attr}`session.windows <libtmux.Session.windows>`

- 0.16 and below: `session.find_where({'window_name': my_window})`

0.17 and after: {meth}`session.windows.get(window_name=my_window, default=None) <libtmux.Session.windows>`

- If not found and not `default`, raises {exc}`~libtmux._internal.query_list.ObjectDoesNotExist`
- If multiple objects found, raises {exc}`~libtmux._internal.query_list.MultipleObjectsReturned`

- 0.16 and below: `session.where({'window_name': my_window})`

0.17 and after: {meth}`session.windows.filter(window_name=my_window) <libtmux.Session.windows>`

- Accessing attributes

- 0.16 and below: `window['id']`

0.17 and after: `window.id`
- 0.16 and below: `window.get('id')`

0.17 and after: `window.id`
- 0.16 and below: `window.get('id', None)`

0.17 and after: `getattr(window, 'id', None)`

### New features

#### Detect if server active (#448)
Expand Down
33 changes: 33 additions & 0 deletions MIGRATION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ well.
[tracker]: https://github.com/tmux-python/libtmux/discussions
```

## 0.17.x: Simplified attributes

### Finding objects / relations

- 0.16 and below: `session._windows()`, `session.list_windows()`, etc.

0.17 and after: {attr}`session.windows <libtmux.Session.windows>`

- 0.16 and below: `session.find_where({'window_name': my_window})`

0.17 and after: {meth}`session.windows.get(window_name=my_window, default=None) <libtmux.Session.windows>`

- If not found and not `default`, raises {exc}`~libtmux._internal.query_list.ObjectDoesNotExist`
- If multiple objects found, raises {exc}`~libtmux._internal.query_list.MultipleObjectsReturned`



- 0.16 and below: `session.where({'window_name': my_window})`

0.17 and after: {meth}`session.windows.filter(window_name=my_window) <libtmux.Session.windows>`

### Accessing attributes

- 0.16 and below: `window['id']`

0.17 and after: `window.id`
- 0.16 and below: `window.get('id')`

0.17 and after: `window.id`
- 0.16 and below: `window.get('id', None)`

0.17 and after: `getattr(window, 'id', None)`

## Next release

_Migration instructions for the upcoming release will be added here_
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ current tmux server / session / window pane.
List sessions:

```python
>>> server.list_sessions()
>>> server.sessions
[Session($1 ...), Session($0 ...)]
```

Find session:

```python
>>> server.get_by_id('$1')
>>> server.sessions.filter(session_id='$1')[0]
Session($1 ...)
```

Expand All @@ -85,7 +85,7 @@ Find session by dict lookup:
```python
>>> server.sessions[0].rename_session('foo')
Session($1 foo)
>>> server.find_where({ "session_name": "foo" })
>>> server.sessions.filter(session_name="foo")[0]
Session($1 foo)
```

Expand Down Expand Up @@ -150,12 +150,14 @@ Type inside the pane (send key strokes):

>>> pane.send_keys('echo hey', enter=False)
>>> pane.enter()
Pane(%1 ...)
```

Grab the output of pane:

```python
>>> pane.clear() # clear the pane
Pane(%1 ...)
>>> pane.send_keys("cowsay 'hello'", enter=True)
>>> print('\n'.join(pane.cmd('capture-pane', '-p').stdout)) # doctest: +SKIP
$ cowsay 'hello'
Expand Down
4 changes: 2 additions & 2 deletions docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ How is libtmux able to keep references to panes, windows and sessions?
> Window index refers to the # of the window in the session.
>
> To assert pane, window and session data, libtmux will use
> {meth}`Server.list_sessions()`, {meth}`Session.list_windows()`,
> {meth}`Window.list_panes()` to update objects.
> {meth}`Server.sessions()`, {meth}`Session.windows()`,
> {meth}`Window.panes()` to update objects.

## Naming conventions

Expand Down
8 changes: 8 additions & 0 deletions docs/internals/dataclasses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dataclass helpers - `libtmux._internal.dataclasses`

```{eval-rst}
.. automodule:: libtmux._internal.dataclasses
:members:
:special-members:

```
11 changes: 9 additions & 2 deletions docs/internals/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Internals

:::{warning}
Be careful with these! Internal APIs are **not** covered by version policies. They can break or be removed between minor versions!

These APIs are internal and not covered by versioning policy.

If you need an internal API stabilized please [file an issue](https://github.com/tmux-python/libtmux/issues).
:::

## Environmental variables
Expand All @@ -23,3 +23,10 @@ to split `tmux(1)`'s formatting information.

If you find any compatibility problems with the default, or better yet find a string copacetic
many environments and tmux releases, note it at <https://github.com/tmux-python/libtmux/discussions/355>.

## Changes

```{toctree}
dataclasses
query_list
``
6 changes: 6 additions & 0 deletions docs/internals/query_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# List querying - `libtmux._internal.query_list`

```{eval-rst}
.. automodule:: libtmux._internal.query_list
:members:
```
28 changes: 16 additions & 12 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,33 @@ Windows and Panes.
If you have multiple tmux sessions open, you can see that all of the
methods in {class}`Server` are available.

We can list sessions with {meth}`Server.list_sessions`:
We can list sessions with {meth}`Server.sessions`:

```python
>>> server.list_sessions()
>>> server.sessions
[Session($1 ...), Session($0 ...)]
```

This returns a list of {class}`Session` objects you can grab. We can
find our current session with:

```python
>>> server.list_sessions()[0]
>>> server.sessions[0]
Session($1 ...)
```

However, this isn't guaranteed, libtmux works against current tmux information, the
session's name could be changed, or another tmux session may be created,
so {meth}`Server.get_by_id` and {meth}`Server.find_where` exists as a lookup.
so {meth}`Server.sessions` and {meth}`Server.windows` exists as a lookup.

## Get session by ID

tmux sessions use the `$[0-9]` convention as a way to identify sessions.

`$1` is whatever the ID `list_sessions()` returned above.
`$1` is whatever the ID `sessions()` returned above.

```python
>>> server.get_by_id('$1')
>>> server.sessions.filter(session_id='$1')[0]
Session($1 ...)
```

Expand All @@ -163,13 +163,16 @@ You may `session = server.get_by_id('$<yourId>')` to use the session object.
>>> server.sessions[0].rename_session('foo')
Session($1 foo)

>>> server.find_where({ "session_name": "foo" })
>>> server.sessions.filter(session_name="foo")[0]
Session($1 foo)

>>> server.sessions.get(session_name="foo")
Session($1 foo)
```

With `find_where`, pass in a dict and return the first object found. In
With `filter`, pass in attributes and return a list of matches. In
this case, a {class}`Server` holds a collection of child {class}`Session`.
{class}`Session` and {class}`Window` both utilize `find_where` to sift
{class}`Session` and {class}`Window` both utilize `filter` to sift
through Windows and Panes, respectively.

So you may now use:
Expand All @@ -178,7 +181,7 @@ So you may now use:
>>> server.sessions[0].rename_session('foo')
Session($1 foo)

>>> session = server.find_where({ "session_name": "foo" })
>>> session = server.sessions.get(session_name="foo")
>>> session
Session($1 foo)
```
Expand Down Expand Up @@ -213,7 +216,7 @@ Let's delete that window ({meth}`Session.kill_window`).
Method 1: Use passthrough to tmux's `target` system.

```python
>>> session.kill_window(window.id)
>>> session.kill_window(window.window_id)
```

The window in the bg dissappeared. This was the equivalent of
Expand Down Expand Up @@ -260,7 +263,7 @@ And kill:
>>> window.kill_window()
```

Use {meth}`Session.list_windows()` and {meth}`Session.find_where()` to list and sort
Use {meth}`Session.windows` and {meth}`Session.windows.filter()` to list and sort
through active {class}`Window`'s.

## Manipulating windows
Expand Down Expand Up @@ -346,6 +349,7 @@ using {meth}`Pane.enter()`:

```python
>>> pane.enter()
Pane(%1 ...)
```

### Avoid cluttering shell history
Expand Down
55 changes: 17 additions & 38 deletions docs/reference/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,24 @@ Session($1 libtmux_...)
Quick access to basic attributes:

```python
>>> session.name
>>> session.session_name
'libtmux_...'

>>> session.id
>>> session.session_id
'$1'
```

To see all attributes for a session:

```python
>>> sorted(list(session._info.keys()))
from libtmux.neo import Obj

>>> sorted(list(Obj.__dataclass_fields__.keys()))
['session_attached', 'session_created', ...]
```

Some may conflict with python API, to access them, you can use `.get()`, to get the count
of sessions in a window:

```python
>>> session.get('session_windows')
>>> session.session_windows
'...'
```

Expand All @@ -85,30 +84,23 @@ Window(@1 ...:..., Session($1 ...))
Basics:

```python
>>> window.name
>>> window.window_name
'...'

>>> window.id
>>> window.window_id
'@1'

>>> window.height
>>> window.window_height
'...'

>>> window.width
>>> window.window_width
'...'
```

Everything available:

```python
>>> sorted(list(window.keys()))
['session_id', 'session_name', 'window_active', ..., 'window_width']
```

Use `get()` for details not accessible via properties:
Use attribute access for details not accessible via properties:

```python
>>> window.get('window_panes')
>>> window.window_panes
'1'
```

Expand All @@ -126,33 +118,20 @@ Pane(%1 Window(@1 ...:..., Session($1 libtmux_...)))
Basics:

```python
>>> pane.current_command
>>> pane.pane_current_command
'...'

>>> type(pane.current_command)
>>> type(pane.pane_current_command)
<class 'str'>

>>> pane.height
>>> pane.pane_height
'...'

>>> pane.width
>>> pane.pane_width
'...'

>>> pane.index
>>> pane.pane_index
'0'
```

Everything:

````python
>>> sorted(list(pane._info.keys()))
['alternate_on', 'alternate_saved_x', ..., 'wrap_flag']

Use `get()` for details keys:

```python
>>> pane.get('pane_width')
'...'
````

[formats]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#FORMATS
Empty file.
Loading