From 6f2c8f2e4042b63e11b8aa55dea07f2183bfa4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Tue, 14 Jul 2015 16:14:29 +0200 Subject: [PATCH 01/23] Added `run_in_background` function. --- i3pystatus/core/command.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index 8303b4a..fb975b7 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -1,4 +1,4 @@ -# from subprocess import CalledProcessError +import logging from collections import namedtuple import subprocess @@ -14,6 +14,9 @@ def run_through_shell(command, enable_shell=False): in one variable """ + if not enable_shell and not isinstance(command, list): + command = command.split() + returncode = None stderr = None try: @@ -27,7 +30,35 @@ def run_through_shell(command, enable_shell=False): except OSError as e: out = e.strerror stderr = e.strerror + logging.getLogger("i3pystatus.command").exception("") except subprocess.CalledProcessError as e: out = e.output + logging.getLogger("i3pystatus.command").exception("") return CommandResult(returncode, out, stderr) + + +def run_in_background(command, detach=False): + """ + Runs a command in background. + No output is retrieved. + Useful for running GUI applications that would block click events. + + :param detach If set to `True` the application will be executed using the + `i3-msg` command. + """ + + if not isinstance(command, list): + command = command.split() + + if detach: + command.insert(0, "exec") + command.insert(0, "i3-msg") + + try: + subprocess.Popen(command, stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + except OSError: + logging.getLogger("i3pystatus.command").exception("") + except subprocess.CalledProcessError: + logging.getLogger("i3pystatus.command").exception("") From e6d88abaae6bb84a8422cbc22f69bd567bb0ad4a Mon Sep 17 00:00:00 2001 From: schroeji Date: Thu, 17 Sep 2015 12:10:49 +0200 Subject: [PATCH 02/23] Added yaourt backend for the updates script. --- i3pystatus/updates/yaourt.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 i3pystatus/updates/yaourt.py diff --git a/i3pystatus/updates/yaourt.py b/i3pystatus/updates/yaourt.py new file mode 100644 index 0000000..023aaea --- /dev/null +++ b/i3pystatus/updates/yaourt.py @@ -0,0 +1,17 @@ +""" +This module counts the available updates using yaourt. +You should not use it together with the pacman backend otherwise +some packeges might be counted twice. +""" + +from i3pystatus.core.command import run_through_shell +from i3pystatus.updates import Backend + +class Yaourt(Backend): + + @property + def updates(self): + command = ["yaourt", "-Qua"] + checkupdates = run_through_shell(command) + return checkupdates.out.count('\n') +Backend = Yaourt \ No newline at end of file From 1e6e42a3d8aadba52c72cde76c04f9788e6eb2bb Mon Sep 17 00:00:00 2001 From: schroeji Date: Thu, 17 Sep 2015 13:42:22 +0200 Subject: [PATCH 03/23] Fixed for Travis CI --- i3pystatus/updates/yaourt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/i3pystatus/updates/yaourt.py b/i3pystatus/updates/yaourt.py index 023aaea..9d68455 100644 --- a/i3pystatus/updates/yaourt.py +++ b/i3pystatus/updates/yaourt.py @@ -7,6 +7,7 @@ some packeges might be counted twice. from i3pystatus.core.command import run_through_shell from i3pystatus.updates import Backend + class Yaourt(Backend): @property @@ -14,4 +15,4 @@ class Yaourt(Backend): command = ["yaourt", "-Qua"] checkupdates = run_through_shell(command) return checkupdates.out.count('\n') -Backend = Yaourt \ No newline at end of file +Backend = Yaourt From f577bd2bf9eeb972e7a37e9aceb64af758e9354b Mon Sep 17 00:00:00 2001 From: schroeji Date: Mon, 21 Sep 2015 13:08:05 +0200 Subject: [PATCH 04/23] Added aur_only mode for yaourt backend --- i3pystatus/updates/yaourt.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/i3pystatus/updates/yaourt.py b/i3pystatus/updates/yaourt.py index 9d68455..50a4770 100644 --- a/i3pystatus/updates/yaourt.py +++ b/i3pystatus/updates/yaourt.py @@ -1,18 +1,25 @@ """ This module counts the available updates using yaourt. -You should not use it together with the pacman backend otherwise -some packeges might be counted twice. +By default it will only count aur packages. +If you want to count both pacman and aur packages set the variable +count_only_aur = False """ - +import re from i3pystatus.core.command import run_through_shell from i3pystatus.updates import Backend class Yaourt(Backend): - + def __init__(self, aur_only = True): + self.aur_only = aur_only + @property + def updates(self): command = ["yaourt", "-Qua"] checkupdates = run_through_shell(command) - return checkupdates.out.count('\n') + if(self.aur_only): + return len( re.findall("^aur/", checkupdates.out, flags=re.M) ) + return checkupdates.out.count("\n") + Backend = Yaourt From 375b77178da90aaa799aa45b35ce78a41fa2b641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Mon, 21 Sep 2015 23:09:19 +0200 Subject: [PATCH 05/23] Renamed 'run_in_background' to 'execute'. --- i3pystatus/core/command.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index fb975b7..4da74a4 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -30,22 +30,22 @@ def run_through_shell(command, enable_shell=False): except OSError as e: out = e.strerror stderr = e.strerror - logging.getLogger("i3pystatus.command").exception("") + logging.getLogger("i3pystatus.core.command").exception("") except subprocess.CalledProcessError as e: out = e.output - logging.getLogger("i3pystatus.command").exception("") + logging.getLogger("i3pystatus.core.command").exception("") return CommandResult(returncode, out, stderr) -def run_in_background(command, detach=False): +def execute(command, detach=False): """ Runs a command in background. No output is retrieved. Useful for running GUI applications that would block click events. :param detach If set to `True` the application will be executed using the - `i3-msg` command. + `i3-msg` command (survives i3 in-place restart). """ if not isinstance(command, list): @@ -59,6 +59,6 @@ def run_in_background(command, detach=False): subprocess.Popen(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except OSError: - logging.getLogger("i3pystatus.command").exception("") + logging.getLogger("i3pystatus.core.command").exception("") except subprocess.CalledProcessError: - logging.getLogger("i3pystatus.command").exception("") + logging.getLogger("i3pystatus.core.command").exception("") From 4bfe04dab6f87efc70954a8f20ec26d8d7c71ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Tue, 22 Sep 2015 10:35:42 +0200 Subject: [PATCH 06/23] Module: Clickevents now do not wait for commands output. Fix for #254. --- i3pystatus/core/modules.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py index 700af3e..ae4cbf0 100644 --- a/i3pystatus/core/modules.py +++ b/i3pystatus/core/modules.py @@ -1,6 +1,7 @@ from i3pystatus.core.settings import SettingsBase from i3pystatus.core.threading import Manager from i3pystatus.core.util import convert_position +from i3pystatus.core.command import execute from i3pystatus.core.command import run_through_shell @@ -106,7 +107,7 @@ class Module(SettingsBase): if cb is not "run": getattr(self, cb)(*args) else: - run_through_shell(cb, *args) + execute(cb, *args) return True def move(self, position): From 84d5fc8efcd14add5906426e218516f348a3a498 Mon Sep 17 00:00:00 2001 From: facetoe Date: Wed, 23 Sep 2015 22:05:09 +0800 Subject: [PATCH 07/23] Remove PRAW deprecation warning. --- i3pystatus/reddit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/reddit.py b/i3pystatus/reddit.py index 3535fbd..12694d5 100644 --- a/i3pystatus/reddit.py +++ b/i3pystatus/reddit.py @@ -73,7 +73,7 @@ class Reddit(IntervalModule): r = praw.Reddit(user_agent='i3pystatus') if self.password: - r.login(self.username, self.password) + r.login(self.username, self.password, disable_warning=True) unread_messages = sum(1 for i in r.get_unread()) if unread_messages: d = vars(next(r.get_unread())) From b1341741ccc5b2b2f55139a88fea52a5fd5af5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Thu, 24 Sep 2015 19:08:41 +0200 Subject: [PATCH 08/23] Docs: Added missing rule for members of `i3pystatus.core.command` module. Docs: Sorted core package modules alphabetically. --- docs/i3pystatus.core.rst | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/i3pystatus.core.rst b/docs/i3pystatus.core.rst index b6d15c2..054c174 100644 --- a/docs/i3pystatus.core.rst +++ b/docs/i3pystatus.core.rst @@ -9,6 +9,21 @@ core Package :undoc-members: :show-inheritance: +:mod:`color` Module +------------------- + +.. automodule:: i3pystatus.core.color + :members: + :undoc-members: + :show-inheritance: + +:mod:`command` Module +--------------------- + +.. automodule:: i3pystatus.core.command + :members: + :undoc-members: + :show-inheritance: :mod:`desktop` Module --------------------- @@ -74,11 +89,4 @@ core Package :undoc-members: :show-inheritance: -:mod:`color` Module -------------------- - -.. automodule:: i3pystatus.core.color - :members: - :undoc-members: - :show-inheritance: From e9835070e2a9d822fc43e4b1e3ae1d3564b5cee2 Mon Sep 17 00:00:00 2001 From: schroeji Date: Fri, 25 Sep 2015 11:15:56 +0200 Subject: [PATCH 09/23] Fix for Travis CI --- i3pystatus/updates/yaourt.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/i3pystatus/updates/yaourt.py b/i3pystatus/updates/yaourt.py index 50a4770..4164731 100644 --- a/i3pystatus/updates/yaourt.py +++ b/i3pystatus/updates/yaourt.py @@ -10,16 +10,15 @@ from i3pystatus.updates import Backend class Yaourt(Backend): - def __init__(self, aur_only = True): + def __init__(self, aur_only=True): self.aur_only = aur_only - + @property - def updates(self): command = ["yaourt", "-Qua"] checkupdates = run_through_shell(command) if(self.aur_only): - return len( re.findall("^aur/", checkupdates.out, flags=re.M) ) + return len(re.findall("^aur/", checkupdates.out, flags=re.M)) return checkupdates.out.count("\n") Backend = Yaourt From 97e2ad7bbaf6409cef34ff51568e94489fbb0299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 13:55:56 +0200 Subject: [PATCH 10/23] Fixed detached mode of `execute` and updated its docstring. --- i3pystatus/core/command.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index 4da74a4..42d1bda 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -40,20 +40,24 @@ def run_through_shell(command, enable_shell=False): def execute(command, detach=False): """ - Runs a command in background. - No output is retrieved. - Useful for running GUI applications that would block click events. + Runs a command in background. No output is retrieved. Useful for running GUI + applications that would block click events. - :param detach If set to `True` the application will be executed using the - `i3-msg` command (survives i3 in-place restart). + :param command: A string or a list of strings containing the name and + arguments of the program. + :param detach: If set to `True` the program will be executed using the + `i3-msg` command. As a result the program is executed independent of + i3pystatus as a child of i3 process. Because of how i3-msg parses its + arguments the type of `command` is limited to string in this mode. """ - if not isinstance(command, list): - command = command.split() - if detach: - command.insert(0, "exec") - command.insert(0, "i3-msg") + if not isinstance(command, str): + raise TypeError("Detached mode expects a string as command.") + command = ["i3-msg", "exec", command] + else: + if not isinstance(command, list): + command = command.split() try: subprocess.Popen(command, stdin=subprocess.DEVNULL, From 32ab77c2039bf0b27426cb2cdc61b5a6c9b8c5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 13:57:09 +0200 Subject: [PATCH 11/23] Module: External programs are launched in detached mode. --- i3pystatus/core/modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py index ae4cbf0..30755cf 100644 --- a/i3pystatus/core/modules.py +++ b/i3pystatus/core/modules.py @@ -107,7 +107,7 @@ class Module(SettingsBase): if cb is not "run": getattr(self, cb)(*args) else: - execute(cb, *args) + execute(cb, detach=True) return True def move(self, position): From d07168beb7807244ae73ce5ef4390dc907cf3da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 13:58:08 +0200 Subject: [PATCH 12/23] Module: Allow passing of arguments to callable callbacks. --- i3pystatus/core/modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py index 30755cf..0c069c4 100644 --- a/i3pystatus/core/modules.py +++ b/i3pystatus/core/modules.py @@ -102,7 +102,7 @@ class Module(SettingsBase): self.logger.debug("cb=%s args=%s" % (cb, args)) if callable(cb): - cb(self) + cb(self, *args) elif hasattr(self, cb): if cb is not "run": getattr(self, cb)(*args) From 30b73fe6d67dfb0fce6c07a8074d072c5af17255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 18:17:54 +0200 Subject: [PATCH 13/23] SettingsBase: Fixed logging for modules outside of i3pystatus directory i.e. modules defined directly in the config file. --- i3pystatus/core/settings.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/i3pystatus/core/settings.py b/i3pystatus/core/settings.py index 2ed7f2f..75095e8 100644 --- a/i3pystatus/core/settings.py +++ b/i3pystatus/core/settings.py @@ -99,7 +99,10 @@ class SettingsBase(metaclass=SettingsBaseMeta): raise ConfigMissingError( type(self).__name__, missing=exc.keys) from exc - self.logger = logging.getLogger(self.__name__) + if self.__name__.startswith("i3pystatus"): + self.logger = logging.getLogger(self.__name__) + else: + self.logger = logging.getLogger("i3pystatus." + self.__name__) self.logger.setLevel(self.log_level) self.init() From 4aa6b2a73c712188fd4acb73bf965a12d58a69ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 18:47:54 +0200 Subject: [PATCH 14/23] Docs: Updated docstring of `run_through_shell`. --- i3pystatus/core/command.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index 42d1bda..af79ce0 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -7,11 +7,21 @@ CommandResult = namedtuple("Result", ['rc', 'out', 'err']) def run_through_shell(command, enable_shell=False): """ - Retrieves output of command - Returns tuple success (boolean)/ stdout(string) / stderr (string) + Retrieve output of a command. + Returns a named tuple with three elements: - Don't use this function with programs that outputs lots of data since the output is saved - in one variable + * ``rc`` (integer) Return code of command. + * ``out`` (string) Everything that was printed to stdout. + * ``err`` (string) Everything that was printed to stderr. + + Don't use this function with programs that outputs lots of data since the + output is saved in one variable. + + :param command: A string or a list of strings containing the name and + arguments of the program. + :param enable_shell: If set ot `True` users default shell will be invoked + and given ``command`` to execute. The ``command`` should obviously be a + string since shell does all the parsing. """ if not enable_shell and not isinstance(command, list): From 6d3c7eddc8899b8dce54d41ac546db0521cca33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 19:12:05 +0200 Subject: [PATCH 15/23] Module: More detailed logs for clickevents as suggested by @teto in #231. --- i3pystatus/core/modules.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py index 0c069c4..d1fa235 100644 --- a/i3pystatus/core/modules.py +++ b/i3pystatus/core/modules.py @@ -75,6 +75,11 @@ class Module(SettingsBase): ) """ + def log_event(name, button, cb, args, action): + msg = "{}: button={}, cb='{}', args={}, type='{}'".format( + name, button, cb, args, action) + self.logger.debug(msg) + def split_callback_and_args(cb): if isinstance(cb, list): return cb[0], cb[1:] @@ -91,23 +96,25 @@ class Module(SettingsBase): elif button == 5: # mouse wheel down cb = self.on_downscroll else: - self.logger.info("Button '%d' not handled yet." % (button)) + log_event(self.__name__, button, None, None, "Unhandled button") return False if not cb: - self.logger.info("no cb attached") + log_event(self.__name__, button, None, None, "No callback attached") return False else: cb, args = split_callback_and_args(cb) - self.logger.debug("cb=%s args=%s" % (cb, args)) if callable(cb): cb(self, *args) + log_event(self.__name__, button, cb, args, "Python callback") elif hasattr(self, cb): if cb is not "run": getattr(self, cb)(*args) + log_event(self.__name__, button, cb, args, "Member callback") else: execute(cb, detach=True) + log_event(self.__name__, button, cb, args, "External command") return True def move(self, position): From 6b22ec25e469ab29f570e078be2c91875e7e5393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 19:16:04 +0200 Subject: [PATCH 16/23] Docs: Rewritten Callbacks section of user documentation. --- docs/configuration.rst | 99 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 14 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 54bd1d8..0dffa4a 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -281,23 +281,94 @@ The global default action for all settings is ``None`` (do nothing), but many modules define other defaults, which are documented in the module reference. -The settings can be of different types, namely +The values you can assign to these four settings can be divided to following +three categories: -- a list referring to a method of the module, e.g. +.. rubric:: Member callbacks - .. code:: python +These callbacks are part of the module itself and usually do some simple module +related tasks (like changing volume when scrolling, etc.). All available +callbacks are (most likely not) documented in their respective module +documentation. - status.register("clock", - on_leftclick=["scroll_format", 1]) +For example the module :py:class:`.ALSA` has callbacks named ``switch_mute``, +``increase_volume`` and ``decrease volume``. They are already assigned by +default but you can change them to your liking when registering the module. - ``scroll_format`` is a method of the ``clock`` module, the ``1`` is - passed as a parameter and indicates the direction in this case. -- as a special case of the above: a string referring to a method, no - parameters are passed. -- a list where the first element is a callable and the following - elements are passed as arguments to the callable -- again a special case of the above: just a callable, no parameters -- a string which is run in a shell +.. code:: python + + status.register("alsa", + on_leftclick = ["switch_mute"], + # or as a strings without the list + on_upscroll = "decrease_volume", + on_downscroll = "increase_volume", + # this will refresh any module by clicking on it + on_rightclick = "run", + ) + +Some callbacks also have additional parameters. Both ``increase_volume`` and +``decrease_volume`` have an optional parameter ``delta`` which determines the +amount of percent to add/subtract from the current volume. + +.. code:: python + + status.register("alsa", + # all additional items in the list are sent to the callback as arguments + on_upscroll = ["decrease_volume", 2], + on_downscroll = ["increase_volume", 2], + ) + + +.. rubric:: Python callbacks + +These refer to to any callable Python object (most likely a function). + +.. code:: python + + # Note that the 'self' parameter is required and gives access to all + # variables of the module. + def change_text(self): + self.output["full_text"] = "Clicked" + + status.register("text", + text = "Initial text", + on_leftclick = [change_text], + # or + on_rightclick = change_text, + ) + +You can also create callbacks with parameters. + +.. code:: python + + def change_text(self, text="Hello world!", color="#ffffff"): + self.output["full_text"] = text + self.output["color"] = color + + status.register("text", + text = "Initial text", + color = "#00ff00", + on_leftclick = [change_text, "Clicked LMB", "#ff0000"], + on_rightclick = [change_text, "Clicked RMB"], + on_upscroll = change_text, + ) + +.. rubric:: External program callbacks + +You can also use callbacks to execute external programs. Any string that does +not match any `member callback` is treated as an external command. If you want +to do anything more complex than executing a program with a few arguments, +consider creating an `python callback` or execute a script instead. + +.. code:: python + + status.register("text", + text = "Launcher?", + # open terminal window running htop + on_leftclick = "i3-sensible-terminal -e htop", + # open i3pystatus github page in firefox + on_rightclick = "firefox --new-window https://github.com/enkore/i3pystatus", + ) .. _hints: @@ -375,7 +446,7 @@ Refreshing the bar The whole bar can be refreshed by sending SIGUSR1 signal to i3pystatus process. This feature is available only in standalone operation (:py:class:`.Status` was -created with `standalone=True` parameter). +created with ``standalone=True`` parameter). To find the PID of the i3pystatus process look for the ``status_command`` you use in your i3 config file. From 68d11b2f81688c4742c2cde5f4bf0ce6baf2e89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Fri, 25 Sep 2015 19:37:53 +0200 Subject: [PATCH 17/23] Module: Log first then let bad things happen. :) --- i3pystatus/core/modules.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py index d1fa235..c9eaa98 100644 --- a/i3pystatus/core/modules.py +++ b/i3pystatus/core/modules.py @@ -106,15 +106,15 @@ class Module(SettingsBase): cb, args = split_callback_and_args(cb) if callable(cb): - cb(self, *args) log_event(self.__name__, button, cb, args, "Python callback") + cb(self, *args) elif hasattr(self, cb): if cb is not "run": - getattr(self, cb)(*args) log_event(self.__name__, button, cb, args, "Member callback") + getattr(self, cb)(*args) else: - execute(cb, detach=True) log_event(self.__name__, button, cb, args, "External command") + execute(cb, detach=True) return True def move(self, position): From 18062dd4162d4889608ab67416e2b434b56a609e Mon Sep 17 00:00:00 2001 From: schroeji Date: Fri, 25 Sep 2015 20:22:26 +0200 Subject: [PATCH 18/23] Moved the docstring and added usage examples. --- i3pystatus/updates/yaourt.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/i3pystatus/updates/yaourt.py b/i3pystatus/updates/yaourt.py index 4164731..699c466 100644 --- a/i3pystatus/updates/yaourt.py +++ b/i3pystatus/updates/yaourt.py @@ -1,15 +1,25 @@ -""" -This module counts the available updates using yaourt. -By default it will only count aur packages. -If you want to count both pacman and aur packages set the variable -count_only_aur = False -""" import re from i3pystatus.core.command import run_through_shell from i3pystatus.updates import Backend class Yaourt(Backend): + """ + This module counts the available updates using yaourt. + By default it will only count aur packages. Thus it can be used with the pacman backend like this: + + from i3pystatus.updates import pacman, yaourt + status.register("updates", + backends = [pacman.Pacman(), yaourt.Yaourt()]) + + If you want to count both pacman and aur packages with this module you can set the variable + count_only_aur = False like this: + + from i3pystatus.updates import yaourt + status.register("updates", + backends = [yaourt.Yaourt(False)]) + """ + def __init__(self, aur_only=True): self.aur_only = aur_only From e70a199d69cbbdd53f46ff6014b5368a6c7e4472 Mon Sep 17 00:00:00 2001 From: schroeji Date: Fri, 25 Sep 2015 23:06:38 +0200 Subject: [PATCH 19/23] Removed indentation in docstring. --- i3pystatus/updates/yaourt.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/i3pystatus/updates/yaourt.py b/i3pystatus/updates/yaourt.py index 699c466..3738ff6 100644 --- a/i3pystatus/updates/yaourt.py +++ b/i3pystatus/updates/yaourt.py @@ -9,15 +9,13 @@ class Yaourt(Backend): By default it will only count aur packages. Thus it can be used with the pacman backend like this: from i3pystatus.updates import pacman, yaourt - status.register("updates", - backends = [pacman.Pacman(), yaourt.Yaourt()]) + status.register("updates", backends = [pacman.Pacman(), yaourt.Yaourt()]) If you want to count both pacman and aur packages with this module you can set the variable count_only_aur = False like this: from i3pystatus.updates import yaourt - status.register("updates", - backends = [yaourt.Yaourt(False)]) + status.register("updates", backends = [yaourt.Yaourt(False)]) """ def __init__(self, aur_only=True): From 2ef74ded7999148b93cb29ec09ee189d86211ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Tue, 29 Sep 2015 12:47:32 +0200 Subject: [PATCH 20/23] Replace regular 'split' to 'shlex.split'. --- i3pystatus/core/command.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index af79ce0..57915ba 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -1,6 +1,7 @@ import logging -from collections import namedtuple +import shlex import subprocess +from collections import namedtuple CommandResult = namedtuple("Result", ['rc', 'out', 'err']) @@ -25,7 +26,7 @@ def run_through_shell(command, enable_shell=False): """ if not enable_shell and not isinstance(command, list): - command = command.split() + command = shlex.split(command) returncode = None stderr = None @@ -67,7 +68,7 @@ def execute(command, detach=False): command = ["i3-msg", "exec", command] else: if not isinstance(command, list): - command = command.split() + command = shlex.split(command) try: subprocess.Popen(command, stdin=subprocess.DEVNULL, From 081bd329f981f618160a9ff6d20af12935b21cb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Sat, 3 Oct 2015 10:14:29 +0200 Subject: [PATCH 21/23] Changed checks for `command` attribute in `run_through_shell` and `execute`. --- i3pystatus/core/command.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index 57915ba..e6dfcc4 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -25,13 +25,14 @@ def run_through_shell(command, enable_shell=False): string since shell does all the parsing. """ - if not enable_shell and not isinstance(command, list): + if not enable_shell and isinstance(command, str): command = shlex.split(command) returncode = None stderr = None try: - proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=enable_shell) + proc = subprocess.Popen(command, stderr=subprocess.PIPE, + stdout=subprocess.PIPE, shell=enable_shell) out, stderr = proc.communicate() out = out.decode("UTF-8") stderr = stderr.decode("UTF-8") @@ -64,10 +65,13 @@ def execute(command, detach=False): if detach: if not isinstance(command, str): - raise TypeError("Detached mode expects a string as command.") + msg = "Detached mode expects a string as command, not {}".format( + command) + logging.getLogger("i3pystatus.core.command").error(msg) + raise AttributeError(msg) command = ["i3-msg", "exec", command] else: - if not isinstance(command, list): + if isinstance(command, str): command = shlex.split(command) try: From b0e914d4e99643cd50414a9b9098ca7c50003388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Sat, 3 Oct 2015 11:20:16 +0200 Subject: [PATCH 22/23] Module: Updated docstring for `on_click` method. --- i3pystatus/core/modules.py | 52 ++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/i3pystatus/core/modules.py b/i3pystatus/core/modules.py index c9eaa98..a0e1b56 100644 --- a/i3pystatus/core/modules.py +++ b/i3pystatus/core/modules.py @@ -48,31 +48,39 @@ class Module(SettingsBase): def on_click(self, button): """ - Maps a click event (include mousewheel events) with its associated callback. - It then triggers the callback depending on the nature (ie type) of - the callback variable: - 1. if null callback, do nothing - 2. if it's a python function () - 3. if it's the name of a method of the current module (string) + Maps a click event with its associated callback. - To setup the callbacks, you can set the settings 'on_leftclick', - 'on_rightclick', 'on_upscroll', 'on_downscroll'. + Currently implemented events are: - For instance, you can test with: - :: + =========== ================ ========= + Event Callback setting Button ID + =========== ================ ========= + Left click on_leftclick 1 + Right click on_rightclick 3 + Scroll up on_upscroll 4 + Scroll down on_downscroll 5 + =========== ================ ========= + + The action is determined by the nature (type and value) of the callback + setting in the following order: + + 1. If null callback (``None``), no action is taken. + 2. If it's a `python function`, call it and pass any additional + arguments. + 3. If it's name of a `member method` of current module (string), call it + and pass any additional arguments. + 4. If the name does not match with `member method` name execute program + with such name. + + .. seealso:: :ref:`callbacks` for more information about + callback settings and examples. + + :param button: The ID of button event received from i3bar. + :type button: int + :return: Returns ``True`` if a valid callback action was executed. + ``False`` otherwise. + :rtype: bool - status.register("clock", - format=[ - ("Format 0",'Europe/London'), - ("%a %-d Format 1",'Europe/Dublin'), - "%a %-d %b %X format 2", - ("%a %-d %b %X format 3", 'Europe/Paris'), - ], - on_leftclick= ["urxvtc"] , # launch urxvtc on left click - on_rightclick= ["scroll_format", 2] , # update format by steps of 2 - on_upscroll= [print, "hello world"] , # call python function print - log_level=logging.DEBUG, - ) """ def log_event(name, button, cb, args, action): From f696cb5989399087a4046b7ee6afac0ea9bc855f Mon Sep 17 00:00:00 2001 From: Niclas Eriksen Date: Thu, 15 Oct 2015 12:13:34 +0200 Subject: [PATCH 23/23] Can now fetch link and comment karma of user Link and comment karma of user can be fetched with {link_karma} and {comment_karma} if username is set. --- i3pystatus/reddit.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/i3pystatus/reddit.py b/i3pystatus/reddit.py index 3535fbd..b2889af 100644 --- a/i3pystatus/reddit.py +++ b/i3pystatus/reddit.py @@ -28,6 +28,8 @@ class Reddit(IntervalModule): * {message_author} * {message_subject} * {message_body} + * {link_karma} + * {comment_karma} """ @@ -131,6 +133,14 @@ class Reddit(IntervalModule): title = fdict["submission_title"][:(self.title_maxlen - 3)] + "..." fdict["submission_title"] = title + if self.username: + u = r.get_redditor(self.username) + fdict["link_karma"] = u.link_karma + fdict["comment_karma"] = u.comment_karma + else: + fdict["link_karma"] = "" + fdict["comment_karma"] = "" + full_text = self.format.format(**fdict) self.output = { "full_text": full_text,