comparison 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
comparison
equal deleted inserted replaced
1252:80a92eb82b7f 1253:6d49fae517ba
5 from pathlib import Path 5 from pathlib import Path
6 from ast import literal_eval 6 from ast import literal_eval
7 from sat.core.i18n import _ 7 from sat.core.i18n import _
8 from sat.core.log import getLogger 8 from sat.core.log import getLogger
9 from sat.core import exceptions 9 from sat.core import exceptions
10 from sat.tools.common.template import safe
11 from libervia.server.constants import Const as C 10 from libervia.server.constants import Const as C
12 from libervia.server.tasks import task 11 from libervia.server.tasks import task
13 12
14 13
15 log = getLogger(__name__) 14 log = getLogger(__name__)
16 15
17 16
18 class Task(task.Task): 17 class Task(task.Task):
19 DOC_DIRS_DEFAULT = ('doc', 'docs')
20 18
21 def prepare(self): 19 def prepare(self):
22 if "brython" not in self.resource.browser_modules: 20 if "brython" not in self.resource.browser_modules:
23 raise exceptions.CancelError(f"No brython module found") 21 raise exceptions.CancelError(f"No brython module found")
24 22
62 for dyn_data in self.resource.browser_modules["brython"]: 60 for dyn_data in self.resource.browser_modules["brython"]:
63 url_hash = dyn_data['url_hash'] 61 url_hash = dyn_data['url_hash']
64 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}" 62 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}"
65 on_load_opts = { 63 on_load_opts = {
66 "debug": 1, 64 "debug": 1,
67 "pythonpath": [import_url], 65 "cache": True,
66 "pythonpath": [f"/{C.BUILD_DIR}", import_url],
68 } 67 }
69 dyn_data['template'] = { 68 dyn_data['template'] = {
70 "scripts": [{"src": f"/{C.BUILD_DIR}/brython.js"}], 69 "scripts": [{"src": f"/{C.BUILD_DIR}/brython.js"}],
71 "body_onload": f"brython({json.dumps(on_load_opts)})", 70 "body_onload": f"brython({json.dumps(on_load_opts)})",
72 } 71 }
73 self.WATCH_DIRS.append(dyn_data['path'].resolve()) 72 self.WATCH_DIRS.append(dyn_data['path'].resolve())
74 73
74 def copyFiles(self, files_paths, dest):
75 for p in files_paths:
76 log.debug(f"copying {p}")
77 if p.is_dir():
78 shutil.copytree(p, dest)
79 else:
80 shutil.copy(p, dest)
81
75 def start(self): 82 def start(self):
76 dyn_path = self.build_path / C.BUILD_DIR_DYN 83 dyn_path = self.build_path / C.BUILD_DIR_DYN
77 for dyn_data in self.resource.browser_modules["brython"]: 84 for dyn_data in self.resource.browser_modules["brython"]:
78 url_hash = dyn_data['url_hash'] 85 url_hash = dyn_data['url_hash']
79 page_dyn_path = dyn_path / url_hash 86 if url_hash is None:
80 if page_dyn_path.exists(): 87 # root modules
81 log.debug("cleaning existing path") 88 self.copyFiles(dyn_data['path'].glob('*py'), self.build_path)
82 shutil.rmtree(page_dyn_path) 89 else:
90 page_dyn_path = dyn_path / url_hash
91 if page_dyn_path.exists():
92 log.debug("cleaning existing path")
93 shutil.rmtree(page_dyn_path)
83 94
84 page_dyn_path.mkdir(parents=True, exist_ok=True) 95 page_dyn_path.mkdir(parents=True, exist_ok=True)
85 log.debug("copying browser python files") 96 log.debug("copying browser python files")
86 for p in dyn_data['path'].iterdir(): 97 self.copyFiles(dyn_data['path'].iterdir(), page_dyn_path)
87 log.debug(f"copying {p}")
88 if p.is_dir():
89 shutil.copytree(p, page_dyn_path)
90 else:
91 shutil.copy(p, page_dyn_path)
92 98
93 script = { 99 script = {
94 'type': 'text/python', 100 'type': 'text/python',
95 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" 101 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py"
96 } 102 }
97 scripts = dyn_data['template']['scripts'] 103 scripts = dyn_data['template']['scripts']
98 if script not in scripts: 104 if script not in scripts:
99 scripts.append(script) 105 scripts.append(script)