From f40418475f31653491c2006c834d7e5a5e89652e Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 07:11:23 +0100 Subject: [PATCH] [modules/datetime] Re-enable datetime module Add datetime module + aliases date and time. see #23 --- bumblebee/engine.py | 4 ++++ bumblebee/modules/date.py | 1 + bumblebee/modules/datetime.py | 31 +++++++++++++++++++++++++++++++ bumblebee/modules/time.py | 1 + bumblebee/output.py | 5 +++++ bumblebee/theme.py | 5 ++++- tests/test_i3baroutput.py | 8 +++++--- 7 files changed, 51 insertions(+), 4 deletions(-) create mode 120000 bumblebee/modules/date.py create mode 100644 bumblebee/modules/datetime.py create mode 120000 bumblebee/modules/time.py diff --git a/bumblebee/engine.py b/bumblebee/engine.py index d87e66f..c5b6266 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -22,6 +22,10 @@ class Module(object): """Return the widgets to draw for this module""" return self._widgets + def update(self, widgets): + """By default, update() is a NOP""" + pass + class Engine(object): """Engine for driving the application diff --git a/bumblebee/modules/date.py b/bumblebee/modules/date.py new file mode 120000 index 0000000..bde6404 --- /dev/null +++ b/bumblebee/modules/date.py @@ -0,0 +1 @@ +datetime.py \ No newline at end of file diff --git a/bumblebee/modules/datetime.py b/bumblebee/modules/datetime.py new file mode 100644 index 0000000..fc11a89 --- /dev/null +++ b/bumblebee/modules/datetime.py @@ -0,0 +1,31 @@ +# pylint: disable=C0111,R0903 + +"""Displays the current time, using the optional format string as input for strftime.""" + +from __future__ import absolute_import +import datetime +import bumblebee.engine + +def default_format(module): + default = "%x %X" + if module == "date": + default = "%x" + if module == "time": + default = "%X" + return default + +class Module(bumblebee.engine.Module): + def __init__(self, engine): + super(Module, self).__init__(engine, + bumblebee.output.Widget(full_text=self.get_time) + ) + module = self.__module__.split(".")[-1] + + self._fmt = default_format(module) + +# self._fmt = self._config.parameter("format", default_format(module)) + + def get_time(self): + return datetime.datetime.now().strftime(self._fmt) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/time.py b/bumblebee/modules/time.py new file mode 120000 index 0000000..bde6404 --- /dev/null +++ b/bumblebee/modules/time.py @@ -0,0 +1 @@ +datetime.py \ No newline at end of file diff --git a/bumblebee/output.py b/bumblebee/output.py index 13b35c6..92bad91 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -12,9 +12,14 @@ class Widget(object): self._module = None def set_module(self, module): + """Set the module that spawned this widget + + This is done outside the constructor to avoid having to + pass in the module name in every concrete module implementation""" self._module = module.name def module(self): + """Return the name of the module that spawned this widget""" return self._module def full_text(self): diff --git a/bumblebee/theme.py b/bumblebee/theme.py index 380b20e..2498655 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -31,10 +31,12 @@ class Theme(object): return self._get(widget, "suffix", None) def loads(self, data): + """Initialize the theme from a JSON string""" theme = json.loads(data) self._init(theme) def _load_icons(self, name): + """Load icons for a theme""" path = "{}/icons/".format(theme_path()) return self.load(name, path=path) @@ -52,7 +54,7 @@ class Theme(object): raise bumblebee.error.ThemeLoadError("no such theme: {}".format(name)) def _get(self, widget, name, default=None): - + """Return the config value 'name' for 'widget'""" module_theme = self._theme.get(widget.module(), {}) padding = None @@ -71,6 +73,7 @@ class Theme(object): # http://blog.impressiver.com/post/31434674390/deep-merge-multiple-python-dicts # nicely done :) def _merge(self, target, *args): + """Merge two arbitrarily nested data structures""" if len(args) > 1: for item in args: self._merge(item) diff --git a/tests/test_i3baroutput.py b/tests/test_i3baroutput.py index 55d6e10..dbf2feb 100644 --- a/tests/test_i3baroutput.py +++ b/tests/test_i3baroutput.py @@ -44,7 +44,7 @@ class TestI3BarOutput(unittest.TestCase): self.output.flush() result = json.loads(stdout.getvalue()) for res in result: - self.assertEquals(res["full_text"], widget.full_text()) + self.assertEquals(res["full_text"], self.someWidget.full_text()) @mock.patch("sys.stdout", new_callable=StringIO) def test_begin(self, stdout): @@ -84,7 +84,9 @@ class TestI3BarOutput(unittest.TestCase): self.output.flush() result = json.loads(stdout.getvalue())[0] self.assertEquals(result["full_text"], "{}{}{}".format( - self.theme.prefix(self.someWidget), self.someWidget.full_text(), self.theme.suffix(self.someWidget)) - ) + self.theme.prefix(self.someWidget), + self.someWidget.full_text(), + self.theme.suffix(self.someWidget) + )) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4