annotate libervia/web/server/tasks/implicit/task_js_modules.py @ 1519:01b8d68edd70

version: use standard Python's `dev0` instead of Libervia specific `D` for dev version
author Goffi <goffi@goffi.org>
date Tue, 06 Jun 2023 17:40:16 +0200
parents eb00d593801d
children 038d4bfdd967
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/ur/bin/env python3
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 import json
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from pathlib import Path
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
5 from libervia.backend.core.i18n import _
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
6 from libervia.backend.core.log import getLogger
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
7 from libervia.backend.core import exceptions
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
8 from libervia.web.server.constants import Const as C
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
9 from libervia.web.server.tasks import task
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 log = getLogger(__name__)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
13
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 class Task(task.Task):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 async def prepare(self):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 if "js" not in self.resource.browser_modules:
1434
2f6dac783c8e tasks(js_modules, sass): remove support for `npm`:
Goffi <goffi@goffi.org>
parents: 1273
diff changeset
19 raise exceptions.CancelError("No JS module needed")
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 async def start(self):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 js_data = self.resource.browser_modules['js']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 package = js_data.get('package', {})
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 package_path = self.build_path / 'package.json'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 with package_path.open('w') as f:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 json.dump(package, f)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1434
diff changeset
28 cmd = self.find_command('yarnpkg', 'yarn')
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 await self.runCommand(cmd, 'install', path=str(self.build_path))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 brython_map = js_data['brython_map']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 pass
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 else:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 log.info(_("creating JS modules mapping for Brython"))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 js_modules_path = self.build_path / 'js_modules'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 js_modules_path.mkdir(exist_ok=True)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 init_path = js_modules_path / '__init__.py'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 init_path.touch()
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 for module_name, module_data in brython_map.items():
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 log.debug(f"generating mapping for {module_name}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 if ' ' in module_name:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 f"module {module_name!r} has space(s), it must not!")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 module_path = js_modules_path / f"{module_name}.py"
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if isinstance(module_data, str):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 module_data = {'path': module_data}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 js_path = module_data.pop('path')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 f'module data for {module_name} must have a "path" key')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 module_data['path'] = Path('node_modules') / js_path.strip(' /')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 export = module_data.get('export') or [module_name]
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 export_objects = '\n'.join(f'{e} = window.{e}' for e in export)
1259
0b269d4a46a3 task (js_modules): handle `extra_init`:
Goffi <goffi@goffi.org>
parents: 1254
diff changeset
58 extra_kwargs = {"build_dir": C.BUILD_DIR}
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 with module_path.open('w') as f:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 f.write(f"""\
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 #!/usr/bin/env python3
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 from browser import window, load
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 {module_data.get('extra_import', '')}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 load("{Path('/').joinpath(C.BUILD_DIR, module_data['path'])}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 {export_objects}
1259
0b269d4a46a3 task (js_modules): handle `extra_init`:
Goffi <goffi@goffi.org>
parents: 1254
diff changeset
68 {module_data.get('extra_init', '').format(**extra_kwargs)}
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 """)