diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2020-04-22 10:03:04 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2020-04-22 10:03:04 +0200 |
| commit | 586e35e3906214fc964714dd60091b290400ba1d (patch) | |
| tree | 34fcc2b46d8a95abadd454773edcc94c48df6ffc /bcst/theme.py | |
| parent | 24ef10da650d091e67bd82ec3f9b329bcee1a15e (diff) | |
Clean theme and improve api
Diffstat (limited to 'bcst/theme.py')
| -rw-r--r-- | bcst/theme.py | 73 |
1 files changed, 51 insertions, 22 deletions
diff --git a/bcst/theme.py b/bcst/theme.py index 9bdd956..93efed2 100644 --- a/bcst/theme.py +++ b/bcst/theme.py @@ -1,18 +1,18 @@ #!/usr/bin/env python -from bcst.resource import Resource + from shutil import copytree, ignore_patterns from jinja2 import Template -import os -from os import path +import json +from os import path, listdir themes_location=path.join(path.dirname(path.abspath(__file__)),"themes") def list_themes(): themes=list() - for f in os.listdir(themes_location): - if(not(os.path.isfile(os.path.join(themes_location,f)))): + for f in listdir(themes_location): + if(not(path.isfile(path.join(themes_location,f)))): themes.append(f) return(themes) @@ -24,15 +24,52 @@ def get_theme_path(name): print("Could not find theme: "+name) exit(1) -class Theme: +class Resource: + """ + Load a resource file. + - path: Contains the resources location + - data: Contains the loaded (from json) resource data + - content: Contains the plain text data of the resource file + """ + def __init__(self, resource_path): + self.path=resource_path + # Read data + try: + with open(resource_path,'r') as resFile: + self.content=resFile.read() + except: + self.error("unable to read "+resource_path) + # Decode data + try: + self.data=json.loads(self.content) + except: + self.error("unable to load json from "+resource_path) + + def error(self, msg): + """ + Raise error and exit. + """ + print("In Resource ==> "+msg) + exit(1) + + def update_data(self, new_data): + """ + Update current resource data. + """ + self.data.update(new_data) + + +class Theme: + """ + Load a theme. + """ def __init__(self, name): - self.theme_path=get_theme_path(name) - self.res_path=self.theme_path+"/resources.json" - self.data=Resource(self.res_path).json + self.path=get_theme_path(name) + self.resource=Resource(self.path+"/resources.json") # Read theme try: - with open(self.theme_path+"/index.html",'r') as f: + with open(self.path+"/index.html",'r') as f: self.template=Template(f.read()) except IOError: print("Unable to found "+resource) @@ -40,17 +77,9 @@ class Theme: def update_resource(self,resource_path): r=Resource(resource_path) - self.data.update(r.json) - + self.resource.update_data(r.data) - def extract(self, dest): - with open(dest, "w") as resFile: - resFile.write(self.data.data) - - - def deploy(self, dest_path): - copytree(self.theme_path, dest_path, dirs_exist_ok=True,ignore=ignore_patterns("*.json","index.html")) - themes_dir=os.path.split(self.theme_path)[0] - theme_dir=os.path.split(self.theme_path)[1] + def generate(self, dest_path): + copytree(self.path, dest_path, dirs_exist_ok=True,ignore=ignore_patterns("*.json","index.html")) with open(dest_path+"/index.html", "w") as index: - index.write(self.template.render(self.data)) + index.write(self.template.render(self.resource.data)) |
