diff libervia/server/tasks/implicit/task_brython.py @ 1256:08cd652dea14

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.
author Goffi <goffi@goffi.org>
date Sun, 03 May 2020 18:15:22 +0200
parents 6d49fae517ba
children 3fc3f2cde6a1
line wrap: on
line diff
--- 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)