3
3
import shutil
4
4
import subprocess
5
5
import sys
6
+ from enum import Enum
6
7
from pathlib import Path
7
8
from typing import Any , Dict , Optional , Sequence , Union
8
9
25
26
__version__ = version (__package__ )
26
27
27
28
29
+ class MetaType (str , Enum ):
30
+ NONE = "none"
31
+ POETRY = "poetry"
32
+ SETUP = "setup"
33
+
34
+
28
35
class Project :
29
36
TEMPLATE_FILTERS = {"snakecase" : utils .snake_case , "kebabcase" : utils .kebab_case , "pascalcase" : utils .pascal_case }
30
37
project_name_override : Optional [str ] = None
31
38
package_name_override : Optional [str ] = None
32
39
package_version_override : Optional [str ] = None
33
40
34
- def __init__ (self , * , openapi : GeneratorData , custom_template_path : Optional [Path ] = None ) -> None :
41
+ def __init__ (self , * , openapi : GeneratorData , meta : MetaType , custom_template_path : Optional [Path ] = None ) -> None :
35
42
self .openapi : GeneratorData = openapi
43
+ self .meta : MetaType = meta
36
44
37
45
package_loader = PackageLoader (__package__ )
38
46
loader : BaseLoader
@@ -48,7 +56,9 @@ def __init__(self, *, openapi: GeneratorData, custom_template_path: Optional[Pat
48
56
self .env : Environment = Environment (loader = loader , trim_blocks = True , lstrip_blocks = True )
49
57
50
58
self .project_name : str = self .project_name_override or f"{ utils .kebab_case (openapi .title ).lower ()} -client"
51
- self .project_dir : Path = Path .cwd () / self .project_name
59
+ self .project_dir : Path = Path .cwd ()
60
+ if meta != MetaType .NONE :
61
+ self .project_dir /= self .project_name
52
62
53
63
self .package_name : str = self .package_name_override or self .project_name .replace ("-" , "_" )
54
64
self .package_dir : Path = self .project_dir / self .package_name
@@ -62,11 +72,14 @@ def __init__(self, *, openapi: GeneratorData, custom_template_path: Optional[Pat
62
72
def build (self ) -> Sequence [GeneratorError ]:
63
73
""" Create the project from templates """
64
74
65
- print (f"Generating { self .project_name } " )
66
- try :
67
- self .project_dir .mkdir ()
68
- except FileExistsError :
69
- return [GeneratorError (detail = "Directory already exists. Delete it or use the update command." )]
75
+ if self .meta == MetaType .NONE :
76
+ print (f"Generating { self .package_name } " )
77
+ else :
78
+ print (f"Generating { self .project_name } " )
79
+ try :
80
+ self .project_dir .mkdir ()
81
+ except FileExistsError :
82
+ return [GeneratorError (detail = "Directory already exists. Delete it or use the update command." )]
70
83
self ._create_package ()
71
84
self ._build_metadata ()
72
85
self ._build_models ()
@@ -79,7 +92,7 @@ def update(self) -> Sequence[GeneratorError]:
79
92
80
93
if not self .package_dir .is_dir ():
81
94
raise FileNotFoundError ()
82
- print (f"Updating { self .project_name } " )
95
+ print (f"Updating { self .package_name } " )
83
96
shutil .rmtree (self .package_dir )
84
97
self ._create_package ()
85
98
self ._build_models ()
@@ -119,25 +132,21 @@ def _create_package(self) -> None:
119
132
package_init_template = self .env .get_template ("package_init.pyi" )
120
133
package_init .write_text (package_init_template .render (description = self .package_description ))
121
134
122
- pytyped = self .package_dir / "py.typed"
123
- pytyped .write_text ("# Marker file for PEP 561" )
135
+ if self .meta != MetaType .NONE :
136
+ pytyped = self .package_dir / "py.typed"
137
+ pytyped .write_text ("# Marker file for PEP 561" )
124
138
125
139
types_template = self .env .get_template ("types.py" )
126
140
types_path = self .package_dir / "types.py"
127
141
types_path .write_text (types_template .render ())
128
142
129
143
def _build_metadata (self ) -> None :
130
- # Create a pyproject.toml file
131
- pyproject_template = self .env .get_template ("pyproject.toml" )
132
- pyproject_path = self .project_dir / "pyproject.toml"
133
- pyproject_path .write_text (
134
- pyproject_template .render (
135
- project_name = self .project_name ,
136
- package_name = self .package_name ,
137
- version = self .version ,
138
- description = self .package_description ,
139
- )
140
- )
144
+ if self .meta == MetaType .NONE :
145
+ return
146
+
147
+ self ._build_pyproject_toml (use_poetry = self .meta == MetaType .POETRY )
148
+ if self .meta == MetaType .SETUP :
149
+ self ._build_setup_py ()
141
150
142
151
# README.md
143
152
readme = self .project_dir / "README.md"
@@ -153,6 +162,31 @@ def _build_metadata(self) -> None:
153
162
git_ignore_template = self .env .get_template (".gitignore" )
154
163
git_ignore_path .write_text (git_ignore_template .render ())
155
164
165
+ def _build_pyproject_toml (self , * , use_poetry : bool ) -> None :
166
+ template = "pyproject.toml" if use_poetry else "pyproject_no_poetry.toml"
167
+ pyproject_template = self .env .get_template (template )
168
+ pyproject_path = self .project_dir / "pyproject.toml"
169
+ pyproject_path .write_text (
170
+ pyproject_template .render (
171
+ project_name = self .project_name ,
172
+ package_name = self .package_name ,
173
+ version = self .version ,
174
+ description = self .package_description ,
175
+ )
176
+ )
177
+
178
+ def _build_setup_py (self ) -> None :
179
+ template = self .env .get_template ("setup.py" )
180
+ path = self .project_dir / "setup.py"
181
+ path .write_text (
182
+ template .render (
183
+ project_name = self .project_name ,
184
+ package_name = self .package_name ,
185
+ version = self .version ,
186
+ description = self .package_description ,
187
+ )
188
+ )
189
+
156
190
def _build_models (self ) -> None :
157
191
# Generate models
158
192
models_dir = self .package_dir / "models"
@@ -205,42 +239,42 @@ def _build_api(self) -> None:
205
239
206
240
207
241
def _get_project_for_url_or_path (
208
- url : Optional [str ], path : Optional [Path ], custom_template_path : Optional [Path ] = None
242
+ url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
209
243
) -> Union [Project , GeneratorError ]:
210
244
data_dict = _get_document (url = url , path = path )
211
245
if isinstance (data_dict , GeneratorError ):
212
246
return data_dict
213
247
openapi = GeneratorData .from_dict (data_dict )
214
248
if isinstance (openapi , GeneratorError ):
215
249
return openapi
216
- return Project (openapi = openapi , custom_template_path = custom_template_path )
250
+ return Project (openapi = openapi , custom_template_path = custom_template_path , meta = meta )
217
251
218
252
219
253
def create_new_client (
220
- * , url : Optional [str ], path : Optional [Path ], custom_template_path : Optional [Path ] = None
254
+ * , url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
221
255
) -> Sequence [GeneratorError ]:
222
256
"""
223
257
Generate the client library
224
258
225
259
Returns:
226
260
A list containing any errors encountered when generating.
227
261
"""
228
- project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path )
262
+ project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path , meta = meta )
229
263
if isinstance (project , GeneratorError ):
230
264
return [project ]
231
265
return project .build ()
232
266
233
267
234
268
def update_existing_client (
235
- * , url : Optional [str ], path : Optional [Path ], custom_template_path : Optional [Path ] = None
269
+ * , url : Optional [str ], path : Optional [Path ], meta : MetaType , custom_template_path : Optional [Path ] = None
236
270
) -> Sequence [GeneratorError ]:
237
271
"""
238
272
Update an existing client library
239
273
240
274
Returns:
241
275
A list containing any errors encountered when generating.
242
276
"""
243
- project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path )
277
+ project = _get_project_for_url_or_path (url = url , path = path , custom_template_path = custom_template_path , meta = meta )
244
278
if isinstance (project , GeneratorError ):
245
279
return [project ]
246
280
return project .update ()
0 commit comments