Coverage for gws-app/gws/spec/generator/manifest.py: 28%
46 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +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 js = json.loads('\n'.join(lines))
26 except Exception as exc:
27 raise Error('invalid json') from exc
29 return _parse(js, path)
32##
34def _version(val, js, path):
35 p = [int(s) for s in val.split('.')]
36 return '.'.join(str(s) for s in p)
39def _plugins(val, js, path):
40 plugins = []
41 basedir = os.path.dirname(path)
43 for p in val:
44 path = p['path'] if os.path.isabs(p['path']) else os.path.abspath(os.path.join(basedir, p['path']))
45 name = p.get('name') or os.path.basename(path)
46 plugins.append({'name': name, 'path': path})
48 return plugins
51def _strlist(val, js, path):
52 return [str(s) for s in val]
55def _str(val, js, path):
56 return str(val)
59def _bool(val, js, path):
60 return bool(val)
63_KEYS = [
64 ('uid', _str, None),
65 ('release', _version, None),
66 ('locales', _strlist, []),
67 ('plugins', _plugins, []),
68 ('excludePlugins', _strlist, []),
69 ('withFallbackConfig', _bool, False),
70 ('withStrictConfig', _bool, False),
71]
74def _parse(js, path):
75 res = {}
77 for key, fn, default in _KEYS:
78 if key not in js:
79 res[key] = default
80 else:
81 try:
82 res[key] = fn(js[key], js, path)
83 except Exception as exc:
84 raise Error(f'invalid value for key {key!r} in {path!r}') from exc
86 return res