From 31f9154be2f4f2d49713fb7e065f5e3844870e0a Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sat, 17 Dec 2016 07:05:23 +0100 Subject: [PATCH] [help] Add theme and module listing Re-enable the possibility to list available themes and modules. fixes #29 --- bumblebee/config.py | 33 ++++++++++++++++++++++ bumblebee/modules/test.py | 3 +- bumblebee/theme.py | 9 ++++++ tests/test_theme.py | 2 +- themes/{invalid.json => test_invalid.json} | 0 5 files changed, 45 insertions(+), 2 deletions(-) rename themes/{invalid.json => test_invalid.json} (100%) diff --git a/bumblebee/config.py b/bumblebee/config.py index b1b15ed..92472e5 100644 --- a/bumblebee/config.py +++ b/bumblebee/config.py @@ -5,11 +5,42 @@ module parameters, etc.) to all other components """ import argparse +import textwrap +import importlib import bumblebee.store MODULE_HELP = "Specify a space-separated list of modules to load. The order of the list determines their order in the i3bar (from left to right). Use : to provide an alias in case you want to load the same module multiple times, but specify different parameters." THEME_HELP = "Specify the theme to use for drawing modules" PARAMETER_HELP = "Provide configuration parameters in the form of .=" +LIST_HELP = "Display a list of either available themes or available modules along with their parameters." + +class print_usage(argparse.Action): + def __init__(self, option_strings, dest, nargs=None, **kwargs): + argparse.Action.__init__(self, option_strings, dest, nargs, **kwargs) + self._indent = " "*2 + + def __call__(self, parser, namespace, value, option_string=None): + if value == "modules": + self.print_modules() + elif value == "themes": + self.print_themes() + else: + parser.print_help() + parser.exit() + + def print_themes(self): + print(textwrap.fill(", ".join(bumblebee.theme.themes()), + 80, initial_indent = self._indent, subsequent_indent = self._indent + )) + + def print_modules(self): + for m in bumblebee.engine.all_modules(): + mod = importlib.import_module("bumblebee.modules.{}".format(m["name"])) + print(textwrap.fill("{}:".format(m["name"]), 80, + initial_indent=self._indent*2, subsequent_indent=self._indent*2)) + for line in mod.__doc__.split("\n"): + print(textwrap.fill(line, 80, + initial_indent=self._indent*3, subsequent_indent=self._indent*6)) def create_parser(): """Create the argument parser""" @@ -19,6 +50,8 @@ def create_parser(): parser.add_argument("-t", "--theme", default="default", help=THEME_HELP) parser.add_argument("-p", "--parameters", nargs="+", default=[], help=PARAMETER_HELP) + parser.add_argument("-l", "--list", choices=["modules", "themes"], action=print_usage, + help=LIST_HELP) return parser class Config(bumblebee.store.Store): diff --git a/bumblebee/modules/test.py b/bumblebee/modules/test.py index 9f7485f..5e92e0e 100644 --- a/bumblebee/modules/test.py +++ b/bumblebee/modules/test.py @@ -1,6 +1,7 @@ # pylint: disable=C0111,R0903 -"""Test module""" +"""Test module +""" import bumblebee.engine diff --git a/bumblebee/theme.py b/bumblebee/theme.py index 3486f70..779155b 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -3,6 +3,7 @@ """Theme support""" import os +import glob import copy import json @@ -12,6 +13,14 @@ def theme_path(): """Return the path of the theme directory""" return os.path.dirname("{}/../themes/".format(os.path.dirname(os.path.realpath(__file__)))) +def themes(): + result = [] + + for filename in glob.iglob("{}/*.json".format(theme_path())): + if "test" not in filename: + result.append(os.path.basename(filename).replace(".json", "")) + return result + class Theme(object): """Represents a collection of icons and colors""" def __init__(self, name): diff --git a/tests/test_theme.py b/tests/test_theme.py index 8d9dccc..ef356ca 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -8,7 +8,7 @@ from tests.util import MockWidget class TestTheme(unittest.TestCase): def setUp(self): self.nonexistentThemeName = "no-such-theme" - self.invalidThemeName = "invalid" + self.invalidThemeName = "test_invalid" self.validThemeName = "test" self.themedWidget = MockWidget("bla") self.theme = Theme(self.validThemeName) diff --git a/themes/invalid.json b/themes/test_invalid.json similarity index 100% rename from themes/invalid.json rename to themes/test_invalid.json