-
Notifications
You must be signed in to change notification settings - Fork 13
/
manage.py
281 lines (227 loc) · 7.6 KB
/
manage.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
# -*- coding: utf-8 -*-
import pkgutil
from os import path
from pathlib import Path
from sys import exit
from uuid import uuid4
import typer
from cookiecutter.main import cookiecutter
from loguru import logger
from string_utils import is_snake_case
CONSTANTS_FILE_PATH = Path(__file__).parent / "pipelines" / "constants.py"
CONSTANTS_FIND_AGENTS_TEXT = "M9w=k-b_\n"
COOKIECUTTER_PATH_AGENCY = Path(__file__).parent / "pipelines"
COOKIECUTTER_PATH_PROJECT = Path(__file__).parent / "pipelines" / "{{cookiecutter.project_name}}"
FLOWS_FILE_PATH = Path(__file__).parent / "pipelines" / "flows.py"
PIPELINES_PATH = Path(__file__).parent / "pipelines"
app = typer.Typer()
@logger.catch
def append_text(original_text: str, text_to_add: str, after_text: str = None):
"""
Appends text to a string after a given text.
:param original_text: The original text.
:param text_to_add: The text to add.
:param after_text: The text after which the text to add should be added.
:return: The original text with the text to add appended.
"""
if after_text is None:
return original_text + text_to_add
else:
return original_text.replace(after_text, after_text + text_to_add)
@logger.catch
def add_import(project_name: str):
"""
Adds an import to the project's flows file.
:param project_name: The project name.
:return: The original text with the import appended.
"""
with open(FLOWS_FILE_PATH, "r") as flows_file:
flows_text = flows_file.read()
flows_text = append_text(flows_text, f"from pipelines.{project_name}.flows import *\n")
with open(FLOWS_FILE_PATH, "w") as flows_file:
flows_file.write(flows_text)
@logger.catch
def add_agency_import(agency_name: str, project_name: str):
"""
Adds an import to the agency's __init__ file.
:param agency_name: The agency name.
:param project_name: The project name.
:return: The original text with the import appended.
"""
path = PIPELINES_PATH / agency_name / "__init__.py"
with open(path, "r") as flows_file:
flows_text = flows_file.read()
flows_text = append_text(
flows_text, f"from pipelines.{agency_name}.{project_name}.flows import *\n"
)
with open(path, "w") as flows_file:
flows_file.write(flows_text)
@logger.catch
def add_agent(project_name: str):
"""
Adds the agent to the constants file.
:param project_name: The name of the project.
"""
with open(CONSTANTS_FILE_PATH, "r") as file:
file_text = file.read()
file_text = append_text(
file_text,
f' {project_name.upper()}_AGENT_LABEL = "{uuid4()}"\n',
CONSTANTS_FIND_AGENTS_TEXT,
)
with open(CONSTANTS_FILE_PATH, "w") as file:
file.write(file_text)
@logger.catch
def cut_agency_cookie(agency_name: str):
"""
Runs the cookiecutter command.
:param project_name: The name of the project.
"""
cookiecutter(
str(COOKIECUTTER_PATH_AGENCY),
no_input=True,
extra_context={
"project_name": agency_name,
"workspace_name": "example",
},
output_dir=str(COOKIECUTTER_PATH_AGENCY),
)
delete_file(COOKIECUTTER_PATH_AGENCY / agency_name / "cookiecutter.json")
@logger.catch
def cut_project_cookie(agency_name: str, project_name: str):
"""
Runs the cookiecutter command.
:param project_name: The name of the project.
"""
cookiecutter(
str(COOKIECUTTER_PATH_PROJECT),
no_input=True,
extra_context={
"project_name": agency_name,
"workspace_name": project_name,
},
output_dir=str(COOKIECUTTER_PATH_AGENCY / agency_name),
)
@logger.catch
def delete_file(fname: str):
"""
Deletes a file.
:param fname: The name of the file.
"""
if path.exists(fname):
Path(fname).unlink()
@logger.catch
def single_word_valid(input: str) -> bool:
"""
- Must have only letters and numbers
- Must not start with a number
"""
return input.isalnum() and not input[0].isdigit()
@logger.catch
def name_already_exists(input: str) -> bool:
"""
Checks if the name already exists.
:param input: The input.
:return: True if the name already exists, False otherwise.
"""
try:
import pipelines
except ImportError:
logger.error("Não foi possível importar as pipelines.")
exit(1)
pkgpath = path.dirname(pipelines.__file__)
return input in [name for _, name, _ in pkgutil.iter_modules([pkgpath])]
@logger.catch
def check_name(input: str) -> str:
"""
Checks if the name is valid.
:param input: The input.
:return: The input if it is valid, otherwise an error message.
"""
# Must be either valid as a single word or a snake case name
valid = single_word_valid(input) or is_snake_case(input)
# Also, must not already exist
valid = valid and not name_already_exists(input)
if valid:
return input
else:
logger.error(f"O nome {input} é inválido. Ele deve estar em snake_case e ser único")
exit(1)
@logger.catch
def agency_must_exist(input: str) -> str:
"""
Checks whether the agency exists.
:param input: The input.
:return: The input if it exists, otherwise an error message.
"""
try:
import pipelines
except ImportError:
logger.error("Não foi possível importar as pipelines.")
exit(1)
pkgpath = path.dirname(pipelines.__file__)
agencies = [name for _, name, _ in pkgutil.iter_modules([pkgpath])]
if input in agencies:
return input
else:
logger.error(f"A agência {input} não existe.")
exit(1)
@logger.catch
def project_must_not_exist_in_agency(input: str, agency: str) -> str:
"""
Checks whether the project exists in the agency.
:param input: The input.
:param agency: The agency.
:return: The input if it exists, otherwise an error message.
"""
try:
import pipelines
except ImportError:
logger.error("Não foi possível importar as pipelines.")
exit(1)
pkgpath = path.dirname(pipelines.__file__)
agencies = [name for _, name, _ in pkgutil.iter_modules([pkgpath])]
if agency in agencies:
projects = [name for _, name, _ in pkgutil.iter_modules([str(Path(pkgpath) / agency)])]
if input in projects:
logger.error(f"O projeto {input} já existe na agência {agency}.")
exit(1)
return input
else:
logger.error(f"O órgão {agency} não foi encontrado.")
exit(1)
@app.command()
def add_agency(agency_name: str = typer.Argument(..., callback=check_name)):
"""
Cria um novo órgão usando o cookiecutter.
"""
add_agent(agency_name)
add_import(agency_name)
cut_agency_cookie(agency_name)
@app.command()
def add_project(
agency_name: str = typer.Argument(..., callback=agency_must_exist),
project_name: str = typer.Argument(...),
):
"""
Cria um novo projeto no órgão usando o cookiecutter.
"""
project_must_not_exist_in_agency(project_name, agency_name)
add_agency_import(agency_name, project_name)
cut_project_cookie(agency_name, project_name)
@app.command()
def list_projects():
"""
Lista os projetos cadastrados e nomes reservados
"""
try:
import pipelines
except ImportError:
logger.error("Não foi possível importar as pipelines.")
exit(1)
pkgpath = path.dirname(pipelines.__file__)
logger.info("Projetos cadastrados e nomes reservados:")
for _, name, _ in pkgutil.iter_modules([pkgpath]):
logger.info(f"- {name}")
if __name__ == "__main__":
app()