diff options
| -rwxr-xr-x | py3status/__init__.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/py3status/__init__.py b/py3status/__init__.py index 024451e..2027882 100755 --- a/py3status/__init__.py +++ b/py3status/__init__.py @@ -1,4 +1,7 @@ +from __future__ import print_function + import argparse +import ast import imp import locale import os @@ -40,6 +43,12 @@ def print_line(line): sys.__stdout__.flush() +def print_stderr(line): + """Print line to stderr + """ + print(line, file=sys.stderr) + + class IOPoller: """ This class implements a predictive and timing-out I/O reader @@ -1171,8 +1180,19 @@ class Py3statusWrapper(): (default 60 sec)""") parser.add_argument('-v', '--version', action="store_true", help="""show py3status version and exit""") + + parser.add_argument('cli_command', nargs='?', + choices=[ + 'list-modules', + ]) + parser.add_argument('cli_command_arg', nargs='?', help=argparse.SUPPRESS) + options = parser.parse_args() + if options.cli_command: + config['cli_command'] = options.cli_command + config['cli_command_arg'] = options.cli_command_arg + # only asked for version if options.version: from platform import python_version @@ -1276,6 +1296,14 @@ class Py3statusWrapper(): # setup configuration self.config = self.get_config() + + if 'cli_command' in self.config: + self.handle_cli_command( + self.config['cli_command'], + self.config['cli_command_arg'] + ) + sys.exit() + if self.config['debug']: syslog( LOG_INFO, @@ -1571,6 +1599,47 @@ class Py3statusWrapper(): delta += 0.1 sleep(0.1) + @staticmethod + def print_module_description(mod_name, mod_path): + """Print module description extracted from its docstring. + """ + if mod_name == '__init__': + return + + path = os.path.join(*mod_path) + try: + with open(path) as f: + module = ast.parse(f.read()) + + items = [i for i in module.body if isinstance(i, ast.Expr)] + if items: + ds = items[0].value.s + ds = ds.lstrip() + ds = ds.split('\n\n',1)[0] # Filter only up to an empty line. + ds = ds.split('.', 1)[0] # And the first dot + ds = ds.replace('\n', ' ') + print_stderr(" %-22s %s." % (mod_name, ds)) + + else: + print_stderr(" %-22s No docstring in %s" % (mod_name, path)) + + except Exception as e: + print_stderr(" %-22s Unable to parse %s" % (mod_name, path)) + + def handle_cli_command(self, cmd, cmd_arg): + """Handle a command from the CLI. + """ + if cmd == 'list-modules': + user_modules = self.get_user_modules() + print_stderr("Available modules:") + for mod_name, mod_path in sorted(user_modules.items()): + self.print_module_description(mod_name, mod_path) + + sys.exit() + + print_stderr("Error: unknown command") + sys.exit(1) + def main(): try: |
