From d30192cec9855efb4b0ad1a13b94683642613156 Mon Sep 17 00:00:00 2001 From: jurialmunkey Date: Sun, 6 Aug 2023 14:48:36 +1000 Subject: [PATCH] :sparkles: XML generator from templates --- resources/lib/script.py | 10 ++++-- resources/lib/skinshortcuts.py | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 resources/lib/skinshortcuts.py diff --git a/resources/lib/script.py b/resources/lib/script.py index ae025bb..7eb2875 100644 --- a/resources/lib/script.py +++ b/resources/lib/script.py @@ -45,6 +45,10 @@ def router(self): if self.params.get('action') == 'buildviews': from resources.lib.viewtypes import ViewTypes return ViewTypes().update_xml(skinfolder=self.params.get('folder'), **self.params) - else: - from resources.lib.skinvariables import SkinVariables - return SkinVariables(template=self.params.get('template'), skinfolder=self.params.get('folder')).update_xml(**self.params) + + if self.params.get('action') == 'buildtemplate': + from resources.lib.skinshortcuts import SkinShortcutsTemplate + return SkinShortcutsTemplate(template=self.params.get('template')).update_xml(**self.params) + + from resources.lib.skinvariables import SkinVariables + return SkinVariables(template=self.params.get('template'), skinfolder=self.params.get('folder')).update_xml(**self.params) diff --git a/resources/lib/skinshortcuts.py b/resources/lib/skinshortcuts.py new file mode 100644 index 0000000..efc671b --- /dev/null +++ b/resources/lib/skinshortcuts.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Module: default +# Author: jurialmunkey +# License: GPL v.3 https://www.gnu.org/copyleft/gpl.html +import xbmc +import xbmcgui +import xbmcaddon +from json import loads +from jurialmunkey.futils import load_filecontent, write_skinfile, make_hash + +ADDON = xbmcaddon.Addon() + + +class SkinShortcutsTemplate(object): + def __init__(self, template: str = None): + self.template = f'skinvariables-generator-{template}' if template else 'skinvariables-generator' + self.hashname = f'script-{self.template}-hash' + self.folders = ['shortcuts'] + self.content = load_filecontent(f'special://skin/shortcuts/{self.template}.json') + self.meta = loads(self.content) or {} + self.filename = self.meta['output'] + + @staticmethod + def create_xml(meta, header=None, footer=None): + + def _make_template(i): + template = load_filecontent(f'special://skin/shortcuts/{i.pop("template")}') + template = template.format(**i) + return template + + cheader = [header] if header else [] + cfooter = [footer] if footer else [] + content = cheader + [_make_template(i) for i in meta] + cfooter + + return '\n'.join(content) + + def update_xml(self, force=False, no_reload=False, **kwargs): + if not self.meta: + return + + hashvalue = make_hash(self.content) + + if not force: # Allow overriding over built check + last_version = xbmc.getInfoLabel(f'Skin.String({self.hashname})') + if hashvalue and last_version and hashvalue == last_version: + return # Already updated + + p_dialog = xbmcgui.DialogProgressBG() + p_dialog.create(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32000)) + + content = self.create_xml(self.meta['genxml'], header=self.meta.get('header'), footer=self.meta.get('footer')) + + # Save to folder + write_skinfile(folders=self.folders, filename=self.filename, content=content, hashvalue=hashvalue, hashname=self.hashname) + + p_dialog.close() + xbmc.executebuiltin('ReloadSkin()') if not no_reload else None