Mercurial > libervia-web
changeset 1268:e628724530ec
pages, tasks (brython): use set for scripts + common template data:
- scripts are put in a set using a namedtuple, this way duplicates can be avoided easily
- if `template` is set in dyn_data_common, items there are set in template data if they
don't already exist
- (brython): common template data is used to set body_onload when there is a root brython
script and no page script
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 19 May 2020 11:05:40 +0200 |
parents | b5f920845d34 |
children | 470c6cfdf4ce |
files | libervia/server/classes.py libervia/server/pages.py libervia/server/server.py libervia/server/tasks/implicit/task_brython.py |
diffstat | 4 files changed, 38 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/libervia/server/classes.py Tue May 19 11:02:33 2020 +0200 +++ b/libervia/server/classes.py Tue May 19 11:05:40 2020 +0200 @@ -24,3 +24,4 @@ WebsocketMeta = namedtuple("WebsocketMeta", ("url", "token", "debug")) Notification = namedtuple("Notification", ("message", "level")) +Script = namedtuple("Script", ("src", "type", "content"), defaults=(None, ""))
--- a/libervia/server/pages.py Tue May 19 11:02:33 2020 +0200 +++ b/libervia/server/pages.py Tue May 19 11:05:40 2020 +0200 @@ -1332,11 +1332,16 @@ except KeyError: pass else: - template_data.setdefault('scripts', []).extend(scripts) + template_data.setdefault('scripts', set()).update(scripts) template_data.update(data.get('template', {})) - common_scripts = self.vhost_root.dyn_data_common['scripts'] + data_common = self.vhost_root.dyn_data_common + common_scripts = data_common['scripts'] if common_scripts: - template_data.setdefault('scripts', []).extend(common_scripts) + template_data.setdefault('scripts', set()).update(common_scripts) + if "template" in data_common: + for key, value in data_common["template"].items(): + if key not in template_data: + template_data[key] = value return self.host.renderer.render( self.template,
--- a/libervia/server/server.py Tue May 19 11:02:33 2020 +0200 +++ b/libervia/server/server.py Tue May 19 11:05:40 2020 +0200 @@ -190,7 +190,7 @@ self.named_pages = {} self.browser_modules = {} # template dynamic data used in all pages - self.dyn_data_common = {"scripts": []} + self.dyn_data_common = {"scripts": set()} for theme, data in host.renderer.getThemesData(site_name).items(): # we check themes for browser metadata, and merge them here if found self.site_themes.add(theme)
--- a/libervia/server/tasks/implicit/task_brython.py Tue May 19 11:02:33 2020 +0200 +++ b/libervia/server/tasks/implicit/task_brython.py Tue May 19 11:05:40 2020 +0200 @@ -8,6 +8,7 @@ from sat.core.log import getLogger from sat.core import exceptions from libervia.server.constants import Const as C +from libervia.server.classes import Script from libervia.server.tasks import task @@ -60,18 +61,24 @@ for dyn_data in self.resource.browser_modules["brython"]: url_hash = dyn_data['url_hash'] import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}" - on_load_opts = { - "debug": 1, - "cache": True, - "pythonpath": [f"/{C.BUILD_DIR}", import_url], - } - dyn_data.setdefault('scripts', []).extend([ - {"src": f"/{C.BUILD_DIR}/brython.js"}, - ]) - dyn_data.setdefault('template', {})['body_onload'] = ( - f"brython({json.dumps(on_load_opts)})") + dyn_data.setdefault('scripts', set()).update({ + Script(src=f"/{C.BUILD_DIR}/brython.js"), + }) + dyn_data.setdefault('template', {})['body_onload'] = self.getBodyOnload( + extra_path=[import_url]) self.WATCH_DIRS.append(dyn_data['path'].resolve()) + def getBodyOnload(self, debug=True, cache=True, extra_path=None): + on_load_opts = {"pythonpath": [f"/{C.BUILD_DIR}"]} + if debug: + on_load_opts[debug] = 1 + if cache: + on_load_opts["cache"] = True + if extra_path is not None: + on_load_opts["pythonpath"].extend(extra_path) + + return f"brython({json.dumps(on_load_opts)})" + def copyFiles(self, files_paths, dest): for p in files_paths: log.debug(f"copying {p}") @@ -99,13 +106,12 @@ init_file = dyn_data['path'] / '__init__.py' if init_file.is_file(): - common_scripts = self.resource.dyn_data_common['scripts'] - script = { - 'type': 'text/python', - 'src': init_dest_url - } - if script not in common_scripts: - common_scripts.append(script) + self.resource.dyn_data_common['scripts'].update({ + Script(src=f"/{C.BUILD_DIR}/brython.js"), + Script(type='text/python', src=init_dest_url) + }) + self.resource.dyn_data_common.setdefault( + "template", {})['body_onload'] = self.getBodyOnload() else: page_dyn_path = dyn_path / url_hash if page_dyn_path.exists(): @@ -116,10 +122,8 @@ log.debug("copying browser python files") self.copyFiles(dyn_data['path'].iterdir(), page_dyn_path) - script = { - 'type': 'text/python', - 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" - } - scripts = dyn_data['scripts'] - if script not in scripts: - scripts.append(script) + script = Script( + type='text/python', + src=f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" + ) + dyn_data['scripts'].add(script)