[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):
def __init__(self, args):
self._raw = args
self._parser = self.parser()
self._parser = self._parser()
self._indent = " "*4
if len(args) == 0:
@ -24,21 +24,15 @@ class Config(object):
if self._args.list:
self._parser.exit()
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" ],
default="modules")
parser.add_argument("-t", "--theme", help="Specify which theme to use for "
"drawing the modules",
default="default")
def parameter(self, name, default):
# TODO
return default
return parser
def theme(self):
return self._args.theme
def modules(self):
return self._args.modules
def print_themes(self):
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)
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

View file

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

View file

@ -32,7 +32,7 @@ class ModuleDescription(object):
return "n/a"
class Module(object):
def __init__(self, args):
def __init__(self, output, config):
pass
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."
class Module(bumblebee.module.Module):
def __init__(self, output, args):
super(Module, self).__init__(args)
def __init__(self, output, config):
super(Module, self).__init__(output, config)
module = self.__module__.split(".")[-1]
default = "%x %X"
@ -28,8 +28,8 @@ class Module(bumblebee.module.Module):
if module == "time":
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):
return datetime.datetime.now().strftime(self._fmt)

View file

@ -12,10 +12,10 @@ def themes():
class Theme:
_cycle_index = 0
_cycle = None
def __init__(self, args):
def __init__(self, config):
self._data = None
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._defaults = self._data.get("defaults", {})
self._cycle = self._defaults.get("cycle", [])