Coverage for gws-app/gws/spec/generator/manifest.py: 58%
48 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
1"""Tools to deal with r8 MANIFEST files."""
3import json
4import os
7class Error(Exception):
8 pass
11def from_path(path):
12 with open(path, 'rt', encoding='utf8') as fp:
13 return from_text(fp.read(), path)
16def from_text(text, path):
17 lines = []
18 for s in text.split('\n'):
19 # we allow // or # comments in json
20 if s.strip().startswith(('//', '#')):
21 s = ''
22 lines.append(s)
24 try:
25 dct = json.loads('\n'.join(lines))
26 except Exception as exc:
27 raise Error('invalid json') from exc
29 return _parse(dct, path)
32##
34def _version(val, dct, path):
35 p = [int(s) for s in val.split('.')]
36 return '.'.join(str(s) for s in p)
39def _plugins(val, dct, path):
40 plugins = []
42 for p in val:
43 plugin_path = _relpath(p['path'], dct, path)
44 plugin_name = p.get('name') or os.path.basename(plugin_path)
45 plugins.append({'name': plugin_name, 'path': plugin_path})
47 return plugins
50def _relpath(val, dct, path):
51 basedir = os.path.dirname(path)
52 return val if os.path.isabs(val) else os.path.abspath(os.path.join(basedir, val))
55def _strlist(val, dct, path):
56 return [str(s) for s in val]
59def _str(val, dct, path):
60 return str(val)
63def _bool(val, dct, path):
64 return bool(val)
67_KEYS = [
68 ('uid', _str, None),
69 ('release', _version, None),
70 ('locales', _strlist, []),
71 ('plugins', _plugins, []),
72 ('tsConfig', _relpath, ''),
73 ('excludePlugins', _strlist, []),
74 ('withFallbackConfig', _bool, False),
75 ('withStrictConfig', _bool, False),
76]
79def _parse(dct, path):
80 res = {}
82 for key, fn, default in _KEYS:
83 if key not in dct:
84 res[key] = default
85 else:
86 try:
87 res[key] = fn(dct[key], dct, path)
88 except Exception as exc:
89 raise Error(f'invalid value for key {key!r} in {path!r}') from exc
91 return res