Coverage for gws-app/gws/spec/generator/generator.py: 9%
74 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
1import os
3from . import base, configref, manifest, normalizer, parser, specs, strings, typescript, util
5Error = base.Error
8def generate_and_store(root_dir=None, out_dir=None, manifest_path=None, debug=False):
9 gen = generate_all(root_dir, out_dir, manifest_path, debug)
11 util.write_json(gen.outDir + '/types.json', {
12 'meta': gen.meta,
13 'types': gen.types
14 })
16 util.write_json(gen.outDir + '/specs.json', {
17 'meta': gen.meta,
18 'chunks': gen.chunks,
19 'specs': gen.specs,
20 'strings': gen.strings
21 })
23 util.write_file(gen.outDir + '/specs.strings.ini', util.make_ini(gen.strings))
24 util.write_file(gen.outDir + '/specs.ts', gen.typescript)
26 util.write_file(gen.outDir + '/configref.en.html', gen.configRef['en'])
27 util.write_file(gen.outDir + '/configref.de.html', gen.configRef['de'])
30def generate_specs(manifest_path=None):
31 gen = generate_all(manifest_path=manifest_path)
32 return {
33 'meta': gen.meta,
34 'chunks': gen.chunks,
35 'specs': gen.specs,
36 'strings': gen.strings,
37 }
40def generate_all(root_dir=None, out_dir=None, manifest_path=None, debug=False):
41 base.log.set_level('DEBUG' if debug else 'INFO')
43 gen = base.Generator()
44 gen.rootDir = root_dir or base.APP_DIR
45 gen.outDir = out_dir
46 gen.selfDir = base.SELF_DIR
47 gen.debug = debug
48 gen.manifestPath = manifest_path
50 init_generator(gen)
52 gen.dump('init')
54 parser.parse(gen)
55 gen.dump('parsed')
57 normalizer.normalize(gen)
58 gen.dump('normalized')
60 gen.specs = specs.extract(gen)
61 gen.dump('specs')
63 gen.typescript = typescript.create(gen)
64 gen.strings = strings.collect(gen)
66 gen.configRef['en'] = configref.create(gen, 'en')
67 gen.configRef['de'] = configref.create(gen, 'de')
69 return gen
72def init_generator(gen) -> base.Generator:
73 gen.meta = {
74 'version': util.read_file(gen.rootDir + '/VERSION').strip(),
75 'manifestPath': None,
76 'manifest': None,
77 }
79 gen.chunks = [
80 dict(name=name, sourceDir=gen.rootDir + path, bundleDir=base.APP_DIR)
81 for name, path in base.SYSTEM_CHUNKS
82 ]
84 manifest_plugins = None
86 if gen.manifestPath:
87 try:
88 base.log.debug(f'loading manifest {gen.manifestPath!r}')
89 gen.meta['manifestPath'] = gen.manifestPath
90 gen.meta['manifest'] = manifest.from_path(gen.manifestPath)
91 except Exception as exc:
92 raise base.Error(f'error loading manifest {gen.manifestPath!r}') from exc
93 manifest_plugins = gen.meta['manifest'].get('plugins')
95 plugin_dict = {}
97 for path in util.find_dirs(gen.rootDir + base.PLUGIN_DIR):
98 name = os.path.basename(path)
99 chunk = dict(name=base.PLUGIN_PREFIX + '.' + name, sourceDir=path, bundleDir=path)
100 plugin_dict[chunk['name']] = chunk
102 for p in manifest_plugins or []:
103 path = p.get('path')
104 name = p.get('name') or os.path.basename(path)
105 if not os.path.isdir(path):
106 raise base.Error(f'error loading plugin {name!r}: directory {path!r} not found')
107 chunk = dict(name=base.PLUGIN_PREFIX + '.' + name, sourceDir=path, bundleDir=path)
108 plugin_dict[chunk['name']] = chunk
110 gen.chunks.extend(plugin_dict.values())
112 for chunk in gen.chunks:
113 excl = base.EXCLUDE_PATHS + chunk.get('exclude', [])
114 chunk['paths'] = {kind: [] for _, kind in base.FILE_KINDS}
116 if not os.path.isdir(chunk['sourceDir']):
117 continue
119 for path in util.find_files(chunk['sourceDir']):
120 if any(x in path for x in excl):
121 continue
122 for pattern, kind in base.FILE_KINDS:
123 if path.endswith(pattern):
124 chunk['paths'][kind].append(path)
125 break
127 gen.chunks.insert(
128 0,
129 dict(
130 name='gws',
131 sourceDir=gen.rootDir + '/gws',
132 bundleDir=gen.rootDir + '/gws',
133 paths={'python': [gen.rootDir + '/gws/__init__.py']}
134 ))
136 return gen