From a37f90921fc68311eb16405411373a8a050accf1 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 14:56:11 +0400 Subject: [PATCH 1/7] Refactor powerline.segments.common.players to use multiple functions Also adds documentation. Still have to update top-level themes. Fixes #445 --- powerline/lint/imp.py | 6 + powerline/segments/common/players.py | 303 +++++++++++++++++++++------ 2 files changed, 249 insertions(+), 60 deletions(-) diff --git a/powerline/lint/imp.py b/powerline/lint/imp.py index 98b7591b..d3966512 100644 --- a/powerline/lint/imp.py +++ b/powerline/lint/imp.py @@ -27,6 +27,12 @@ def import_function(function_type, name, data, context, echoerr, module): problem='module {0} is deprecated'.format(module), problem_mark=module.mark) + if module == 'powerline.segments.common.players' and name == 'now_playing': + echoerr(context='Warning while checking segments (key {key})'.format(key=context.key), + context_mark=name.mark, + problem='function {0}.{1} is deprecated: use {0}.{{player_name}} instead'.format(module, name), + problem_mark=module.mark) + with WithPath(data['import_paths']): try: func = getattr(__import__(str(module), fromlist=[str(name)]), str(name)) diff --git a/powerline/segments/common/players.py b/powerline/segments/common/players.py index 589e419e..47dc8f0b 100644 --- a/powerline/segments/common/players.py +++ b/powerline/segments/common/players.py @@ -5,7 +5,7 @@ import sys from powerline.lib.shell import asrun, run_cmd from powerline.lib.unicode import out_u -from powerline.segments import Segment +from powerline.segments import Segment, with_docstring STATE_SYMBOLS = { @@ -16,9 +16,25 @@ STATE_SYMBOLS = { } -class NowPlayingSegment(Segment): - def __call__(self, player='mpd', format='{state_symbol} {artist} - {title} ({total})', state_symbols=STATE_SYMBOLS, **kwargs): - player_func = getattr(self, 'player_{0}'.format(player)) +def _convert_state(state): + '''Guess player state''' + state = state.lower() + if 'play' in state: + return 'play' + if 'pause' in state: + return 'pause' + if 'stop' in state: + return 'stop' + return 'fallback' + + +def _convert_seconds(seconds): + '''Convert seconds to minutes:seconds format''' + return '{0:.0f}:{1:02.0f}'.format(*divmod(float(seconds), 60)) + + +class PlayerSegment(Segment): + def __call__(self, format='{state_symbol} {artist} - {title} ({total})', state_symbols=STATE_SYMBOLS, **kwargs): stats = { 'state': 'fallback', 'album': None, @@ -27,28 +43,72 @@ class NowPlayingSegment(Segment): 'elapsed': None, 'total': None, } - func_stats = player_func(**kwargs) + func_stats = self.get_player_status(**kwargs) if not func_stats: return None stats.update(func_stats) stats['state_symbol'] = state_symbols.get(stats['state']) return format.format(**stats) - @staticmethod - def _convert_state(state): - state = state.lower() - if 'play' in state: - return 'play' - if 'pause' in state: - return 'pause' - if 'stop' in state: - return 'stop' + def argspecobjs(self): + for ret in super(PlayerSegment, self).argspecobjs(): + yield ret + yield 'get_player_status', self.get_player_status - @staticmethod - def _convert_seconds(seconds): - return '{0:.0f}:{1:02.0f}'.format(*divmod(float(seconds), 60)) + def omitted_args(self, name, method): + return (0,) - def player_cmus(self, pl): + +_common_args = ''' +This player segment should be added like this: + +.. code-block:: json + + {{ + "function": "powerline.segments.common.players.{0}", + "name": "player" + }} + +(with additional ``"args": {{…}}`` if needed). + +:param str format: + Format used for displaying data from player. Should be a str.format-like + string with the following keyword parameters: + + +------------+-------------------------------------------------------------+ + |Parameter |Description | + +============+=============================================================+ + |state_symbol|Symbol displayed for play/pause/stop states. There is also | + | |“fallback” state used in case function failed to get player | + | |state. For this state symbol is by default empty. All | + | |symbols are defined in ``state_symbols`` argument. | + +------------+-------------------------------------------------------------+ + |album |Album that is currently played. | + +------------+-------------------------------------------------------------+ + |artist |Artist whose song is currently played | + +------------+-------------------------------------------------------------+ + |title |Currently played composition. | + +------------+-------------------------------------------------------------+ + |elapsed |Composition duration in format M:SS (minutes:seconds). | + +------------+-------------------------------------------------------------+ + |total |Composition length in format M:SS. | + +------------+-------------------------------------------------------------+ +:param dict state_symbols: + Symbols used for displaying state. Must contain all of the following keys: + + ======== ======================================================== + Key Description + ======== ======================================================== + play Displayed when player is playing. + pause Displayed when player is paused. + stop Displayed when player is not playing anything. + fallback Displayed if state is not one of the above or not known. + ======== ======================================================== +''' + + +class CmusPlayerSegment(PlayerSegment): + def get_player_status(self, pl): '''Return cmus player information. cmus-remote -Q returns data with multi-level information i.e. @@ -75,17 +135,28 @@ class NowPlayingSegment(Segment): now_playing = dict(((token[0] if token[0] not in ignore_levels else token[1], (' '.join(token[1:]) if token[0] not in ignore_levels else ' '.join(token[2:]))) for token in [line.split(' ') for line in now_playing_str.split('\n')[:-1]])) - state = self._convert_state(now_playing.get('status')) + state = _convert_state(now_playing.get('status')) return { 'state': state, 'album': now_playing.get('album'), 'artist': now_playing.get('artist'), 'title': now_playing.get('title'), - 'elapsed': self._convert_seconds(now_playing.get('position', 0)), - 'total': self._convert_seconds(now_playing.get('duration', 0)), + 'elapsed': _convert_seconds(now_playing.get('position', 0)), + 'total': _convert_seconds(now_playing.get('duration', 0)), } - def player_mpd(self, pl, host='localhost', port=6600): + +cmus = with_docstring(CmusPlayerSegment(), +('''Return CMUS player information + +Requires cmus-remote command be acessible from $PATH. + +{0} +''').format(_common_args.format('cmus'))) + + +class MpdPlayerSegment(PlayerSegment): + def get_player_status(self, pl, host='localhost', port=6600): try: import mpd except ImportError: @@ -113,16 +184,33 @@ class NowPlayingSegment(Segment): 'album': now_playing.get('album'), 'artist': now_playing.get('artist'), 'title': now_playing.get('title'), - 'elapsed': self._convert_seconds(now_playing.get('elapsed', 0)), - 'total': self._convert_seconds(now_playing.get('time', 0)), + 'elapsed': _convert_seconds(now_playing.get('elapsed', 0)), + 'total': _convert_seconds(now_playing.get('time', 0)), } - def player_dbus(self, player_name, bus_name, player_path, iface_prop, iface_player): - try: - import dbus - except ImportError: - self.exception('Could not add {0} segment: requires dbus module', player_name) - return + +mpd = with_docstring(MpdPlayerSegment(), +('''Return Music Player Daemon information + +Requires mpc command to be acessible from $PATH or ``mpd`` Python module. + +{0} +:param str host: + Host on which mpd runs. +:param int port: + Port which should be connected to. +''').format(_common_args.format('mpd'))) + + +try: + import dbus +except ImportError: + def _get_dbus_player_status(pl, player_name, **kwargs): + pl.error('Could not add {0} segment: requires dbus module', player_name) + return +else: + def _get_dbus_player_status(pl, bus_name, player_path, iface_prop, + iface_player, player_name='player'): bus = dbus.SessionBus() try: player = bus.get_object(bus_name, player_path) @@ -136,7 +224,7 @@ class NowPlayingSegment(Segment): album = out_u(info.get('xesam:album')) title = out_u(info.get('xesam:title')) artist = info.get('xesam:artist') - state = self._convert_state(status) + state = _convert_state(status) if artist: artist = out_u(artist[0]) return { @@ -144,11 +232,38 @@ class NowPlayingSegment(Segment): 'album': album, 'artist': artist, 'title': title, - 'total': self._convert_seconds(info.get('mpris:length') / 1e6), + 'total': _convert_seconds(info.get('mpris:length') / 1e6), } - def player_spotify_dbus(self, pl): - return self.player_dbus( + +class DbusPlayerSegment(PlayerSegment): + get_player_status = staticmethod(_get_dbus_player_status) + + +dbus_player = with_docstring(DbusPlayerSegment(), +('''Return generic dbus player state + +Requires ``dbus`` python module. Only for players that support specific protocol + (e.g. like :py:func:`spotify` and :py:func:`clementine`). + +{0} +:param str player_name: + Player name. Used in error messages only. +:param str bus_name: + Dbus bus name. +:param str player_path: + Path to the player on the given bus. +:param str iface_prop: + Interface properties name for use with dbus.Interface. +:param str iface_player: + Player name. +''').format(_common_args.format('dbus_player'))) + + +class SpotifyDbusPlayerSegment(PlayerSegment): + def get_player_status(self, pl): + return _get_dbus_player_status( + pl=pl, player_name='Spotify', bus_name='com.spotify.qt', player_path='/', @@ -156,16 +271,18 @@ class NowPlayingSegment(Segment): iface_player='org.freedesktop.MediaPlayer2', ) - def player_clementine(self, pl): - return self.player_dbus( - player_name='Clementine', - bus_name='org.mpris.MediaPlayer2.clementine', - player_path='/org/mpris/MediaPlayer2', - iface_prop='org.freedesktop.DBus.Properties', - iface_player='org.mpris.MediaPlayer2.Player', - ) - def player_spotify_apple_script(self, pl): +spotify_dbus = with_docstring(SpotifyDbusPlayerSegment(), +('''Return spotify player information + +Requires ``dbus`` python module. + +{0} +''').format(_common_args.format('spotify_dbus'))) + + +class SpotifyAppleScriptPlayerSegment(PlayerSegment): + def get_player_status(self, pl): status_delimiter = '-~`/=' ascript = ''' tell application "System Events" @@ -196,7 +313,7 @@ class NowPlayingSegment(Segment): return None spotify_status = spotify.split(status_delimiter) - state = self._convert_state(spotify_status[0]) + state = _convert_state(spotify_status[0]) if state == 'stop': return None return { @@ -204,21 +321,58 @@ class NowPlayingSegment(Segment): 'album': spotify_status[1], 'artist': spotify_status[2], 'title': spotify_status[3], - 'total': self._convert_seconds(int(spotify_status[4])) + 'total': _convert_seconds(int(spotify_status[4])) } - try: - __import__('dbus') - except ImportError: - if sys.platform.startswith('darwin'): - player_spotify = player_spotify_apple_script - else: - player_spotify = player_spotify_dbus - else: - player_spotify = player_spotify_dbus - def player_rhythmbox(self, pl): - now_playing = run_cmd(pl, ['rhythmbox-client', '--no-start', '--no-present', '--print-playing-format', '%at\n%aa\n%tt\n%te\n%td']) +spotify_apple_script = with_docstring(SpotifyAppleScriptPlayerSegment(), +('''Return spotify player information + +Requires ``osascript`` available in $PATH. + +{0} +''').format(_common_args.format('spotify_apple_script'))) + + +if 'dbus' in globals() or not sys.platform.startswith('darwin'): + spotify = spotify_dbus + _old_name = 'spotify_dbus' +else: + spotify = spotify_apple_script + _old_name = 'spotify_apple_script' + + +spotify = with_docstring(spotify, spotify.__doc__.replace(_old_name, 'spotify')) + + +class ClementinePlayerSegment(PlayerSegment): + def get_player_status(self, pl): + return _get_dbus_player_status( + pl=pl, + player_name='Clementine', + bus_name='org.mpris.MediaPlayer2.clementine', + player_path='/org/mpris/MediaPlayer2', + iface_prop='org.freedesktop.DBus.Properties', + iface_player='org.mpris.MediaPlayer2.Player', + ) + + +clementine = with_docstring(ClementinePlayerSegment(), +('''Return clementine player information + +Requires ``dbus`` python module. + +{0} +''').format(_common_args.format('clementine'))) + + +class RhythmboxPlayerSegment(PlayerSegment): + def get_player_status(self, pl): + now_playing = run_cmd(pl, [ + 'rhythmbox-client', + '--no-start', '--no-present', + '--print-playing-format', '%at\n%aa\n%tt\n%te\n%td' + ]) if not now_playing: return now_playing = now_playing.split('\n') @@ -230,7 +384,18 @@ class NowPlayingSegment(Segment): 'total': now_playing[4], } - def player_rdio(self, pl): + +rhythmbox = with_docstring(RhythmboxPlayerSegment(), +('''Return rhythmbox player information + +Requires ``rhythmbox-client`` available in $PATH. + +{0} +''').format(_common_args.format('rhythmbox'))) + + +class RDIOPlayerSegment(PlayerSegment): + def get_player_status(self, pl): status_delimiter = '-~`/=' ascript = ''' tell application "System Events" @@ -255,9 +420,9 @@ class NowPlayingSegment(Segment): now_playing = now_playing.split('\n') if len(now_playing) != 6: return - state = self._convert_state(now_playing[5]) - total = self._convert_seconds(now_playing[4]) - elapsed = self._convert_seconds(float(now_playing[3]) * float(now_playing[4]) / 100) + state = _convert_state(now_playing[5]) + total = _convert_seconds(now_playing[4]) + elapsed = _convert_seconds(float(now_playing[3]) * float(now_playing[4]) / 100) return { 'title': now_playing[0], 'artist': now_playing[1], @@ -267,4 +432,22 @@ class NowPlayingSegment(Segment): 'state': state, 'state_symbol': self.STATE_SYMBOLS.get(state) } + + +rdio = with_docstring(RDIOPlayerSegment(), +('''Return rdio player information + +Requires ``osascript`` available in $PATH. + +{0} +''').format(_common_args.format('rdio'))) + + +class NowPlayingSegment(Segment): + def __call__(self, player='mpd', **kwargs): + player_segment = globals()[player] + assert(isinstance(player_segment, PlayerSegment)) + return player_segment(**kwargs) + + now_playing = NowPlayingSegment() From f233c1f77e801ce6f3eec8f3bff4b6998ef08493 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 14:58:48 +0400 Subject: [PATCH 2/7] Fix mpd support --- powerline/lib/shell.py | 6 ++++-- powerline/segments/common/players.py | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/powerline/lib/shell.py b/powerline/lib/shell.py index df7cc65e..fcdc35ce 100644 --- a/powerline/lib/shell.py +++ b/powerline/lib/shell.py @@ -16,7 +16,7 @@ if sys.platform.startswith('win32'): Popen = partial(Popen, creationflags=0x08000000) -def run_cmd(pl, cmd, stdin=None): +def run_cmd(pl, cmd, stdin=None, strip=True): '''Run command and return its stdout, stripped If running command fails returns None and logs failure to ``pl`` argument. @@ -27,6 +27,8 @@ def run_cmd(pl, cmd, stdin=None): Command which will be run. :param str stdin: String passed to command. May be None. + :param bool strip: + True if the result should be stripped. ''' try: p = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE) @@ -36,7 +38,7 @@ def run_cmd(pl, cmd, stdin=None): else: stdout, err = p.communicate(stdin) stdout = stdout.decode(get_preferred_input_encoding()) - return stdout.strip() + return stdout.strip() if strip else stdout def asrun(pl, ascript): diff --git a/powerline/segments/common/players.py b/powerline/segments/common/players.py index 47dc8f0b..1dedebba 100644 --- a/powerline/segments/common/players.py +++ b/powerline/segments/common/players.py @@ -160,7 +160,12 @@ class MpdPlayerSegment(PlayerSegment): try: import mpd except ImportError: - now_playing = run_cmd(pl, ['mpc', 'current', '-f', '%album%\n%artist%\n%title%\n%time%', '-h', str(host), '-p', str(port)]) + now_playing = run_cmd(pl, [ + 'mpc', 'current', + '-f', '%album%\n%artist%\n%title%\n%time%', + '-h', str(host), + '-p', str(port) + ], strip=False) if not now_playing: return now_playing = now_playing.split('\n') @@ -372,7 +377,7 @@ class RhythmboxPlayerSegment(PlayerSegment): 'rhythmbox-client', '--no-start', '--no-present', '--print-playing-format', '%at\n%aa\n%tt\n%te\n%td' - ]) + ], strip=False) if not now_playing: return now_playing = now_playing.split('\n') From cdb030ec03a6a5d37eae01b17aa0f61729ce7305 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 15:05:28 +0400 Subject: [PATCH 3/7] Rename highlight groups --- powerline/config_files/colorschemes/default.json | 3 ++- powerline/segments/common/players.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/powerline/config_files/colorschemes/default.json b/powerline/config_files/colorschemes/default.json index 7f234506..7bf84501 100644 --- a/powerline/config_files/colorschemes/default.json +++ b/powerline/config_files/colorschemes/default.json @@ -28,7 +28,8 @@ "battery_gradient": { "fg": "white_red", "bg": "gray0", "attr": [] }, "battery_full": { "fg": "red", "bg": "gray0", "attr": [] }, "battery_empty": { "fg": "white", "bg": "gray0", "attr": [] }, - "now_playing": { "fg": "gray10", "bg": "black", "attr": [] }, + "now_playing": "player", + "player": { "fg": "gray10", "bg": "black", "attr": [] }, "user": { "fg": "white", "bg": "darkblue", "attr": ["bold"] }, "superuser": { "fg": "white", "bg": "brightred", "attr": ["bold"] }, "branch": { "fg": "gray9", "bg": "gray2", "attr": [] }, diff --git a/powerline/segments/common/players.py b/powerline/segments/common/players.py index 1dedebba..57e5e403 100644 --- a/powerline/segments/common/players.py +++ b/powerline/segments/common/players.py @@ -48,7 +48,10 @@ class PlayerSegment(Segment): return None stats.update(func_stats) stats['state_symbol'] = state_symbols.get(stats['state']) - return format.format(**stats) + return [{ + 'contents': format.format(**stats), + 'highlight_group': ['player_' + (stats['state'] or 'fallback'), 'player', 'now_playing'], + }] def argspecobjs(self): for ret in super(PlayerSegment, self).argspecobjs(): @@ -71,6 +74,8 @@ This player segment should be added like this: (with additional ``"args": {{…}}`` if needed). +Highlight groups used: ``player_fallback`` or ``player``, ``player_play`` or ``player``, ``player_pause`` or ``player``, ``player_stop`` or ``player``. + :param str format: Format used for displaying data from player. Should be a str.format-like string with the following keyword parameters: From dad11972841c50800d21292f5582e121faf294b5 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 15:09:57 +0400 Subject: [PATCH 4/7] Add `"player"` segment data --- powerline/config_files/themes/ascii.json | 10 ++++++++++ powerline/config_files/themes/powerline.json | 10 ++++++++++ powerline/config_files/themes/unicode.json | 10 ++++++++++ powerline/config_files/themes/unicode_terminus.json | 10 ++++++++++ .../themes/unicode_terminus_condensed.json | 10 ++++++++++ 5 files changed, 50 insertions(+) diff --git a/powerline/config_files/themes/ascii.json b/powerline/config_files/themes/ascii.json index 7b49da21..80b2fcb7 100644 --- a/powerline/config_files/themes/ascii.json +++ b/powerline/config_files/themes/ascii.json @@ -20,6 +20,16 @@ "ellipsis": "..." } }, + "player": { + "args": { + "state_symbols": { + "fallback": "", + "play": ">", + "pause": "~", + "stop": "X" + } + } + }, "line_current_symbol": { "contents": "LN " diff --git a/powerline/config_files/themes/powerline.json b/powerline/config_files/themes/powerline.json index bba3b541..ddc6b60c 100644 --- a/powerline/config_files/themes/powerline.json +++ b/powerline/config_files/themes/powerline.json @@ -23,6 +23,16 @@ "line_current_symbol": { "contents": " " }, + "player": { + "args": { + "state_symbols": { + "fallback": "♫", + "play": "▶", + "pause": "▮▮", + "stop": "■" + } + } + }, "time": { "before": "⌚ " diff --git a/powerline/config_files/themes/unicode.json b/powerline/config_files/themes/unicode.json index de74c23b..d1461ee0 100644 --- a/powerline/config_files/themes/unicode.json +++ b/powerline/config_files/themes/unicode.json @@ -19,6 +19,16 @@ "ellipsis": "⋯" } }, + "player": { + "args": { + "state_symbols": { + "fallback": "♫", + "play": "▶", + "pause": "▮▮", + "stop": "■" + } + } + }, "line_current_symbol": { "contents": "␤ " diff --git a/powerline/config_files/themes/unicode_terminus.json b/powerline/config_files/themes/unicode_terminus.json index 33a06395..d4df1fdd 100644 --- a/powerline/config_files/themes/unicode_terminus.json +++ b/powerline/config_files/themes/unicode_terminus.json @@ -23,6 +23,16 @@ "line_current_symbol": { "contents": "␤ " }, + "player": { + "args": { + "state_symbols": { + "fallback": "♫", + "play": "▶", + "pause": "▮▮", + "stop": "■" + } + } + }, "time": { "before": "" diff --git a/powerline/config_files/themes/unicode_terminus_condensed.json b/powerline/config_files/themes/unicode_terminus_condensed.json index a386c7cd..e153a93c 100644 --- a/powerline/config_files/themes/unicode_terminus_condensed.json +++ b/powerline/config_files/themes/unicode_terminus_condensed.json @@ -24,6 +24,16 @@ "line_current_symbol": { "contents": "␤" }, + "player": { + "args": { + "state_symbols": { + "fallback": "♫", + "play": "▶", + "pause": "▮▮", + "stop": "■" + } + } + }, "time": { "before": "" From 8551e51b6d2a9fd268590725807ad67ad2214f22 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 15:16:26 +0400 Subject: [PATCH 5/7] Prioritize now_playing highlight group, but remove it from colorscheme Reasoning for first: backwards compatibility. If user defined `now_playing` highlight group it is what should be used. Reasoning for second: it is useless there. If user has defined its own group its effect will not be affected, if he has not then there is nothing to talk about. --- powerline/config_files/colorschemes/default.json | 1 - powerline/segments/common/players.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/powerline/config_files/colorschemes/default.json b/powerline/config_files/colorschemes/default.json index 7bf84501..2c138bd3 100644 --- a/powerline/config_files/colorschemes/default.json +++ b/powerline/config_files/colorschemes/default.json @@ -28,7 +28,6 @@ "battery_gradient": { "fg": "white_red", "bg": "gray0", "attr": [] }, "battery_full": { "fg": "red", "bg": "gray0", "attr": [] }, "battery_empty": { "fg": "white", "bg": "gray0", "attr": [] }, - "now_playing": "player", "player": { "fg": "gray10", "bg": "black", "attr": [] }, "user": { "fg": "white", "bg": "darkblue", "attr": ["bold"] }, "superuser": { "fg": "white", "bg": "brightred", "attr": ["bold"] }, diff --git a/powerline/segments/common/players.py b/powerline/segments/common/players.py index 57e5e403..45568728 100644 --- a/powerline/segments/common/players.py +++ b/powerline/segments/common/players.py @@ -50,7 +50,7 @@ class PlayerSegment(Segment): stats['state_symbol'] = state_symbols.get(stats['state']) return [{ 'contents': format.format(**stats), - 'highlight_group': ['player_' + (stats['state'] or 'fallback'), 'player', 'now_playing'], + 'highlight_group': ['now_playing', 'player_' + (stats['state'] or 'fallback'), 'player'], }] def argspecobjs(self): From b8a4d9b0540d3efffe207e869a512ea1a73c500f Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 16:33:34 +0400 Subject: [PATCH 6/7] Add some workarounds for powerline-lint --- docs/source/develop/segments.rst | 18 ++++++++++++++++++ powerline/lint/__init__.py | 7 ++++++- powerline/lint/checks.py | 17 +++++++++++++++++ powerline/segments/common/players.py | 17 +++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/docs/source/develop/segments.rst b/docs/source/develop/segments.rst index f69a600c..66003a72 100644 --- a/docs/source/develop/segments.rst +++ b/docs/source/develop/segments.rst @@ -29,6 +29,24 @@ object it should receive the following arguments: And also any other argument(s) specified by user in :ref:`args key ` (no additional arguments by default). +.. note:: + For powerline-lint to work properly the following things may be needed: + + #. If your segment is a :py:class:`powerline.segments.Segment` and used + arguments are scattered over multiple methods + :py:meth:`powerline.segments.Segment.argspecobjs` should be overridden in + subclass to tell powerline-lint which objects should be inspected for + arguments. + #. If your segment takes some arguments that are never listed, but accessed + via ``kwargs.get()`` or you cannot use previous function for whatever + reason :py:meth:`powerline.segments.Segment.additional_args` should be + overridden in subclass. + #. If you are expecting user to use one :ref:`name ` + for multiple segments which cannot be linked to the segment function + automatically by powerline-lint (e.g. because there are no instances of + the segments in question in the default configuration) you should use + :py:func:`powerline.lint.checks.register_common_name`. + Object representing segment may have the following attributes used by powerline: diff --git a/powerline/lint/__init__.py b/powerline/lint/__init__.py index 49cb511a..51c25860 100644 --- a/powerline/lint/__init__.py +++ b/powerline/lint/__init__.py @@ -19,7 +19,7 @@ from powerline.lint.checks import (check_matcher_func, check_ext, check_config, check_segment_module, check_exinclude_function, type_keys, check_segment_function, check_args, get_one_segment_function, check_highlight_groups, check_highlight_group, check_full_segment_data, - get_all_possible_functions, check_segment_data_key) + get_all_possible_functions, check_segment_data_key, register_common_name) from powerline.lint.spec import Spec from powerline.lint.context import Context @@ -289,6 +289,10 @@ theme_spec = common_theme_spec().update( ) +def register_common_names(): + register_common_name('player', 'powerline.segments.common.players', '_player') + + def check(paths=None, debug=False, echoerr=echoerr, require_ext=None): '''Check configuration sanity @@ -308,6 +312,7 @@ def check(paths=None, debug=False, echoerr=echoerr, require_ext=None): ``False`` if user configuration seems to be completely sane and ``True`` if some problems were found. ''' + register_common_names() search_paths = paths or get_config_paths() find_config_files = generate_config_finder(lambda: search_paths) diff --git a/powerline/lint/checks.py b/powerline/lint/checks.py index a59d4440..52abdb78 100644 --- a/powerline/lint/checks.py +++ b/powerline/lint/checks.py @@ -5,6 +5,8 @@ import os import re import logging +from collections import defaultdict + from powerline.lib.threaded import ThreadedSegment from powerline.lib.unicode import unicode from powerline.lint.markedjson.markedvalue import MarkedUnicode @@ -673,6 +675,16 @@ def get_one_segment_function(data, context, echoerr): yield func +common_names = defaultdict(set) + + +def register_common_name(name, cmodule, cname): + s = cmodule + '.' + cname + cmodule_mark = Mark('', 1, 1, s, 1) + cname_mark = Mark('', 1, len(cmodule) + 1, s, len(cmodule) + 1) + common_names[name].add((MarkedUnicode(cmodule, cmodule_mark), MarkedUnicode(cname, cname_mark))) + + def get_all_possible_functions(data, context, echoerr): name = context[-2][0] module, name = name.rpartition('.')[::2] @@ -681,6 +693,11 @@ def get_all_possible_functions(data, context, echoerr): if func: yield func else: + if name in common_names: + for cmodule, cname in common_names[name]: + cfunc = import_segment(cname, data, context, echoerr, module=MarkedUnicode(cmodule, None)) + if cfunc: + yield cfunc for ext, theme_config in list_themes(data, context): for segments in theme_config.get('segments', {}).values(): for segment in segments: diff --git a/powerline/segments/common/players.py b/powerline/segments/common/players.py index 45568728..838ff1c7 100644 --- a/powerline/segments/common/players.py +++ b/powerline/segments/common/players.py @@ -53,6 +53,9 @@ class PlayerSegment(Segment): 'highlight_group': ['now_playing', 'player_' + (stats['state'] or 'fallback'), 'player'], }] + def get_player_status(self, pl): + pass + def argspecobjs(self): for ret in super(PlayerSegment, self).argspecobjs(): yield ret @@ -112,6 +115,9 @@ Highlight groups used: ``player_fallback`` or ``player``, ``player_play`` or ``p ''' +_player = with_docstring(PlayerSegment(), _common_args.format('_player')) + + class CmusPlayerSegment(PlayerSegment): def get_player_status(self, pl): '''Return cmus player information. @@ -459,5 +465,16 @@ class NowPlayingSegment(Segment): assert(isinstance(player_segment, PlayerSegment)) return player_segment(**kwargs) + def argspecobjs(self): + for ret in super(NowPlayingSegment, self).argspecobjs(): + yield ret + yield '__call__', PlayerSegment.__call__ + for k, v in globals().items(): + if isinstance(v, type) and issubclass(v, PlayerSegment) and v is not DbusPlayerSegment: + yield 'get_player_status', v.get_player_status + + def omitted_args(self, name, method): + return (0,) + now_playing = NowPlayingSegment() From 425e8f744de61451d40779324e640d500e0ebd40 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Oct 2014 16:53:53 +0400 Subject: [PATCH 7/7] Remove self.STATE_SYMBOLS reference: there is no such attribute --- powerline/segments/common/players.py | 1 - 1 file changed, 1 deletion(-) diff --git a/powerline/segments/common/players.py b/powerline/segments/common/players.py index 838ff1c7..a53d36d9 100644 --- a/powerline/segments/common/players.py +++ b/powerline/segments/common/players.py @@ -446,7 +446,6 @@ class RDIOPlayerSegment(PlayerSegment): 'elapsed': elapsed, 'total': total, 'state': state, - 'state_symbol': self.STATE_SYMBOLS.get(state) }