comparison 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
comparison
equal deleted inserted replaced
1255:b1fb57e9176d 1256:08cd652dea14
63 on_load_opts = { 63 on_load_opts = {
64 "debug": 1, 64 "debug": 1,
65 "cache": True, 65 "cache": True,
66 "pythonpath": [f"/{C.BUILD_DIR}", import_url], 66 "pythonpath": [f"/{C.BUILD_DIR}", import_url],
67 } 67 }
68 dyn_data['template'] = { 68 dyn_data.setdefault('scripts', set()).extend([
69 "scripts": [{"src": f"/{C.BUILD_DIR}/brython.js"}], 69 {"src": f"/{C.BUILD_DIR}/brython.js"},
70 "body_onload": f"brython({json.dumps(on_load_opts)})", 70 ])
71 } 71 dyn_data.setdefault('template', {})['body_onload'] = (
72 f"brython({json.dumps(on_load_opts)})")
72 self.WATCH_DIRS.append(dyn_data['path'].resolve()) 73 self.WATCH_DIRS.append(dyn_data['path'].resolve())
73 74
74 def copyFiles(self, files_paths, dest): 75 def copyFiles(self, files_paths, dest):
75 for p in files_paths: 76 for p in files_paths:
76 log.debug(f"copying {p}") 77 log.debug(f"copying {p}")
83 dyn_path = self.build_path / C.BUILD_DIR_DYN 84 dyn_path = self.build_path / C.BUILD_DIR_DYN
84 for dyn_data in self.resource.browser_modules["brython"]: 85 for dyn_data in self.resource.browser_modules["brython"]:
85 url_hash = dyn_data['url_hash'] 86 url_hash = dyn_data['url_hash']
86 if url_hash is None: 87 if url_hash is None:
87 # root modules 88 # root modules
88 self.copyFiles(dyn_data['path'].glob('*py'), self.build_path) 89 url_prefix = dyn_data.get('url_prefix')
90 if url_prefix is None:
91 dest = self.build_path
92 init_dest_url = f"/{C.BUILD_DIR}/__init__.py"
93 else:
94 dest = self.build_path / url_prefix
95 dest.mkdir(exist_ok = True)
96 init_dest_url = f"/{C.BUILD_DIR}/{url_prefix}/__init__.py"
97
98 self.copyFiles(dyn_data['path'].glob('*py'), dest)
99
100 init_file = dyn_data['path'] / '__init__.py'
101 if init_file.is_file():
102 common_scripts = self.resource.dyn_data_common['scripts']
103 script = {
104 'type': 'text/python',
105 'src': init_dest_url
106 }
107 if script not in common_scripts:
108 common_scripts.append(script)
89 else: 109 else:
90 page_dyn_path = dyn_path / url_hash 110 page_dyn_path = dyn_path / url_hash
91 if page_dyn_path.exists(): 111 if page_dyn_path.exists():
92 log.debug("cleaning existing path") 112 log.debug("cleaning existing path")
93 shutil.rmtree(page_dyn_path) 113 shutil.rmtree(page_dyn_path)
98 118
99 script = { 119 script = {
100 'type': 'text/python', 120 'type': 'text/python',
101 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" 121 'src': f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py"
102 } 122 }
103 scripts = dyn_data['template']['scripts'] 123 scripts = dyn_data['scripts']
104 if script not in scripts: 124 if script not in scripts:
105 scripts.append(script) 125 scripts.append(script)