Skip to content

Commit

Permalink
Merge branch 'theme-directories'
Browse files Browse the repository at this point in the history
fixes #203
  • Loading branch information
tobi-wan-kenobi committed Nov 26, 2017
2 parents f353bf6 + 3fe2088 commit 197c4c9
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions bumblebee/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@

def theme_path():
"""Return the path of the theme directory"""
return os.path.dirname("{}/../themes/".format(os.path.dirname(os.path.realpath(__file__))))
return [
os.path.dirname("{}/../themes/".format(os.path.dirname(os.path.realpath(__file__)))),
os.path.dirname(os.path.expanduser("~/.config/bumblebee-status/themes/")),
]

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", ""))
themes = {}

for path in theme_path():
for filename in glob.iglob("{}/*.json".format(path)):
if "test" not in filename:
themes[os.path.basename(filename).replace(".json", "")] = 1
result = themes.keys()
result.sort()
return result

class Theme(object):
Expand All @@ -30,7 +36,10 @@ def __init__(self, name):
self._cycle = {}
self._prevbg = None
self._colorset = {}
self._init(self.load(name))
data = self.load(name)
if not data:
raise bumblebee.error.ThemeLoadError("no such theme")
self._init(data)

def _init(self, data):
"""Initialize theme from data structure"""
Expand Down Expand Up @@ -119,21 +128,30 @@ def _load_colors(self, name):

def _load_icons(self, name):
"""Load icons for a theme"""
path = "{}/icons/".format(theme_path())
return self.load(name, path=path)
result = {}
for path in theme_path():
self._merge(result, self.load(name, path="{}/icons/".format(path)))
return result

def load(self, name, path=theme_path()):
"""Load and parse a theme file"""
themefile = "{}/{}.json".format(path, name)

if os.path.isfile(themefile):
try:
with io.open(themefile, encoding="utf-8") 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))
result = None
if not isinstance(path, list):
path = [path]
for p in path:
themefile = "{}/{}.json".format(p, name)

if os.path.isfile(themefile):
try:
with io.open(themefile, encoding="utf-8") as data:
if result is None:
result = json.load(data)
else:
self._merge(result, json.load(data))
except ValueError as exception:
raise bumblebee.error.ThemeLoadError("JSON error: {}".format(exception))

return result

def _get(self, widget, name, default=None):
"""Return the config value 'name' for 'widget'"""
Expand Down

0 comments on commit 197c4c9

Please sign in to comment.