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

1from typing import Dict 

2from . import base 

3 

4 

5def extract(gen: base.Generator): 

6 """Extracts server specs from the types library. 

7 

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

15 

16 queue = [] 

17 out: Dict[str, Dict] = dict() 

18 

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

22 

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 

26 

27 queue.extend(set(typ.uid for typ in gen.types.values() if typ.extName)) 

28 

29 _extract(gen, queue, out) 

30 

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) 

39 

40 return sorted(out.values(), key=lambda t: t['uid']) 

41 

42 

43def _extract(gen, queue, out): 

44 while queue: 

45 

46 typ = gen.types[queue.pop(0)] 

47 if typ.uid in out or typ.c == base.C.ATOM: 

48 continue 

49 

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 

56 

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 

61 

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 

66 

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 

71 

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 

77 

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 

83 

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 

88 

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 

93 

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 

98 

99 if typ.c == base.C.VARIANT: 

100 out[typ.uid] = dict(tMembers=typ.tMembers) 

101 queue.extend(typ.tMembers.values()) 

102 continue 

103 

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 

109 

110 if typ.c == base.C.CLASS: 

111 out[typ.uid] = dict(tProperties=typ.tProperties) 

112 queue.extend(typ.tProperties.values()) 

113 continue 

114 

115 if typ.c == base.C.ENUM: 

116 out[typ.uid] = dict(enumValues=typ.enumValues, enumDocs=typ.enumDocs) 

117 continue 

118 

119 if typ.c == base.C.LITERAL: 

120 out[typ.uid] = dict(literalValues=typ.literalValues) 

121 continue 

122 

123 if typ.c == base.C.EXT: 

124 continue 

125 

126 raise base.Error(f'unbound object {typ.c}: {typ.uid!r} in {typ.pos}')