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