Mercurial > libervia-web
comparison libervia/server/tasks/implicit/task_brython.py @ 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 | 3fc3f2cde6a1 |
children | 2d1ceb026d0e |
comparison
equal
deleted
inserted
replaced
1267:b5f920845d34 | 1268:e628724530ec |
---|---|
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 libervia.server.constants import Const as C | 10 from libervia.server.constants import Const as C |
11 from libervia.server.classes import Script | |
11 from libervia.server.tasks import task | 12 from libervia.server.tasks import task |
12 | 13 |
13 | 14 |
14 log = getLogger(__name__) | 15 log = getLogger(__name__) |
15 | 16 |
58 self.WATCH_DIRS = [] | 59 self.WATCH_DIRS = [] |
59 | 60 |
60 for dyn_data in self.resource.browser_modules["brython"]: | 61 for dyn_data in self.resource.browser_modules["brython"]: |
61 url_hash = dyn_data['url_hash'] | 62 url_hash = dyn_data['url_hash'] |
62 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}" | 63 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}" |
63 on_load_opts = { | 64 dyn_data.setdefault('scripts', set()).update({ |
64 "debug": 1, | 65 Script(src=f"/{C.BUILD_DIR}/brython.js"), |
65 "cache": True, | 66 }) |
66 "pythonpath": [f"/{C.BUILD_DIR}", import_url], | 67 dyn_data.setdefault('template', {})['body_onload'] = self.getBodyOnload( |
67 } | 68 extra_path=[import_url]) |
68 dyn_data.setdefault('scripts', []).extend([ | |
69 {"src": f"/{C.BUILD_DIR}/brython.js"}, | |
70 ]) | |
71 dyn_data.setdefault('template', {})['body_onload'] = ( | |
72 f"brython({json.dumps(on_load_opts)})") | |
73 self.WATCH_DIRS.append(dyn_data['path'].resolve()) | 69 self.WATCH_DIRS.append(dyn_data['path'].resolve()) |
70 | |
71 def getBodyOnload(self, debug=True, cache=True, extra_path=None): | |
72 on_load_opts = {"pythonpath": [f"/{C.BUILD_DIR}"]} | |
73 if debug: | |
74 on_load_opts[debug] = 1 | |
75 if cache: | |
76 on_load_opts["cache"] = True | |
77 if extra_path is not None: | |
78 on_load_opts["pythonpath"].extend(extra_path) | |
79 | |
80 return f"brython({json.dumps(on_load_opts)})" | |
74 | 81 |
75 def copyFiles(self, files_paths, dest): | 82 def copyFiles(self, files_paths, dest): |
76 for p in files_paths: | 83 for p in files_paths: |
77 log.debug(f"copying {p}") | 84 log.debug(f"copying {p}") |
78 if p.is_dir(): | 85 if p.is_dir(): |
97 | 104 |
98 self.copyFiles(dyn_data['path'].glob('*py'), dest) | 105 self.copyFiles(dyn_data['path'].glob('*py'), dest) |
99 | 106 |
100 init_file = dyn_data['path'] / '__init__.py' | 107 init_file = dyn_data['path'] / '__init__.py' |
101 if init_file.is_file(): | 108 if init_file.is_file(): |
102 common_scripts = self.resource.dyn_data_common['scripts'] | 109 self.resource.dyn_data_common['scripts'].update({ |
103 script = { | 110 Script(src=f"/{C.BUILD_DIR}/brython.js"), |
104 'type': 'text/python', | 111 Script(type='text/python', src=init_dest_url) |
105 'src': init_dest_url | 112 }) |
106 } | 113 self.resource.dyn_data_common.setdefault( |
107 if script not in common_scripts: | 114 "template", {})['body_onload'] = self.getBodyOnload() |
108 common_scripts.append(script) | |
109 else: | 115 else: |
110 page_dyn_path = dyn_path / url_hash | 116 page_dyn_path = dyn_path / url_hash |
111 if page_dyn_path.exists(): | 117 if page_dyn_path.exists(): |
112 log.debug("cleaning existing path") | 118 log.debug("cleaning existing path") |
113 shutil.rmtree(page_dyn_path) | 119 shutil.rmtree(page_dyn_path) |
114 | 120 |
115 page_dyn_path.mkdir(parents=True, exist_ok=True) | 121 page_dyn_path.mkdir(parents=True, exist_ok=True) |
116 log.debug("copying browser python files") | 122 log.debug("copying browser python files") |
117 self.copyFiles(dyn_data['path'].iterdir(), page_dyn_path) | 123 self.copyFiles(dyn_data['path'].iterdir(), page_dyn_path) |
118 | 124 |
119 script = { | 125 script = Script( |
120 'type': 'text/python', | 126 type='text/python', |
121 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" | 127 src=f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" |
122 } | 128 ) |
123 scripts = dyn_data['scripts'] | 129 dyn_data['scripts'].add(script) |
124 if script not in scripts: | |
125 scripts.append(script) |