comparison libervia/server/tasks/implicit/task_brython.py @ 1282:0e4e413eb8db

server: user new OrderedSet to handle scripts: OrderedSet is needed because scripts must not be duplicated and must be generated in right order.
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 16:47:51 +0200
parents 2d1ceb026d0e
children 089742e065e3
comparison
equal deleted inserted replaced
1281:25eacbb1d59e 1282:0e4e413eb8db
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 import utils
10 from libervia.server.constants import Const as C 11 from libervia.server.constants import Const as C
11 from libervia.server.classes import Script 12 from libervia.server.classes import Script
12 from libervia.server.tasks import task 13 from libervia.server.tasks import task
13 14
14 15
60 self.WATCH_DIRS = [] 61 self.WATCH_DIRS = []
61 62
62 for dyn_data in self.resource.browser_modules["brython"]: 63 for dyn_data in self.resource.browser_modules["brython"]:
63 url_hash = dyn_data['url_hash'] 64 url_hash = dyn_data['url_hash']
64 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}" 65 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}"
65 dyn_data.setdefault('scripts', set()).update({ 66 dyn_data.setdefault('scripts', utils.OrderedSet()).update([
66 Script(src=f"/{C.BUILD_DIR}/brython.js"), 67 Script(src=f"/{C.BUILD_DIR}/brython.js"),
67 Script(src=f"/{C.BUILD_DIR}/brython_stdlib.js"), 68 Script(src=f"/{C.BUILD_DIR}/brython_stdlib.js"),
68 }) 69 ])
69 dyn_data.setdefault('template', {})['body_onload'] = self.getBodyOnload( 70 dyn_data.setdefault('template', {})['body_onload'] = self.getBodyOnload(
70 extra_path=[import_url]) 71 extra_path=[import_url])
71 self.WATCH_DIRS.append(dyn_data['path'].resolve()) 72 self.WATCH_DIRS.append(dyn_data['path'].resolve())
72 73
73 def getBodyOnload(self, debug=True, cache=True, extra_path=None): 74 def getBodyOnload(self, debug=True, cache=True, extra_path=None):
106 107
107 self.copyFiles(dyn_data['path'].glob('*py'), dest) 108 self.copyFiles(dyn_data['path'].glob('*py'), dest)
108 109
109 init_file = dyn_data['path'] / '__init__.py' 110 init_file = dyn_data['path'] / '__init__.py'
110 if init_file.is_file(): 111 if init_file.is_file():
111 self.resource.dyn_data_common['scripts'].update({ 112 self.resource.dyn_data_common['scripts'].update([
112 Script(src=f"/{C.BUILD_DIR}/brython.js"), 113 Script(src=f"/{C.BUILD_DIR}/brython.js"),
113 Script(type='text/python', src=init_dest_url) 114 Script(type='text/python', src=init_dest_url)
114 }) 115 ])
115 self.resource.dyn_data_common.setdefault( 116 self.resource.dyn_data_common.setdefault(
116 "template", {})['body_onload'] = self.getBodyOnload() 117 "template", {})['body_onload'] = self.getBodyOnload()
117 else: 118 else:
118 page_dyn_path = dyn_path / url_hash 119 page_dyn_path = dyn_path / url_hash
119 if page_dyn_path.exists(): 120 if page_dyn_path.exists():