diff --git a/bumblebee/error.py b/bumblebee/error.py index b07cc24..129f02d 100644 --- a/bumblebee/error.py +++ b/bumblebee/error.py @@ -8,4 +8,8 @@ class ModuleLoadError(BaseError): """Raised whenever loading a module fails""" pass +class ThemeLoadError(BaseError): + """Raised whenever loading a theme fails""" + pass + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/theme.py b/bumblebee/theme.py index e65bc41..e07c1b8 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -1,7 +1,30 @@ """Theme support""" +import os +import json + +import bumblebee.error + +def theme_path(): + """Return the path of the theme directory""" + return os.path.dirname("{}/../themes/".format(os.path.dirname(os.path.realpath(__file__)))) + class Theme(object): """Represents a collection of icons and colors""" - pass + def __init__(self, name): + self._theme = self.load(name) + + def load(self, name): + """Load and parse a theme file""" + path = theme_path() + themefile = "{}/{}.json".format(path, name) + if os.path.isfile(themefile): + try: + with open(themefile) as data: + return json.load(data) + except ValueError as exception: + raise bumblebee.error.ThemeLoadError("JSON error: {}".format(exception)) + else: + raise bumblebee.error.ThemeLoadError("no such theme: {}".format(name)) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_theme.py b/tests/test_theme.py index 65014f9..746f95b 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -1,3 +1,25 @@ -# pylint: disable=C0103,C0111 +# pylint: disable=C0103,C0111,W0703 + +import unittest +from bumblebee.theme import Theme +from bumblebee.error import ThemeLoadError + +class TestTheme(unittest.TestCase): + def setUp(self): + pass + + def test_load_valid_theme(self): + try: + Theme("solarized-powerline") + except Exception as e: + self.fail(e) + + def test_load_nonexistent_theme(self): + with self.assertRaises(ThemeLoadError): + Theme("no-such-theme") + + def test_load_invalid_theme(self): + with self.assertRaises(ThemeLoadError): + Theme("invalid") # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/invalid.json b/themes/invalid.json new file mode 100644 index 0000000..d510f27 --- /dev/null +++ b/themes/invalid.json @@ -0,0 +1 @@ +this is really not json