ominfra 0.0.0.dev199__py3-none-any.whl → 0.0.0.dev201__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,101 @@
1
+ import dataclasses as dc
2
+ import typing as ta
3
+
4
+ from omlish import check
5
+ from omlish import lang
6
+
7
+
8
+ DateTime = ta.NewType('DateTime', str)
9
+ MillisecondDateTime = ta.NewType('MillisecondDateTime', str)
10
+
11
+ Timestamp = ta.NewType('Timestamp', str)
12
+
13
+
14
+ ##
15
+
16
+
17
+ class TagList:
18
+ pass
19
+
20
+
21
+ ##
22
+
23
+
24
+ class MEMBER_NAME(lang.Marker): # noqa
25
+ pass
26
+
27
+
28
+ class SHAPE_NAME(lang.Marker): # noqa
29
+ pass
30
+
31
+
32
+ ##
33
+
34
+
35
+ def common_metadata(
36
+ *,
37
+ shape_name: str | None = None,
38
+ ) -> dict[ta.Any, ta.Any]:
39
+ md = {}
40
+
41
+ if shape_name is not None:
42
+ md[SHAPE_NAME] = shape_name
43
+
44
+ return md
45
+
46
+
47
+ ##
48
+
49
+
50
+ def shape_metadata(
51
+ **kwargs: ta.Any,
52
+ ) -> dict[ta.Any, ta.Any]:
53
+ md = {**common_metadata(**kwargs)}
54
+
55
+ return md
56
+
57
+
58
+ @dc.dataclass(frozen=True)
59
+ class Shape:
60
+ __shape_metadata__: ta.ClassVar[ta.Mapping[ta.Any, ta.Any]]
61
+
62
+ def __init_subclass__(
63
+ cls,
64
+ *,
65
+ shape_name: str | None = None,
66
+ **kwargs: ta.Any,
67
+ ) -> None:
68
+ super().__init_subclass__(**kwargs)
69
+
70
+ check.state(not hasattr(cls, '__shape_metadata__'))
71
+
72
+ cls.__shape_metadata__ = shape_metadata(**kwargs)
73
+
74
+
75
+ ##
76
+
77
+
78
+ def field_metadata(
79
+ *,
80
+ member_name: str | None = None,
81
+ **kwargs: ta.Any,
82
+ ) -> dict[ta.Any, ta.Any]:
83
+ md = {**common_metadata(**kwargs)}
84
+
85
+ if member_name is not None:
86
+ md[MEMBER_NAME] = member_name
87
+
88
+ return md
89
+
90
+
91
+ ##
92
+
93
+
94
+ @dc.dataclass(frozen=True, eq=False, kw_only=True)
95
+ class Operation:
96
+ name: str
97
+
98
+ input: type[Shape] | None = None
99
+ output: type[Shape] | None = None
100
+
101
+ errors: ta.Sequence[type[Shape]] | None = None
File without changes
@@ -0,0 +1,4 @@
1
+ if __name__ == '__main__':
2
+ from .cli import _main # noqa
3
+
4
+ _main()
@@ -0,0 +1,139 @@
1
+ import dataclasses as dc
2
+ import keyword
3
+ import os.path
4
+ import sys
5
+ import typing as ta
6
+
7
+ from omlish import lang
8
+ from omlish import marshal as msh
9
+ from omlish.argparse import all as ap
10
+ from omlish.configs import all as configs
11
+
12
+ from .gen import ModelGen
13
+
14
+
15
+ ##
16
+
17
+
18
+ class Cli(ap.Cli):
19
+ def _arg_list(self, s: ta.Iterable[str] | None) -> list[str]:
20
+ return list(lang.flatten(e.split(',') for e in s or []))
21
+
22
+ #
23
+
24
+ def _gen_module(
25
+ self,
26
+ service_name: str,
27
+ *,
28
+ shape_names: ta.Iterable[str] | None = None,
29
+ operation_names: ta.Iterable[str] | None = None,
30
+ ) -> str:
31
+ service_model = ModelGen.load_service_model(service_name)
32
+
33
+ bmg = ModelGen(
34
+ service_model,
35
+ shape_names=ModelGen.get_referenced_shape_names(
36
+ service_model,
37
+ shape_names=shape_names or (),
38
+ operation_names=operation_names or (),
39
+ ),
40
+ operation_names=operation_names or (),
41
+ )
42
+
43
+ return bmg.gen_module()
44
+
45
+ @ap.cmd(
46
+ ap.arg('service'),
47
+ ap.arg('-s', '--shape', action='append'),
48
+ ap.arg('-o', '--operation', action='append'),
49
+ )
50
+ def gen_module(self) -> None:
51
+ mod = self._gen_module(
52
+ self.args.service,
53
+ shape_names=self._arg_list(self.args.shape),
54
+ operation_names=self._arg_list(self.args.operation),
55
+ )
56
+
57
+ sys.stdout.write(mod)
58
+
59
+ #
60
+
61
+ @dc.dataclass(frozen=True)
62
+ class ServicesConfig:
63
+ @dc.dataclass(frozen=True)
64
+ class Service:
65
+ name: str
66
+ shapes: ta.Sequence[str] | None = None
67
+ operations: ta.Sequence[str] | None = None
68
+
69
+ services: ta.Sequence[Service] | None = None
70
+
71
+ def _gen_service(
72
+ self,
73
+ svc: ServicesConfig.Service,
74
+ output_dir: str,
75
+ ) -> None:
76
+ mod = self._gen_module(
77
+ svc.name,
78
+ shape_names=svc.shapes,
79
+ operation_names=svc.operations,
80
+ )
81
+
82
+ fn = svc.name
83
+ if fn in keyword.kwlist:
84
+ fn += '_'
85
+ output_file = os.path.join(output_dir, f'{fn}.py')
86
+ with open(output_file, 'w') as f:
87
+ f.write(mod)
88
+
89
+ @ap.cmd(
90
+ ap.arg('config-file', nargs='?'),
91
+ )
92
+ def gen_services(self) -> None:
93
+ config_file = self.args.config_file
94
+ if config_file is None:
95
+ config_file = os.path.abspath(os.path.join(os.path.dirname(__file__), '../services/services.toml'))
96
+
97
+ cfg_dct = dict(configs.DEFAULT_FILE_LOADER.load_file(config_file).as_map())
98
+ cfg_dct = configs.processing.matched_rewrite(
99
+ configs.processing.build_named_children,
100
+ cfg_dct,
101
+ ('services',),
102
+ )
103
+ cfg: Cli.ServicesConfig = msh.unmarshal(cfg_dct, Cli.ServicesConfig)
104
+
105
+ output_dir = os.path.dirname(os.path.abspath(config_file))
106
+
107
+ for svc in cfg.services or []:
108
+ self._gen_service(svc, output_dir)
109
+
110
+ #
111
+
112
+ @ap.cmd()
113
+ def list_services(self) -> None:
114
+ for name in sorted(ModelGen.list_available_services()):
115
+ print(name)
116
+
117
+ @ap.cmd(
118
+ ap.arg('service'),
119
+ )
120
+ def list_shapes(self) -> None:
121
+ service_model = ModelGen.load_service_model(self.args.service)
122
+ for name in sorted(service_model.shape_names):
123
+ print(name)
124
+
125
+ @ap.cmd(
126
+ ap.arg('service'),
127
+ )
128
+ def list_operations(self) -> None:
129
+ service_model = ModelGen.load_service_model(self.args.service)
130
+ for name in sorted(service_model.operation_names):
131
+ print(name)
132
+
133
+
134
+ def _main() -> None:
135
+ Cli().cli_run_and_exit()
136
+
137
+
138
+ if __name__ == '__main__':
139
+ _main()