forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
120 lines (102 loc) · 3.88 KB
/
config.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
import json
import mimetypes
from enum import Enum
from pathlib import Path
from typing import Dict, List, Optional, Union
from attr import define
from pydantic import BaseModel
from ruamel.yaml import YAML
class ClassOverride(BaseModel):
"""An override of a single generated class.
See https://github.com/openapi-generators/openapi-python-client#class_overrides
"""
class_name: Optional[str] = None
module_name: Optional[str] = None
class MetaType(str, Enum):
"""The types of metadata supported for project generation."""
NONE = "none"
POETRY = "poetry"
SETUP = "setup"
PDM = "pdm"
class ConfigFile(BaseModel):
"""Contains any configurable values passed via a config file.
See https://github.com/openapi-generators/openapi-python-client#configuration
"""
class_overrides: Optional[Dict[str, ClassOverride]] = None
content_type_overrides: Optional[Dict[str, str]] = None
project_name_override: Optional[str] = None
package_name_override: Optional[str] = None
package_version_override: Optional[str] = None
use_path_prefixes_for_title_model_names: bool = True
post_hooks: Optional[List[str]] = None
field_prefix: str = "field_"
http_timeout: int = 5
case_sensitive_enums: bool = False
@staticmethod
def load_from_path(path: Path) -> "ConfigFile":
"""Creates a Config from provided JSON or YAML file and sets a bunch of globals from it"""
mime = mimetypes.guess_type(path.absolute().as_uri(), strict=True)[0]
if mime == "application/json":
config_data = json.loads(path.read_text())
else:
yaml = YAML(typ="safe")
config_data = yaml.load(path)
config = ConfigFile(**config_data)
return config
@define
class Config:
"""Contains all the config values for the generator, from files, defaults, and CLI arguments."""
meta_type: MetaType
class_overrides: Dict[str, ClassOverride]
project_name_override: Optional[str]
package_name_override: Optional[str]
package_version_override: Optional[str]
use_path_prefixes_for_title_model_names: bool
post_hooks: List[str]
field_prefix: str
http_timeout: int
document_source: Union[Path, str]
file_encoding: str
content_type_overrides: Dict[str, str]
overwrite: bool
output_path: Optional[Path]
case_sensitive_enums: bool
@staticmethod
def from_sources(
config_file: ConfigFile,
meta_type: MetaType,
document_source: Union[Path, str],
file_encoding: str,
overwrite: bool,
output_path: Optional[Path],
) -> "Config":
if config_file.post_hooks is not None:
post_hooks = config_file.post_hooks
elif meta_type == MetaType.NONE:
post_hooks = [
"ruff check . --fix --extend-select=I",
"ruff format .",
]
else:
post_hooks = [
"ruff check --fix .",
"ruff format .",
]
config = Config(
meta_type=meta_type,
class_overrides=config_file.class_overrides or {},
content_type_overrides=config_file.content_type_overrides or {},
project_name_override=config_file.project_name_override,
package_name_override=config_file.package_name_override,
package_version_override=config_file.package_version_override,
use_path_prefixes_for_title_model_names=config_file.use_path_prefixes_for_title_model_names,
post_hooks=post_hooks,
field_prefix=config_file.field_prefix,
http_timeout=config_file.http_timeout,
document_source=document_source,
file_encoding=file_encoding,
overwrite=overwrite,
output_path=output_path,
case_sensitive_enums=config_file.case_sensitive_enums,
)
return config