diff libervia/server/tasks/implicit/task_brython.py @ 1253:6d49fae517ba

pages: browser metadata + root `_browser`: - the `_browser` directory can now be put in root of a site `pages` directory, it will then include modules for the whole website - in `_browser` directories (notably the root one), a `browser_meta.json` file can be put to specify settings for a browser engine - pathlib.Path is now used LiberviaRootResource.site_path - introduced some type hints - task_brython copy modules in root `_browser` to build_path root. - minimal python version is now 3.7 due to type hints
author Goffi <goffi@goffi.org>
date Wed, 29 Apr 2020 17:34:53 +0200
parents 821b6ce57f99
children 08cd652dea14
line wrap: on
line diff
--- a/libervia/server/tasks/implicit/task_brython.py	Wed Apr 29 15:00:54 2020 +0200
+++ b/libervia/server/tasks/implicit/task_brython.py	Wed Apr 29 17:34:53 2020 +0200
@@ -7,7 +7,6 @@
 from sat.core.i18n import _
 from sat.core.log import getLogger
 from sat.core import exceptions
-from sat.tools.common.template import safe
 from libervia.server.constants import Const as C
 from libervia.server.tasks import task
 
@@ -16,7 +15,6 @@
 
 
 class Task(task.Task):
-    DOC_DIRS_DEFAULT = ('doc', 'docs')
 
     def prepare(self):
         if "brython" not in self.resource.browser_modules:
@@ -64,7 +62,8 @@
             import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}"
             on_load_opts = {
                 "debug": 1,
-                "pythonpath": [import_url],
+                "cache": True,
+                "pythonpath": [f"/{C.BUILD_DIR}", import_url],
             }
             dyn_data['template'] = {
                 "scripts": [{"src": f"/{C.BUILD_DIR}/brython.js"}],
@@ -72,28 +71,35 @@
             }
             self.WATCH_DIRS.append(dyn_data['path'].resolve())
 
+    def copyFiles(self, files_paths, dest):
+        for p in files_paths:
+            log.debug(f"copying {p}")
+            if p.is_dir():
+                shutil.copytree(p, dest)
+            else:
+                shutil.copy(p, dest)
+
     def start(self):
         dyn_path = self.build_path / C.BUILD_DIR_DYN
         for dyn_data in self.resource.browser_modules["brython"]:
             url_hash = dyn_data['url_hash']
-            page_dyn_path = dyn_path / url_hash
-            if page_dyn_path.exists():
-                log.debug("cleaning existing path")
-                shutil.rmtree(page_dyn_path)
+            if url_hash is None:
+                # root modules
+                self.copyFiles(dyn_data['path'].glob('*py'), self.build_path)
+            else:
+                page_dyn_path = dyn_path / url_hash
+                if page_dyn_path.exists():
+                    log.debug("cleaning existing path")
+                    shutil.rmtree(page_dyn_path)
 
-            page_dyn_path.mkdir(parents=True, exist_ok=True)
-            log.debug("copying browser python files")
-            for p in dyn_data['path'].iterdir():
-                log.debug(f"copying {p}")
-                if p.is_dir():
-                    shutil.copytree(p, page_dyn_path)
-                else:
-                    shutil.copy(p, page_dyn_path)
+                page_dyn_path.mkdir(parents=True, exist_ok=True)
+                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['template']['scripts']
-            if script not in scripts:
-                scripts.append(script)
+                script = {
+                    'type': 'text/python',
+                    'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py"
+                }
+                scripts = dyn_data['template']['scripts']
+                if script not in scripts:
+                    scripts.append(script)