[modules] Refactor module initialization

Modules now get an output and a complete config object. This should make
customization much easier in the future.
This commit is contained in:
Tobias Witek 2016-11-05 08:11:08 +01:00
parent c2adf38b92
commit 7f91b8298f
5 changed files with 46 additions and 31 deletions

View file

@ -8,7 +8,7 @@ import bumblebee.module
class Config(object): class Config(object):
def __init__(self, args): def __init__(self, args):
self._raw = args self._raw = args
self._parser = self.parser() self._parser = self._parser()
self._indent = " "*4 self._indent = " "*4
if len(args) == 0: if len(args) == 0:
@ -24,21 +24,15 @@ class Config(object):
if self._args.list: if self._args.list:
self._parser.exit() self._parser.exit()
def parser(self): def parameter(self, name, default):
parser = argparse.ArgumentParser(description="display system data in the i3bar") # TODO
parser.add_argument("-m", "--modules", nargs="+", return default
help="List of modules to load. The order of the list determines "
"their order in the i3bar (from left to right)",
default=[])
parser.add_argument("-l", "--list",
help="List: 'modules', 'themes' ",
choices = [ "modules", "themes" ],
default="modules")
parser.add_argument("-t", "--theme", help="Specify which theme to use for "
"drawing the modules",
default="default")
return parser def theme(self):
return self._args.theme
def modules(self):
return self._args.modules
def print_themes(self): def print_themes(self):
print(textwrap.fill(", ".join(bumblebee.theme.themes()), print(textwrap.fill(", ".join(bumblebee.theme.themes()),
@ -57,4 +51,23 @@ class Config(object):
80, initial_indent=self._indent*2, subsequent_indent=self._indent*4) 80, initial_indent=self._indent*2, subsequent_indent=self._indent*4)
print "" print ""
def _parser(self):
parser = argparse.ArgumentParser(description="display system data in the i3bar")
parser.add_argument("-m", "--modules", nargs="+",
help="List of modules to load. The order of the list determines "
"their order in the i3bar (from left to right)",
default=[],
)
parser.add_argument("-l", "--list",
help="List: 'modules', 'themes' ",
choices = [ "modules", "themes" ],
)
parser.add_argument("-t", "--theme", help="Specify which theme to use for "
"drawing the modules",
default="default",
)
return parser
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -10,23 +10,23 @@ import bumblebee.output
import bumblebee.modules import bumblebee.modules
class Engine: class Engine:
def __init__(self, args): def __init__(self, config):
self._modules = [] self._modules = []
self._args = args self._config = config
self._theme = bumblebee.theme.Theme(args) self._theme = bumblebee.theme.Theme(config)
self._output = bumblebee.output.output(args) self._output = bumblebee.output.output(config)
def load_module(self, modulespec): def load_module(self, name):
name = modulespec.split(self._args.split)[0]
args = None if name == modulespec else modulespec.split(self._args.split)[1:]
module = importlib.import_module("bumblebee.modules.{}".format(name)) module = importlib.import_module("bumblebee.modules.{}".format(name))
return getattr(module, "Module")(self._output, args) return getattr(module, "Module")(self._output, self._config)
def load_modules(self): def load_modules(self):
for m in self._args.modules: for m in self._config.modules():
self._modules.append(self.load_module(m)) self._modules.append(self.load_module(m))
def register_event(self, eventspec): def register_event(self, eventspec):
return
# TODO
event = eventspec.split(self._args.split) event = eventspec.split(self._args.split)
if len(event) < 3: if len(event) < 3:
raise Exception("invalid click event format, expected 3 parameters") raise Exception("invalid click event format, expected 3 parameters")
@ -37,6 +37,8 @@ class Engine:
) )
def register_events(self): def register_events(self):
return
# TODO
for e in self._args.events: for e in self._args.events:
self.register_event(e) self.register_event(e)

View file

@ -32,7 +32,7 @@ class ModuleDescription(object):
return "n/a" return "n/a"
class Module(object): class Module(object):
def __init__(self, args): def __init__(self, output, config):
pass pass
def data(self): def data(self):

View file

@ -18,8 +18,8 @@ def description():
return "Displays the current time, using the optional format string as input for strftime." return "Displays the current time, using the optional format string as input for strftime."
class Module(bumblebee.module.Module): class Module(bumblebee.module.Module):
def __init__(self, output, args): def __init__(self, output, config):
super(Module, self).__init__(args) super(Module, self).__init__(output, config)
module = self.__module__.split(".")[-1] module = self.__module__.split(".")[-1]
default = "%x %X" default = "%x %X"
@ -28,8 +28,8 @@ class Module(bumblebee.module.Module):
if module == "time": if module == "time":
default = "%X" default = "%X"
self._fmt = args[0] if args else default param_name = "{}.format".format(module)
self._fmt = config.parameter(param_name, default)
def data(self): def data(self):
return datetime.datetime.now().strftime(self._fmt) return datetime.datetime.now().strftime(self._fmt)

View file

@ -12,10 +12,10 @@ def themes():
class Theme: class Theme:
_cycle_index = 0 _cycle_index = 0
_cycle = None _cycle = None
def __init__(self, args): def __init__(self, config):
self._data = None self._data = None
path = os.path.dirname(os.path.realpath(__file__)) path = os.path.dirname(os.path.realpath(__file__))
with open("{}/{}.json".format(getpath(), args.theme)) as f: with open("{}/{}.json".format(getpath(), config.theme())) as f:
self._data = json.load(f) self._data = json.load(f)
self._defaults = self._data.get("defaults", {}) self._defaults = self._data.get("defaults", {})
self._cycle = self._defaults.get("cycle", []) self._cycle = self._defaults.get("cycle", [])