-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
core.py
485 lines (359 loc) · 15.3 KB
/
core.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
"""Fastapi-mvc core implementations.
Attributes:
VERSION (str): Fastapi-mvc version.
ANSWERS_FILE (str): Relative path to copier answers file.
Resources:
1. `Click documentation`_
2. `Copier documentation`_
.. _Click documentation:
https://click.palletsprojects.com/en/8.1.x/
.. _Copier documentation:
https://copier.readthedocs.io/en/v6.2.0
"""
import os
from collections import defaultdict
import click
import copier
from copier.tools import Style, printf
from copier.user_data import load_answersfile_data
# CONSTANTS
VERSION = "0.19.0"
ANSWERS_FILE = ".fastapi-mvc.yml"
class ClickAliasedGroup(click.Group):
"""Custom click.Group class implementation.
Attributes:
aliases (typing.Dict[str, str]): Map of command aliases to their names.
Resources:
1. `click.Group class documentation`_
.. _click.Group class documentation:
https://click.palletsprojects.com/en/8.1.x/api/#click.Group
"""
def __init__(self, *args, **kwargs):
"""Initialize ClickAliasedGroup class object instance."""
super().__init__(*args, **kwargs)
self.aliases = dict()
def add_command(self, cmd, name=None):
"""Register another Command class object instance with this group.
If the name is not provided, the name of the command is used.
Args:
cmd (Command): Command class object instance to register.
name (typing.Optional[str]): Given command name.
"""
super().add_command(cmd, name)
name = name or cmd.name
if hasattr(cmd, "alias") and cmd.alias:
self.aliases[cmd.alias] = name
def get_command(self, ctx, cmd_name):
"""Return Command class object instance.
Given a context and a command name or alias, this returns a ``Command`` class
object instance if it exists.
Args:
ctx (click.Context): Click Context class object instance.
cmd_name (str): Chosen command name.
Returns:
Command: Class object instance for given command name.
"""
cmd_name = self.aliases.get(cmd_name, cmd_name)
return super().get_command(ctx, cmd_name)
def format_commands(self, ctx, formatter):
"""Write all the commands into the formatter if they exist.
Args:
ctx (click.Context): Click Context class object instance.
formatter (click.HelpFormatter): Click HelpFormatter class object instance.
"""
commands = []
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
if cmd is None:
continue
if cmd.hidden:
continue
if hasattr(cmd, "alias") and cmd.alias:
subcommand = f"{subcommand} ({cmd.alias})"
commands.append((subcommand, cmd))
if len(commands):
limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)
rows = []
for subcommand, cmd in commands:
help = cmd.get_short_help_str(limit)
rows.append((subcommand, help))
if rows:
with formatter.section("Commands"):
formatter.write_dl(rows)
class Command(click.Command):
"""Defines base class for all concrete fastapi-mvc CLI commands.
Args:
alias (typing.Optional[str]): Given command alias.
*args (list): Parent class constructor args.
**kwargs (dict): Parent class constructor kwargs.
Attributes:
project_data (typing.Optional[typing.Dict[str, typing.Any]): Map of copier
answers file questions to their parsed values.
alias (typing.Optional[str]): Given command alias.
Resources:
1. `click.Command class documentation`_
.. _click.Command class documentation:
https://click.palletsprojects.com/en/8.1.x/api/#click.Command
"""
def __init__(self, alias=None, *args, **kwargs):
"""Initialize Command class object instance."""
super().__init__(*args, **kwargs)
self.project_data = None
self.alias = alias
@property
def poetry_path(self):
"""Get Poetry binary abspath.
Returns:
str: Poetry binary abspath.
"""
if os.getenv("POETRY_BINARY"):
return os.getenv("POETRY_BINARY")
poetry_home = os.getenv(
"POETRY_HOME", f"{os.getenv('HOME')}/.local/share/pypoetry"
)
return f"{poetry_home}/venv/bin/poetry"
def ensure_project_data(self):
"""Ensure necessary project data existence.
Run ``copier.user_data.load_answersfile_data`` method, and ensure existence of
required values.
Raises:
SystemExit: If project data is empty or is missing required values.
"""
self.project_data = load_answersfile_data(
dst_path=os.getcwd(),
answers_file=ANSWERS_FILE,
)
if not self.project_data or "package_name" not in self.project_data:
click.secho(
"Not a fastapi-mvc project. Try 'fastapi-mvc new --help' for "
"details how to create one.",
fg="red",
err=True,
)
raise SystemExit(1)
class Generator(Command):
"""Defines base class for all concrete fastapi-mvc generators.
Args:
template (str): Copier template source. Can be repository URL or local path.
vcs_ref (typing.Optional[str]): The branch, tag or commit ID to checkout after
clone. Provided template is a repostiory URL.
category (str): Name under which generator should be printed in
``fastapi-mvc generate`` CLI command help page.
Attributes:
template (str): Copier template source. Can be repository URL or local path.
vcs_ref (typing.Optional[str]): The branch, tag or commit ID to checkout after
clone. Provided template is a repostiory URL.
category (str): Name under which generator should be printed in
``fastapi-mvc generate`` CLI command help page.
Resources:
1. `click.Command class documentation`_
.. _click.Command class documentation:
https://click.palletsprojects.com/en/8.1.x/api/#click.Command
"""
def __init__(self, template, vcs_ref=None, category="Other", *args, **kwargs):
"""Initialize Generator class object instance."""
super().__init__(*args, **kwargs)
self.template = template
self.vcs_ref = vcs_ref
self.category = category
def format_epilog(self, ctx, formatter):
"""Write the epilog into the formatter if it exists.
Args:
ctx (click.Context): Click Context class object instance.
formatter (click.HelpFormatter): Click HelpFormatter class object instance.
"""
if self.epilog:
formatter.write_paragraph()
formatter.write(self.epilog)
@staticmethod
def ensure_permissions(path, r=True, w=False, x=False):
"""Ensure correct permissions to given path.
Args:
path (str): Given path to check.
r (bool): Check read ok.
w (bool): Check write ok.
x (bool): Check executable ok.
Raises:
SystemExit: If path has insufficient permissions.
"""
if not os.path.exists(path):
click.secho(f"Path: '{path}' does not exist.")
raise SystemExit(1)
if r and not os.access(path, os.R_OK):
click.secho(f"Path: '{path}' is not readable.", fg="red", err=True)
raise SystemExit(1)
if w and not os.access(path, os.W_OK):
click.secho(f"Path: '{path}' is not writable.", fg="red", err=True)
raise SystemExit(1)
if x and not os.access(path, os.X_OK):
click.secho(f"Path: '{path}' is not executable.", fg="red", err=True)
raise SystemExit(1)
@staticmethod
def copier_printf(action, msg="", style=None, **kwargs):
"""Define wrapper for ``copier.printf`` method.
Args:
action (str): Given action value, ex: run, create, etc.
msg (str): Given messsage to print.
style (copier.tools.Style): Style to color action with.
"""
if style:
style = getattr(Style, style)
printf(
action=action,
msg=msg,
style=style,
**kwargs,
)
def run_auto(self, dst_path=".", data=None, answers_file=ANSWERS_FILE, **kwargs):
"""Define wrapper for ``copier.run_auto`` method.
Args:
dst_path (str | pathlib.Path): Destination path where to render the project.
data (typing.Optional[typing.Dict[str, typing.Any]): Answers to the
questionary defined in the template.
answers_file (str): Indicates the path for the answers file. The path must
be relative to dst_path.
"""
copier.run_auto(
src_path=self.template,
dst_path=dst_path,
vcs_ref=self.vcs_ref,
answers_file=answers_file,
data=data,
**kwargs,
)
def run_copy(self, dst_path=".", data=None, answers_file=ANSWERS_FILE, **kwargs):
"""Define wrapper for ``copier.run_copy`` method.
Args:
dst_path (str | pathlib.Path): Destination path where to render the project.
data (typing.Optional[typing.Dict[str, typing.Any]): Answers to the
questionary defined in the template.
answers_file (str): Indicates the path for the answers file. The path must
be relative to dst_path.
"""
copier.run_copy(
src_path=self.template,
dst_path=dst_path,
vcs_ref=self.vcs_ref,
answers_file=answers_file,
data=data,
**kwargs,
)
def run_update(self, dst_path=".", data=None, answers_file=ANSWERS_FILE, **kwargs):
"""Define wrapper for ``copier.run_update`` method.
Args:
dst_path (str | pathlib.Path): Destination path where to render the project.
data (typing.Optional[typing.Dict[str, typing.Any]): Answers to the
questionary defined in the template.
answers_file (str): Indicates the path for the answers file. The path must
be relative to dst_path.
"""
copier.run_update(
dst_path=dst_path,
vcs_ref=self.vcs_ref,
answers_file=answers_file,
data=data,
**kwargs,
)
def insert_router_import(self, controller_name):
"""Insert import and router entry into ``app/router.py`` file.
Args:
controller_name (str): Given controller name.
"""
package_name = self.project_data["package_name"]
router = os.path.join(os.getcwd(), f"{package_name}/app/router.py")
import_str = f"from {package_name}.app.controllers import {controller_name}\n"
with open(router, "r") as f:
lines = f.readlines()
if import_str in lines:
return
for i in range(len(lines)):
if lines[i].strip() == "from fastapi import APIRouter":
index = i + 1
break
else:
index = 0
lines.insert(index, import_str)
lines.append(f"root_api_router.include_router({controller_name}.router)\n")
with open(router, "w") as f:
f.writelines(lines)
class GeneratorsMultiCommand(click.MultiCommand):
"""Custom click.MultiCommand class implementation.
Args:
generators (typing.Dict[str, Generator]): Dictionary containing all available
fastapi-mvc generators.
generators_aliases (typing.Dict[str, str]): Map of generator aliases to their
names.
*args (list): Parent class constructor args.
**kwargs (dict): Parent class constructor kwargs.
Attributes:
generators (typing.Dict[str, Generator]): Dictionary containing all available
fastapi-mvc generators.
generators_aliases (typing.Dict[str, str]): Map of generator aliases to their
names.
Resources:
1. `click.MultiCommand class documentation`_
.. _click.MultiCommand class documentation:
https://click.palletsprojects.com/en/8.1.x/api/#click.MultiCommand
"""
def __init__(self, generators, alias=None, *args, **kwargs):
"""Initialize GeneratorsMultiCommand class object instance."""
super().__init__(*args, **kwargs)
self.generators = generators
self.generators_aliases = dict()
for name, gen in self.generators.items():
if hasattr(gen, "alias") and gen.alias:
self.generators_aliases[gen.alias] = name
self.alias = alias
def format_commands(self, ctx, formatter):
"""Write all the generators into the formatter if they exist.
Extra format methods for multi methods that adds all the generators after the
options.
Args:
ctx (click.Context): Click Context class object instance.
formatter (click.HelpFormatter): Click HelpFormatter class object instance.
"""
commands = []
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
if cmd.hidden:
continue
if hasattr(cmd, "alias") and cmd.alias:
subcommand = f"{subcommand} ({cmd.alias})"
commands.append((subcommand, cmd))
if commands:
limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)
rows = defaultdict(list)
for subcommand, cmd in commands:
help = cmd.get_short_help_str(limit)
category = getattr(cmd, "category", "Other")
rows[category].append((subcommand, help))
formatter.write_paragraph()
formatter.write("Please choose a generator below.")
formatter.write_paragraph()
with formatter.section("Builtins"):
formatter.write_dl(rows.pop("Builtins"))
for key, value in rows.items():
with formatter.section(key):
formatter.write_dl(value)
def list_commands(self, ctx):
"""Return a list of subcommand names in the order they should appear.
Args:
ctx (click.Context): Click Context class object instance.
Returns:
list: List of subcommand names in the order they should appear.
"""
return self.generators.keys()
def get_command(self, ctx, name):
"""Return GeneratorCommand class object instance.
Given a context and a command name or alias, this returns a ``GeneratorCommand``
class object instance if it exists or aborts the execution of the program.
Args:
ctx (click.Context): Click Context class object instance.
name (str): Chosen generator name.
Returns:
Generator: Class object instance for given command name.
"""
name = self.generators_aliases.get(name, name)
if name not in self.generators:
ctx.fail(f"No such generator '{name}'.")
return self.generators[name]