-
Notifications
You must be signed in to change notification settings - Fork 106
/
models.py
191 lines (153 loc) · 6.62 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""This helps parse out the workflow models from the OpenAPI schema and creates an init only for those models."""
# we want the init of `workflows.models` to have a filtered import of Workflow models
# we can parse out the JSON using the old code and filter on Workflow objects
import os
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional
import requests
model_types = {"workflows", "events"}
@dataclass(frozen=True, order=True)
class ImportReference:
path: str
name: str
alias: Optional[str] = None
@property
def rendered_import(self) -> str:
name = self.name
if self.alias:
name = f"{self.name} as {self.alias}"
return f"from {self.path} import {name}\n"
@property
def aliased_name(self) -> str:
if self.alias:
return self.alias
return self.name
def get_openapi_spec_url() -> str:
"""Gets the OpenAPI spec URL from argv and returns it."""
assert len(sys.argv) == 3, "Expected two argv arguments - the Argo OpenAPI spec URL and [workflows|events]"
return sys.argv[1]
def get_models_type() -> str:
"""Gets the model type to generate from argv and returns it. This is either `workflows` or `events`."""
assert len(sys.argv) == 3, "Expected two argv arguments - the Argo OpenAPI spec URL and [workflows|events]"
arg = sys.argv[2]
assert arg in model_types, f"Unsupported model type {arg}, expected one of {model_types}"
return arg
def fetch_openapi_spec(url: str) -> dict:
"""Fetches the OpenAPI specification at the given URI."""
response = requests.get(url)
if response.ok:
return response.json()
raise ValueError(
f"Did not receive an ok response from fetching the OpenAPI spec payload from url {url}, "
f"status {response.status_code}"
)
def get_openapi_definitions(spec: dict) -> dict:
"""Extracts the OpenAPI definitions from the given OpenAPI spec."""
assert "definitions" in spec, "Expected `definitions` to be part of the spec"
return spec["definitions"]
def get_refs_from_openapi_definitions(defs: dict) -> list:
"""Extracts the OpenAPI references from the given OpenAPI definitions."""
return list(defs.keys())
def filter_refs_on_models_type(refs: list, models_type: str) -> list:
"""Filters the given list of references using the given model type."""
# TODO: maybe this logic can be a bit more sophisticated
inclusions = ["events", "event", "eventsource", "sensor"]
filtered = refs
events = []
for inclusion in inclusions:
if models_type == "events":
for ref in refs:
if inclusion in ref:
events.append(ref)
else:
exclusions = [
"io.k8s.api.core.v1.Event",
"io.k8s.api.core.v1.EventSeries",
"io.k8s.api.core.v1.EventSource",
]
filtered = list(filter(lambda r: inclusion not in r and r not in exclusions, filtered))
if models_type == "events":
return list(set(events))
return list(set(filtered))
def assemble_root_path_from_models_type(m_type: str) -> str:
"""Assembles the Hera root path from the given models type."""
return f"hera.{m_type}.models"
def get_import_paths_from_refs(refs: list, root_path: str) -> List[ImportReference]:
"""Assembles the Hera import paths from the given list of references."""
result = []
for ref in refs:
split = ref.split(".")
path, obj = ".".join(split[:-1]), split[-1]
# these are duplicate objects and cause collisions, so we use extra flags from the import path to denote
# the differences between them, which helps users avoid import errors
alias = None
if obj in ["HTTPHeader", "LogEntry"]:
raw_alias = f"{split[-2]}{obj}"
alias = raw_alias[0].upper() + raw_alias[1:]
result.append(ImportReference(f"{root_path}.{path}", obj, alias=alias))
return result
def write_imports(references: List[ImportReference], models_type: str, openapi_spec_url: str) -> None:
"""Writes the given imports to the `root_path` models."""
path = Path(__name__).parent.parent / "src" / "hera" / models_type / "models" / "__init__.py"
with open(str(path), "w+") as f:
f.write(
f'"""[DO NOT EDIT MANUALLY] Auto-generated model classes.\n\n'
f"Auto-generated by Hera via `make {models_type}-models`.\n"
f"OpenAPI spec URL: {openapi_spec_url}\n"
f'"""\n\n'
)
if models_type == "events":
events = [
"Item",
"Event",
"EventResponse",
"GetUserInfoResponse",
"InfoResponse",
"Version",
]
for event in events:
references.append(ImportReference("hera.events.models.io.argoproj.workflow.v1alpha1", event))
enums = [
"ImagePullPolicy",
"TerminationMessagePolicy",
"Protocol",
"Scheme",
"Operator",
"Type",
"Phase",
"TypeModel",
"Effect",
"OperatorModel",
]
for enum in enums:
references.append(ImportReference(f"hera.{models_type}.models.io.k8s.api.core.v1", enum))
for ref in sorted(references):
f.write(ref.rendered_import)
models_strs = sorted([f'"{ref.aliased_name}"' for ref in references])
models_str = ", ".join(models_strs)
f.write(f"\n__all__ = [{models_str}]\n")
def ensure_init():
"""Ensure that an init file is present in every folder.
This executes recursively inside the hera/<models_type>/models folder to ensure that stubgen worked.
"""
for models_type in model_types:
for root, _, files in os.walk(f"src/hera/{models_type}/models"):
# ignore __pycache__ folders
if "__pycache__" in root:
continue
if "__init__.py" not in files:
with open(f"{root}/__init__.py", "w") as f:
f.write("")
if __name__ == "__main__":
models_type = get_models_type()
openapi_spec_url = get_openapi_spec_url()
openapi_spec = fetch_openapi_spec(openapi_spec_url)
definitions = get_openapi_definitions(openapi_spec)
refs = get_refs_from_openapi_definitions(definitions)
filtered_refs = filter_refs_on_models_type(refs, models_type)
root_path = assemble_root_path_from_models_type(models_type)
imports = get_import_paths_from_refs(filtered_refs, root_path)
write_imports(imports, models_type, openapi_spec_url)
ensure_init()