# HG changeset patch # User Goffi # Date 1588522522 -7200 # Node ID 08cd652dea14d2794d3fb27e0adee3998abc03ad # Parent b1fb57e9176d6fe0086490767ffb1fc31e9f5690 server, pages, tasks (brython): common_scripts: - scripts common to all pages can now be put in root `_browser` dir - `scripts` list is not set in `dyn_data`'s `template` anymore, but directly in a root `scripts` key (because it's not data which are sent verbatim to the template renderer) - if an `__init__.py` script is found in root `_browser` dir, brython task will add it to template scripts. diff -r b1fb57e9176d -r 08cd652dea14 libervia/server/pages.py --- a/libervia/server/pages.py Sun May 03 17:06:14 2020 +0200 +++ b/libervia/server/pages.py Sun May 03 18:15:22 2020 +0200 @@ -150,7 +150,7 @@ (request.postpath) else a the request will be transmitted to a subpage @param prepare_render(callable, None): if set, will be used to prepare the rendering. That often means gathering data using the bridge - @param render(callable, None): if not template is set, this method will be + @param render(callable, None): if template is not set, this method will be called and what it returns will be rendered. This method is mutually exclusive with template and must return a unicode string. @@ -897,7 +897,7 @@ return extra def setPagination(self, request, pubsub_data): - """Add to template_data if suitable + """Add to template_data if suitable "previous_page_url" and "next_page_url" will be added using respectively "before" and "after" URL parameters @@ -1319,21 +1319,23 @@ template_data['site'] = self.vhost_root.site_name if self.dyn_data: for data in self.dyn_data.values(): - template_data_dyn = data.get('template', {}) try: - scripts = template_data_dyn['scripts'] + scripts = data['scripts'] except KeyError: pass else: template_data.setdefault('scripts', []).extend(scripts) - template_data.update(template_data_dyn) + template_data.update(data.get('template', {})) + common_scripts = self.vhost_root.dyn_data_common['scripts'] + if common_scripts: + template_data.setdefault('scripts', []).extend(common_scripts) return self.host.renderer.render( self.template, page_url=self.getURL(), - media_path="/" + C.MEDIA_DIR, + media_path=f"/{C.MEDIA_DIR}", cache_path=session_data.cache_dir, - build_path="/" + C.BUILD_DIR + "/", + build_path=f"/{C.BUILD_DIR}/", main_menu=self.main_menu, **template_data) diff -r b1fb57e9176d -r 08cd652dea14 libervia/server/server.py --- a/libervia/server/server.py Sun May 03 17:06:14 2020 +0200 +++ b/libervia/server/server.py Sun May 03 18:15:22 2020 +0200 @@ -186,6 +186,8 @@ self.site_path = Path(site_path) self.named_pages = {} self.browser_modules = {} + # template dynamic data used in all pages + self.dyn_data_common = {"scripts": []} self.uri_callbacks = {} self.pages_redirects = {} self.cached_urls = {} diff -r b1fb57e9176d -r 08cd652dea14 libervia/server/tasks/implicit/task_brython.py --- a/libervia/server/tasks/implicit/task_brython.py Sun May 03 17:06:14 2020 +0200 +++ b/libervia/server/tasks/implicit/task_brython.py Sun May 03 18:15:22 2020 +0200 @@ -65,10 +65,11 @@ "cache": True, "pythonpath": [f"/{C.BUILD_DIR}", import_url], } - dyn_data['template'] = { - "scripts": [{"src": f"/{C.BUILD_DIR}/brython.js"}], - "body_onload": f"brython({json.dumps(on_load_opts)})", - } + dyn_data.setdefault('scripts', set()).extend([ + {"src": f"/{C.BUILD_DIR}/brython.js"}, + ]) + dyn_data.setdefault('template', {})['body_onload'] = ( + f"brython({json.dumps(on_load_opts)})") self.WATCH_DIRS.append(dyn_data['path'].resolve()) def copyFiles(self, files_paths, dest): @@ -85,7 +86,26 @@ url_hash = dyn_data['url_hash'] if url_hash is None: # root modules - self.copyFiles(dyn_data['path'].glob('*py'), self.build_path) + url_prefix = dyn_data.get('url_prefix') + if url_prefix is None: + dest = self.build_path + init_dest_url = f"/{C.BUILD_DIR}/__init__.py" + else: + dest = self.build_path / url_prefix + dest.mkdir(exist_ok = True) + init_dest_url = f"/{C.BUILD_DIR}/{url_prefix}/__init__.py" + + self.copyFiles(dyn_data['path'].glob('*py'), dest) + + 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) else: page_dyn_path = dyn_path / url_hash if page_dyn_path.exists(): @@ -100,6 +120,6 @@ 'type': 'text/python', 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" } - scripts = dyn_data['template']['scripts'] + scripts = dyn_data['scripts'] if script not in scripts: scripts.append(script)