-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_models.py
246 lines (209 loc) · 7.58 KB
/
gen_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import subprocess
from pathlib import Path
from venv import logger
import httpx
from httpx import HTTPStatusError, RequestError
from rich.logging import RichHandler
from rich.traceback import install
# Replace the basic logging config with Rich handler
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[RichHandler(rich_tracebacks=True, markup=True)],
)
logging.getLogger("httpx").setLevel(logging.INFO)
install(show_locals=False)
logger = logging.getLogger(__name__)
class OpenAPIDiscovery:
def __init__(
self,
input_dir: str,
output_dir: str,
version: str,
verbose: bool,
use_cache: bool,
):
self.base_url = "https://api.github.com/repos/eda-labs/openapi"
self.raw_base = f"https://raw.githubusercontent.com/eda-labs/openapi/{version}"
self.gh_token = os.environ.get("GH_AUTH_TOKEN")
if self.gh_token == "":
logger.error("GH_AUTH_TOKEN environment variable is not set.")
exit(1)
self.headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": f"Bearer {self.gh_token}",
}
self.cache_file = Path("cached_specs.json")
self.input_dir = Path(input_dir)
self.output_dir = Path(output_dir)
self.verbose = verbose
self.use_cache = use_cache
self.version = version
if self.verbose:
logger.setLevel(logging.DEBUG)
# Configure retry strategy with httpx
transport = httpx.HTTPTransport(
retries=3,
)
self.client = httpx.Client(
timeout=30.0,
transport=transport,
headers=self.headers,
follow_redirects=True,
)
def get_contents(self, path=""):
url = f"{self.base_url}/contents/{path}"
logger.info(f"Fetching contents from: {url}")
try:
response = self.client.get(url)
response.raise_for_status()
# Rate limiting info
remaining = response.headers.get("X-RateLimit-Remaining")
if remaining:
logger.info(f"API calls remaining: {remaining}")
content = response.json()
if isinstance(content, str):
raise ValueError(f"Unexpected response format: {content}")
return content
except HTTPStatusError as e:
logger.error(f"HTTP error occurred: {e}")
raise
except RequestError as e:
logger.error(f"Request error occurred: {e}")
raise
def load_cached_specs(self) -> dict[str, list[dict[str, str]]]:
try:
if self.cache_file.exists() and self.cache_file.stat().st_size > 0:
with open(self.cache_file) as f:
cached = json.load(f)
return cached
except (json.JSONDecodeError, OSError) as e:
logger.warning(f"Cache read error: {e}")
return {"": []}
def save_specs_cache(self, specs):
try:
with open(self.cache_file, "w") as f:
json.dump(specs, f, indent=2)
except OSError as e:
logger.error(f"Failed to save cache: {e}")
def discover_specs(self, use_cache=True) -> dict[str, list[dict[str, str]]]:
if use_cache:
cached = self.load_cached_specs()
if cached:
logger.info("Using cached specs")
return cached
specs = {self.version: []}
specs_for_version = specs[self.version]
# Check core path
core_specs = self.get_contents("core")
for spec in core_specs:
if spec.get("type") == "file" and spec.get("name", "").endswith(".json"):
specs_for_version.append(
{
"name": Path(spec["name"]).stem,
"url": f"{self.raw_base}/core/{spec['name']}",
}
)
# Check apps path
apps = self.get_contents("apps")
for app in apps:
if app.get("type") == "dir":
versions = self.get_contents(app["path"])
for version in versions:
if version.get("type") == "dir":
files = self.get_contents(version["path"])
for file in files:
if file.get("name", "").endswith(".json"):
specs_for_version.append(
{
"name": app["name"].split(".")[0],
"url": f"{self.raw_base}/{file['path']}",
}
)
if specs:
self.save_specs_cache(specs)
return specs
def generate_models(self):
output_dir = self.output_dir
output_dir.mkdir(exist_ok=True)
specs: dict[str, list[dict[str, str]]] = self.discover_specs(
use_cache=self.use_cache
)
if not specs:
logger.warning("No specs found!")
return
if self.version not in specs:
logger.warning(f"No specs found for version {self.version}")
return
for spec in specs[self.version]:
url_parts = spec["url"].split("/")
module_name = url_parts[-1].replace(".json", "")
cmd = [
"datamodel-codegen",
"--url",
spec["url"],
"--output-model-type",
"pydantic_v2.BaseModel",
"--use-annotated",
"--enum-field-as-literal",
"all",
"--output",
str(output_dir),
]
# the core API should use the file name as the output of the DMCG command
# the core app does not have apps dir in its URL
if "apps" not in url_parts and module_name == "core":
cmd[-1] = str(output_dir) + "/core.py"
try:
logger.info(f"Generating models for {module_name}...")
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
logger.error(f"Error generating models for {module_name}: {e}")
def __del__(self):
self.client.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Discover OpenAPI specifications and generate Pydantic models."
)
parser.add_argument(
"--input",
type=str,
default="./openapi_specs",
help="Path to the OpenAPI specifications directory. Default: ./openapi_specs",
)
parser.add_argument(
"--output",
type=str,
default="./pydantic_eda",
help="Path to the output directory.",
)
parser.add_argument(
"--version",
type=str,
default="main",
help="openapi repo version (tag) to get the models from. Default: main",
)
parser.add_argument(
"--verbose",
action="store_true",
help="Enable verbose logging. Default: False",
)
parser.add_argument(
"--no-cache",
action="store_true",
help="Force fresh discovery by ignoring the cache. Default: False",
)
args = parser.parse_args()
discovery = OpenAPIDiscovery(
input_dir=args.input,
output_dir=args.output,
version=args.version,
verbose=args.verbose,
use_cache=not args.no_cache,
)
discovery.generate_models()