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

1"""Tools to deal with r8 MANIFEST files.""" 

2 

3import json 

4import os 

5 

6 

7class Error(Exception): 

8 pass 

9 

10 

11def from_path(path): 

12 with open(path, 'rt', encoding='utf8') as fp: 

13 return from_text(fp.read(), path) 

14 

15 

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) 

23 

24 try: 

25 dct = json.loads('\n'.join(lines)) 

26 except Exception as exc: 

27 raise Error('invalid json') from exc 

28 

29 return _parse(dct, path) 

30 

31 

32## 

33 

34def _version(val, dct, path): 

35 p = [int(s) for s in val.split('.')] 

36 return '.'.join(str(s) for s in p) 

37 

38 

39def _plugins(val, dct, path): 

40 plugins = [] 

41 

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}) 

46 

47 return plugins 

48 

49 

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)) 

53 

54 

55def _strlist(val, dct, path): 

56 return [str(s) for s in val] 

57 

58 

59def _str(val, dct, path): 

60 return str(val) 

61 

62 

63def _bool(val, dct, path): 

64 return bool(val) 

65 

66 

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] 

77 

78 

79def _parse(dct, path): 

80 res = {} 

81 

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 

90 

91 return res