Coverage for gws-app/gws/spec/generator/specs.py: 4%
89 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
1from typing import Dict
2from . import base
5def extract(gen: base.Generator):
6 """Extracts server specs from the types library.
8 For the server, we need
9 - all gws.ext.object.xxx, but not their properties (too many)
10 - all gws.ext.config.xxx and gws.ext.properties.xxx
11 - their properties, recursively
12 - command methods (gws.ext.command.xxx)
13 - their args and rets, recursively
14 """
16 queue = []
17 out: Dict[str, Dict] = dict()
19 typ = gen.types['gws.base.application.core.Config']
20 out[typ.uid] = dict(c=base.C.CONFIG, tProperties=typ.tProperties)
21 queue.extend(typ.tProperties.values())
23 typ = gen.types['gws.base.application.core.Object']
24 mod = gen.types.get(typ.tModule)
25 out[typ.uid] = dict(c=base.C.OBJECT, modName=mod.name, modPath=mod.path) # type: ignore
27 queue.extend(set(typ.uid for typ in gen.types.values() if typ.extName))
29 _extract(gen, queue, out)
31 for uid, spec in out.items():
32 typ = gen.types[uid]
33 spec.setdefault('c', typ.c)
34 spec.setdefault('uid', typ.uid)
35 if typ.ident:
36 spec.setdefault('ident', typ.ident)
37 if typ.extName:
38 spec.setdefault('extName', typ.extName)
40 return sorted(out.values(), key=lambda t: t['uid'])
43def _extract(gen, queue, out):
44 while queue:
46 typ = gen.types[queue.pop(0)]
47 if typ.uid in out or typ.c == base.C.ATOM:
48 continue
50 if typ.c == base.C.METHOD and typ.extName.startswith(base.EXT_COMMAND_PREFIX):
51 mod = gen.types.get(typ.tModule)
52 out[typ.uid] = dict(c=base.C.COMMAND, tOwner=typ.tOwner, tArg=typ.tArgs[-1], modName=mod.name, modPath=mod.path)
53 queue.append(typ.tOwner)
54 queue.append(typ.tArgs[-1])
55 continue
57 if typ.c == base.C.CLASS and typ.extName.startswith(base.EXT_OBJECT_PREFIX):
58 mod = gen.types.get(typ.tModule)
59 out[typ.uid] = dict(c=base.C.OBJECT, modName=mod.name, modPath=mod.path)
60 continue
62 if typ.c == base.C.CLASS and typ.extName.startswith(base.EXT_CONFIG_PREFIX):
63 out[typ.uid] = dict(c=base.C.CONFIG, tProperties=typ.tProperties)
64 queue.extend(typ.tProperties.values())
65 continue
67 if typ.c == base.C.CLASS and typ.extName.startswith(base.EXT_PROPS_PREFIX):
68 out[typ.uid] = dict(c=base.C.PROPS, tProperties=typ.tProperties)
69 queue.extend(typ.tProperties.values())
70 continue
72 if typ.c == base.C.CLASS:
73 mod = gen.types.get(typ.tModule)
74 out[typ.uid] = dict(c=base.C.CLASS, modName=mod.name, modPath=mod.path, tProperties=typ.tProperties)
75 queue.extend(typ.tProperties.values())
76 continue
78 if typ.c == base.C.DICT:
79 out[typ.uid] = dict(tKey=typ.tKey, tValue=typ.tValue)
80 queue.append(typ.tKey)
81 queue.append(typ.tValue)
82 continue
84 if typ.c in {base.C.LIST, base.C.SET}:
85 out[typ.uid] = dict(tItem=typ.tItem)
86 queue.append(typ.tItem)
87 continue
89 if typ.c in {base.C.OPTIONAL, base.C.TYPE}:
90 out[typ.uid] = dict(tTarget=typ.tTarget)
91 queue.append(typ.tTarget)
92 continue
94 if typ.c in {base.C.TUPLE, base.C.UNION}:
95 out[typ.uid] = dict(tItems=typ.tItems)
96 queue.extend(typ.tItems)
97 continue
99 if typ.c == base.C.VARIANT:
100 out[typ.uid] = dict(tMembers=typ.tMembers)
101 queue.extend(typ.tMembers.values())
102 continue
104 if typ.c == base.C.PROPERTY:
105 out[typ.uid] = dict(tValue=typ.tValue, tOwner=typ.tOwner, defaultValue=typ.defaultValue, hasDefault=typ.hasDefault)
106 queue.append(typ.tValue)
107 queue.append(typ.tOwner)
108 continue
110 if typ.c == base.C.CLASS:
111 out[typ.uid] = dict(tProperties=typ.tProperties)
112 queue.extend(typ.tProperties.values())
113 continue
115 if typ.c == base.C.ENUM:
116 out[typ.uid] = dict(enumValues=typ.enumValues, enumDocs=typ.enumDocs)
117 continue
119 if typ.c == base.C.LITERAL:
120 out[typ.uid] = dict(literalValues=typ.literalValues)
121 continue
123 if typ.c == base.C.EXT:
124 continue
126 raise base.Error(f'unbound object {typ.c}: {typ.uid!r} in {typ.pos}')