Skip to content

Commit 9e44406

Browse files
committed
Merge pull request #3 from tony/api-overhaul
API cleanup for 0.4
2 parents 26bf2c5 + 46a971d commit 9e44406

File tree

14 files changed

+191
-229
lines changed

14 files changed

+191
-229
lines changed

CHANGES

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@ Changelog
44

55
Here you can find the recent changes to libtmux
66

7-
- libtmux forked from `tmuxp`_.
7+
- :release:`0.4.0 <2016-05-23>`
8+
- :support:`-` attributes for formatters are now accessible via
9+
``Session``, ``Window`` and ``Pane`` objects. ``session.name``
10+
is equivalent to ``session.get('session_name')``, you can do the
11+
same with other properties in ``_info``. ``window.name``,
12+
``pane.current_path``, ``session.id``, ``window.id``, ``pane.id``,
13+
``session.index``, ``window.index``, ``pane.index``, etc.
14+
- :support:`-` ``attached_sessions``, ``attached_window`` and
15+
``attached_pane`` are now properties.
16+
- :support:`-` ``_TMUX`` metadata object changed to ``_info``.
17+
- :support:`-` ``.findWhere()`` is now ``find_where``.
18+
- :support:`-` README and usage fixes
19+
20+
- :release:`0.3.0 <2016-05-23>`
21+
- switch to pytest
822

923
- :release:`0.1.0 <2016-05-22>`
24+
- libtmux forked from `tmuxp`_.
1025

1126
.. _tmuxp: https://github.com/tony/tmuxp
1227

README.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
libtmux - library for managing tmux workspaces
1+
libtmux - scripting library for tmux
22

33
|pypi| |docs| |build-status| |coverage| |license|
44

5-
libtmux is the library that powers `tmuxp`_, a tool that helps tmux users
6-
manage their workspaces.
5+
libtmux is the tool behind `tmuxp`_, a tmux workspace manager in python.
76

8-
Take control of tmux via python.
7+
it builds upon tmux' `target`_ and `formats`_ to create an object
8+
mappings to traverse, inspect and interact with live tmux sesssions.
99

