Mercurial > libervia-web
annotate libervia/server/tasks/implicit/task_js_modules.py @ 1459:47db314e60ca
server: `getExtBaseURL` type hints
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 29 Sep 2021 17:39:35 +0200 |
parents | 2f6dac783c8e |
children | 106bae41f5c8 |
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 |
780dbc2f4853
server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 from sat.core.i18n import _ |
780dbc2f4853
server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 from sat.core.log import getLogger |
780dbc2f4853
server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 from sat.core import exceptions |
780dbc2f4853
server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 from libervia.server.constants import Const as C |
780dbc2f4853
server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 from libervia.server.tasks import task |
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 |
1434
2f6dac783c8e
tasks(js_modules, sass): remove support for `npm`:
Goffi <goffi@goffi.org>
parents:
1273
diff
changeset
|
28 cmd = self.findCommand('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 """) |