From 1869cc8bedbd6085959e8566cad6729f3e7e2620 Mon Sep 17 00:00:00 2001 From: sol <1731279+s-ol@users.noreply.github.com> Date: Fri, 8 Jan 2021 11:25:53 +0100 Subject: [PATCH] Use updated i3ipc syntax in i3 segments/listers (#2062) Close #2046 Co-authored-by: s-ol --- docs/source/develop/segments.rst | 8 +-- docs/source/usage/wm-widgets.rst | 3 +- powerline/bindings/wm/__init__.py | 17 +---- powerline/listers/i3wm.py | 13 ++-- powerline/segments/i3wm.py | 20 +++--- tests/test_python/test_listers.py | 112 ++++++----------------------- tests/test_python/test_segments.py | 10 +-- 7 files changed, 48 insertions(+), 135 deletions(-) diff --git a/docs/source/develop/segments.rst b/docs/source/develop/segments.rst index 543ddd57..59549f62 100644 --- a/docs/source/develop/segments.rst +++ b/docs/source/develop/segments.rst @@ -528,10 +528,10 @@ i3wm in lemonbar bindings. ``workspace`` - dictionary containing the workspace name under the key ``"name"`` and - boolean values for the ``"visible"``, ``"urgent"`` and ``"focused"`` - keys, indicating the state of the workspace. Currently only provided by - the :py:func:`powerline.listers.i3wm.workspace_lister` lister. + the `i3-ipc` workspace object corresponding to this workspace. + Contains string attributes ``name`` and ``output``, as well as boolean + attributes for ``visible``, ``urgent`` and ``focused``. Currently only + provided by the :py:func:`powerline.listers.i3wm.workspace_lister` lister. Segment class ============= diff --git a/docs/source/usage/wm-widgets.rst b/docs/source/usage/wm-widgets.rst index 204c008c..bc63da63 100644 --- a/docs/source/usage/wm-widgets.rst +++ b/docs/source/usage/wm-widgets.rst @@ -77,8 +77,7 @@ to run with i3, simply ``exec`` this in the i3 config file and set the ``--i3`` exec python /path/to/powerline/bindings/lemonbar/powerline-lemonbar.py --i3 -Running the binding in i3-mode will require `i3ipc `_ -(or the outdated `i3-py `_). +Running the binding in i3-mode will require `i3ipc `_. See the `lemonbar documentation `_ for more information and options. diff --git a/powerline/bindings/wm/__init__.py b/powerline/bindings/wm/__init__.py index 646b701a..d2c6f30a 100644 --- a/powerline/bindings/wm/__init__.py +++ b/powerline/bindings/wm/__init__.py @@ -24,15 +24,6 @@ def i3_subscribe(conn, event, callback): :param func callback: Function to run on event. ''' - try: - import i3ipc - except ImportError: - import i3 - conn.Subscription(callback, event) - return - else: - pass - conn.on(event, callback) from threading import Thread @@ -57,12 +48,8 @@ def get_i3_connection(): ''' global conn if not conn: - try: - import i3ipc - except ImportError: - import i3 as conn - else: - conn = i3ipc.Connection() + import i3ipc + conn = i3ipc.Connection() return conn diff --git a/powerline/listers/i3wm.py b/powerline/listers/i3wm.py index 0bbcfdc3..b1984e94 100644 --- a/powerline/listers/i3wm.py +++ b/powerline/listers/i3wm.py @@ -50,19 +50,14 @@ def workspace_lister(pl, segment_info, only_show=None, output=None): ( updated( segment_info, - output=w['output'], - workspace={ - 'name': w['name'], - 'visible': w['visible'], - 'urgent': w['urgent'], - 'focused': w['focused'], - }, + output=w.output, + workspace=w, ), { 'draw_inner_divider': None } ) for w in get_i3_connection().get_workspaces() - if (((not only_show or any(w[typ] for typ in only_show)) - and (not output or w['output'] == output))) + if (((not only_show or any(getattr(w, typ) for typ in only_show)) + and (not output or w.output == output))) ) diff --git a/powerline/segments/i3wm.py b/powerline/segments/i3wm.py index 3f508ee2..a6c0ee85 100644 --- a/powerline/segments/i3wm.py +++ b/powerline/segments/i3wm.py @@ -12,11 +12,11 @@ WORKSPACE_REGEX = re.compile(r'^[0-9]+: ?') def workspace_groups(w): group = [] - if w['focused']: + if w.focused: group.append('w_focused') - if w['urgent']: + if w.urgent: group.append('w_urgent') - if w['visible']: + if w.visible: group.append('w_visible') group.append('workspace') return group @@ -52,12 +52,12 @@ def workspaces(pl, segment_info, only_show=None, output=None, strip=0): return [ { - 'contents': w['name'][strip:], + 'contents': w.name[strip:], 'highlight_groups': workspace_groups(w) } for w in get_i3_connection().get_workspaces() - if ((not only_show or any(w[typ] for typ in only_show)) - and (not output or w['output'] == output)) + if ((not only_show or any(getattr(w, typ) for typ in only_show)) + and (not output or w.output == output)) ] @@ -80,7 +80,7 @@ def workspace(pl, segment_info, workspace=None, strip=False): try: w = next(( w for w in get_i3_connection().get_workspaces() - if w['name'] == workspace + if w.name == workspace )) except StopIteration: return None @@ -90,13 +90,13 @@ def workspace(pl, segment_info, workspace=None, strip=False): try: w = next(( w for w in get_i3_connection().get_workspaces() - if w['focused'] + if w.focused )) except StopIteration: return None return [{ - 'contents': format_name(w['name'], strip=strip), + 'contents': format_name(w.name, strip=strip), 'highlight_groups': workspace_groups(w) }] @@ -150,6 +150,6 @@ def scratchpad(pl, icons=SCRATCHPAD_ICONS): 'contents': icons.get(w.scratchpad_state, icons['changed']), 'highlight_groups': scratchpad_groups(w) } - for w in get_i3_connection().get_tree().descendents() + for w in get_i3_connection().get_tree().descendants() if w.scratchpad_state != 'none' ] diff --git a/tests/test_python/test_listers.py b/tests/test_python/test_listers.py index a33f0333..f2368dfa 100644 --- a/tests/test_python/test_listers.py +++ b/tests/test_python/test_listers.py @@ -8,14 +8,16 @@ from tests.modules import TestCase class TestI3WM(TestCase): + workspaces = [ + Args(name='1: w1', output='LVDS1', focused=False, urgent=False, visible=False), + Args(name='2: w2', output='LVDS1', focused=False, urgent=False, visible=True), + Args(name='3: w3', output='HDMI1', focused=False, urgent=True, visible=True), + Args(name='4: w4', output='DVI01', focused=True, urgent=True, visible=True), + ] + @staticmethod def get_workspaces(): - return iter([ - {'name': '1: w1', 'output': 'LVDS1', 'focused': False, 'urgent': False, 'visible': False}, - {'name': '2: w2', 'output': 'LVDS1', 'focused': False, 'urgent': False, 'visible': True}, - {'name': '3: w3', 'output': 'HDMI1', 'focused': False, 'urgent': True, 'visible': True}, - {'name': '4: w4', 'output': 'DVI01', 'focused': True, 'urgent': True, 'visible': True}, - ]) + return iter(TestI3WM.workspaces) @staticmethod def get_outputs(pl): @@ -46,42 +48,22 @@ class TestI3WM(TestCase): ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '1: w1', - 'focused': False, - 'urgent': False, - 'visible': False - } + 'workspace': self.workspaces[0], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '2: w2', - 'focused': False, - 'urgent': False, - 'visible': True - } + 'workspace': self.workspaces[1], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'HDMI1', - 'workspace': { - 'name': '3: w3', - 'focused': False, - 'urgent': True, - 'visible': True - } + 'workspace': self.workspaces[2], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'DVI01', - 'workspace': { - 'name': '4: w4', - 'focused': True, - 'urgent': True, - 'visible': True - } + 'workspace': self.workspaces[3], }, {'draw_inner_divider': None}), ] ) @@ -92,22 +74,12 @@ class TestI3WM(TestCase): ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '1: w1', - 'focused': False, - 'urgent': False, - 'visible': False - } + 'workspace': self.workspaces[0], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '2: w2', - 'focused': False, - 'urgent': False, - 'visible': True - } + 'workspace': self.workspaces[1], }, {'draw_inner_divider': None}), ] ) @@ -121,22 +93,12 @@ class TestI3WM(TestCase): ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '1: w1', - 'focused': False, - 'urgent': False, - 'visible': False - } + 'workspace': self.workspaces[0], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '2: w2', - 'focused': False, - 'urgent': False, - 'visible': True - } + 'workspace': self.workspaces[1], }, {'draw_inner_divider': None}), ] ) @@ -151,42 +113,22 @@ class TestI3WM(TestCase): ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '1: w1', - 'focused': False, - 'urgent': False, - 'visible': False - } + 'workspace': self.workspaces[0], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'LVDS1', - 'workspace': { - 'name': '2: w2', - 'focused': False, - 'urgent': False, - 'visible': True - } + 'workspace': self.workspaces[1], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'HDMI1', - 'workspace': { - 'name': '3: w3', - 'focused': False, - 'urgent': True, - 'visible': True - } + 'workspace': self.workspaces[2], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'DVI01', - 'workspace': { - 'name': '4: w4', - 'focused': True, - 'urgent': True, - 'visible': True - } + 'workspace': self.workspaces[3], }, {'draw_inner_divider': None}), ] ) @@ -201,22 +143,12 @@ class TestI3WM(TestCase): ({ 'a': 1, 'output': 'HDMI1', - 'workspace': { - 'name': '3: w3', - 'focused': False, - 'urgent': True, - 'visible': True - } + 'workspace': self.workspaces[2], }, {'draw_inner_divider': None}), ({ 'a': 1, 'output': 'DVI01', - 'workspace': { - 'name': '4: w4', - 'focused': True, - 'urgent': True, - 'visible': True - } + 'workspace': self.workspaces[3], }, {'draw_inner_divider': None}), ] ) diff --git a/tests/test_python/test_segments.py b/tests/test_python/test_segments.py index 6c441143..1ee8cd0a 100644 --- a/tests/test_python/test_segments.py +++ b/tests/test_python/test_segments.py @@ -1004,10 +1004,10 @@ class TestI3WM(TestCase): @staticmethod def get_workspaces(): return iter([ - {'name': '1: w1', 'output': 'LVDS1', 'focused': False, 'urgent': False, 'visible': False}, - {'name': '2: w2', 'output': 'LVDS1', 'focused': False, 'urgent': False, 'visible': True}, - {'name': '3: w3', 'output': 'HDMI1', 'focused': False, 'urgent': True, 'visible': True}, - {'name': '4: w4', 'output': 'DVI01', 'focused': True, 'urgent': True, 'visible': True}, + Args(name='1: w1', output='LVDS1', focused = False, urgent = False, visible = False), + Args(name='2: w2', output='LVDS1', focused = False, urgent = False, visible = True), + Args(name='3: w3', output='HDMI1', focused = False, urgent = True, visible = True), + Args(name='4: w4', output='DVI01', focused = True, urgent = True, visible = True), ]) def test_workspaces(self): @@ -1093,7 +1093,7 @@ class TestI3WM(TestCase): def get_tree(self): return self - def descendents(self): + def descendants(self): nodes_unfocused = [Args(focused = False)] nodes_focused = [Args(focused = True)]