10-
View the `documentation`_ homepage, `API`_ information and `architectural
10+
view the `documentation`_ homepage, `API`_ information and `architectural
1111
details`_.
1212

13-
.. _tmuxp: https://github.com/tony/tmuxp
14-
.. _documentation: https://libtmux.readthedocs.io/
15-
.. _API: https://libtmux.readthedocs.io/api.html
16-
.. _architectural details: https://libtmux.readthedocs.io/internals.html
17-
1813
install
1914
-------
2015

@@ -56,17 +51,17 @@ list sessions::
5651

5752
find session::
5853

59-
>>> server.getById('$3')
54+
>>> server.get_by_id('$3')
6055
Session($3 foo)
6156

6257
find session by dict lookup::
6358

64-
>>> server.findWhere({ "session_name": "foo" })
59+
>>> server.find_where({ "session_name": "foo" })
6560
Session($3 foo)
6661

6762
assign session to ``session``::
6863

69-
>>> session = server.findWhere({ "session_name": "foo" })
64+
>>> session = server.find_where({ "session_name": "foo" })
7065

7166
play with session::
7267

@@ -131,6 +126,15 @@ powerful traversal features::
131126
>>> pane.window.session
132127
Session($3 foo)
133128

129+
.. _BSD: http://opensource.org/licenses/BSD-3-Clause
130+
.. _developing and testing: http://libtmux.readthedocs.io/developing.html
131+
.. _tmuxp: https://github.com/tony/tmuxp
132+
.. _documentation: https://libtmux.readthedocs.io/
133+
.. _API: https://libtmux.readthedocs.io/api.html
134+
.. _architectural details: https://libtmux.readthedocs.io/internals.html
135+
.. _formats: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#FORMAT
136+
.. _target: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS
137+
134138
Project details
135139
---------------
136140

@@ -168,10 +172,6 @@ tests .. code-block:: bash
168172
$ make test
169173
============== ==========================================================
170174

171-
.. _BSD: http://opensource.org/licenses/BSD-3-Clause
172-
.. _developing and testing: http://libtmux.readthedocs.io/developing.html
173-
.. _Issues tracker: https://github.com/tony/libtmux/issues
174-
175175
.. |pypi| image:: https://img.shields.io/pypi/v/libtmux.svg
176176
:alt: Python Package
177177
:target: http://badge.fury.io/py/libtmux

doc/quickstart.rst

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ find our current session with:
9191
9292
However, this isn't guaranteed, libtmux works against current tmux information, the
9393
session's name could be changed, or another tmux session may be created,
94-
so :meth:`Server.getById` and :meth:`Server.findWhere` exists as a lookup.
94+
so :meth:`Server.get_by_id` and :meth:`Server.find_where` exists as a lookup.
9595

9696
Get session by ID
9797
-----------------
@@ -103,33 +103,29 @@ tmux sessions use the ``$[0-9]`` convention as a way to identify sessions.
103103
.. code-block:: python
104104
105105
106-
>>> server.getById('$3')
106+
>>> server.get_by_id('$3')
107107
Session($3 foo)
108108
109-
You may ``session = getById('$<yourId>')`` to use the session object.
109+
You may ``session = server.get_by_id('$<yourId>')`` to use the session object.
110110

111111
Get session by name / other properties
112112
--------------------------------------
113113

114-
I really like `Backbone`_'s approach to handling collections of structured
115-
data. So I made a :meth:`Server.findWhere` method modelled after
116-
`Backbone.Collection.prototype.findWhere`_.
117-
118114
.. code-block:: python
119115
120-
>>> server.findWhere({ "session_name": "foo" })
116+
>>> server.find_where({ "session_name": "foo" })
121117
Session($3 foo)
122118
123-
With ``findWhere``, pass in a dict and return the first object found. In
119+
With ``find_where``, pass in a dict and return the first object found. In
124120
this case, a :class:`Server` holds a collection of child :class:`Session`.
125-
:class:`Session` and :class:`Window` both utilize ``findWhere`` to sift
121+
:class:`Session` and :class:`Window` both utilize ``find_where`` to sift
126122
through Windows and Panes, respectively.
127123

128124
So you may now use:
129125

130126
.. code-block:: python
131127
132-
>>> session = server.findWhere({ "session_name": "foo" })
128+
>>> session = server.find_where({ "session_name": "foo" })
133129
134130
to give us a ``session`` object to play with.
135131

@@ -203,7 +199,7 @@ And kill:
203199
204200
>>> window.kill_window()
205201
206-
Use :meth:`Session.list_windows()` and :meth:`Session.findWhere()` to list and sort
202+
Use :meth:`Session.list_windows()` and :meth:`Session.find_where()` to list and sort
207203
through active :class:`Window`'s.
208204

209205
Manipulating windows
@@ -302,8 +298,6 @@ sessions in the background. :)
302298
and our `test suite`_ (see :ref:`developing`.)
303299

304300
.. _sliderepl: http://discorporate.us/projects/sliderepl/
305-
.. _backbone: http:/ /backbonejs.org
306-
.. _Backbone.Collection.prototype.findWhere: http://backbonejs.org/#Collection-findWhere
307301
.. _workspacebuilder.py: https://github.com/tony/libtmux/blob/master/libtmux/workspacebuilder.py
308302
.. _test suite: https://github.com/tony/libtmux/tree/master/tests
309303
.. _ptpython: https://github.com/jonathanslenders/ptpython

libtmux/common.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,31 +198,38 @@ class TmuxMappingObject(collections.MutableMapping):
198198
:class:`Session` and :class:`Server`.
199199
200200
Instance attributes for useful information :term:`tmux(1)` uses for
201-
Session, Window, Pane, stored :attr:`self._TMUX`. For example, a
201+
Session, Window, Pane, stored :attr:`self._info`. For example, a
202202
:class:`Window` will have a ``window_id`` and ``window_name``.
203203
204204
"""
205205

206206
def __getitem__(self, key):
207-
return self._TMUX[key]
207+
return self._info[key]
208208

209209
def __setitem__(self, key, value):
210-
self._TMUX[key] = value
210+
self._info[key] = value
211211
self.dirty = True
212212

213213
def __delitem__(self, key):
214-
del self._TMUX[key]
214+
del self._info[key]
215215
self.dirty = True
216216

217217
def keys(self):
218218
"""Return list of keys."""
219-
return self._TMUX.keys()
219+
return self._info.keys()
220220

221221
def __iter__(self):
222-
return self._TMUX.__iter__()
222+
return self._info.__iter__()
223223

224224
def __len__(self):
225-
return len(self._TMUX.keys())
225+
return len(self._info.keys())
226+
227+
def __getattr__(self, key):
228+
try:
229+
return self._info[self.formatter_prefix + key]
230+
except:
231+
raise AttributeError('%s has no property %s' %
232+
(self.__class__, key))
226233

227234

228235
class TmuxRelationalObject(object):
@@ -234,10 +241,10 @@ class TmuxRelationalObject(object):
234241
:class:`Window`)
235242
236243
Children of :class:`TmuxRelationalObject` are going to have a
237-
``self.children``, ``self.childIdAttribute`` and ``self.list_children``.
244+
``self.children``, ``self.child_id_attribute`` and ``self.list_children``.
238245
239246
================ ================== ===================== ============================
240-
Object ``.children`` ``.childIdAttribute`` method
247+
Object ``.children`` ``.child_id_attribute`` method
241248
================ ================== ===================== ============================
242249
:class:`Server` ``self._sessions`` 'session_id' :meth:`Server.list_sessions`
243250
:class:`Session` ``self._windows`` 'window_id' :meth:`Session.list_windows`
@@ -247,13 +254,11 @@ class TmuxRelationalObject(object):
247254
248255
"""
249256

250-
def findWhere(self, attrs):
257+
def find_where(self, attrs):
251258
"""Return object on first match.
252259
253-
Based on `.findWhere()`_ from `underscore.js`_.
254-
255-
.. _.findWhere(): http://underscorejs.org/#findWhere
256-
.. _underscore.js: http://underscorejs.org/
260+
:versionchanged: 0.4
261+
Renamed from ``.findWhere`` to ``.find_where``.
257262
258263
"""
259264
try:
@@ -264,11 +269,6 @@ def findWhere(self, attrs):
264269
def where(self, attrs, first=False):
265270
"""Return objects matching child objects properties.
266271
267-
Based on `.where()`_ from `underscore.js`_.
268-
269-
.. _.where(): http://underscorejs.org/#where
270-
.. _underscore.js: http://underscorejs.org/
271-
272272
:param attrs: tmux properties to match
273273
:type attrs: dict
274274
:rtype: list
@@ -290,8 +290,8 @@ def by(val, *args):
290290
else:
291291
return list(filter(by, self.children))
292292

293-
def getById(self, id):
294-
"""Return object based on ``childIdAttribute``.
293+
def get_by_id(self, id):
294+
"""Return object based on ``child_id_attribute``.
295295
296296
Based on `.get()`_ from `backbone.js`_.
297297
@@ -304,7 +304,7 @@ def getById(self, id):
304304
305305
"""
306306
for child in self.children:
307-
if child[self.childIdAttribute] == id:
307+
if child[self.child_id_attribute] == id:
308308
return child
309309
else:
310310
continue

libtmux/pane.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class Pane(TmuxMappingObject, TmuxRelationalObject):
2626
Renamed from ``.tmux`` to ``.cmd``.
2727
2828
"""
29+
formatter_prefix = 'pane_'
2930

3031
def __init__(self, window=None, **kwargs):
3132
if not window:
32-
raise ValueError('Pane must have \
33-
``Window`` object')
33+
raise ValueError('Pane must have ``Window`` object')
3434

3535
self.window = window
3636
self.session = self.window.session
@@ -41,7 +41,7 @@ def __init__(self, window=None, **kwargs):
4141
self.server._update_panes()
4242

4343
@property
44-
def _TMUX(self, *args):
44+
def _info(self, *args):
4545

4646
attrs = {
4747
'pane_id': self._pane_id

libtmux/server.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class Server(TmuxRelationalObject, EnvironmentMixin):
4444
#: ``-2`` or ``-8``
4545
colors = None
4646
#: unique child ID key
47-
childIdAttribute = 'session_id'
47+
child_id_attribute = 'session_id'
48+
#: namespace used by tmux formatter variables
49+
formatter_prefix = 'server_'
4850

4951
def __init__(
5052
self,
@@ -262,7 +264,13 @@ def _list_panes(self):
262264

263265
# clear up empty dict
264266
panes = [
265-
dict((k, v) for k, v in window.items() if v) for window in panes
267+
dict(
268+
(k, v) for k, v in window.items()
269+
if v or
270+
k == 'pane_current_path'
271+
) # preserve pane_current_path, in case it entered a new process
272+
# where we may not get a cwd from.
273+
for window in panes
266274
]
267275

268276
if self._panes:
@@ -281,6 +289,7 @@ def _update_panes(self):
281289
self._list_panes()
282290
return self
283291

292+
@property
284293
def attached_sessions(self):
285294
"""Return active :class:`Session` objects.
286295
@@ -296,9 +305,8 @@ def attached_sessions(self):
296305
for session in sessions:
297306
if 'session_attached' in session:
298307
# for now session_active is a unicode
299-
if session.get('session_attached') == '1':
300-
logger.debug('session %s attached', session.get(
301-
'session_name'))
308+
if session.attached == '1':
309+
logger.debug('session %s attached', session.name)
302310
attached_sessions.append(session)
303311
else:
304312
continue

0 commit comments

Comments
 (0)