-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.py
executable file
·187 lines (154 loc) · 5.86 KB
/
clean.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
"""Init script to create a Clean Architecrure environment."""
import os
import argparse
# CLEAN_FEAT_STRUCTURE = ['domains', 'applications', 'interfaces', 'providers']
TEST_ENV = {
'path': {'root': 'tests', 'subs': ['unit', 'functional']}
}
CLEAN_FEAT_STRUCTURE = {
'root_path': {
'init': {
'docstring': '"""This is the init for __ROOT__."""',
},
},
'structure': {
'domains': {
'docstring': '"""This is the domain (entity, DTO} for __FEAT__."""',
},
'applications': {
'docstring': '"""This is the application (use case} for '
'__FEAT__."""',
},
'interfaces': {
'docstring': '"""This is the interface (abstract) for __FEAT__."""',
},
'providers': {
'docstring': '"""This is the provider for __FEAT__."""',
},
},
'tests': {
'name': 'tests',
'unit': {
'name': 'unit',
'domains': {
'docstring': '"""This is the domain (entity, DTO} unit test '
'for __FEAT__."""',
},
'applications': {
'docstring': '"""This is the application (use case} unit test '
'for __FEAT__."""',
},
'interfaces': {
'docstring': '"""This is the interface (abstract) unit test '
'for __FEAT__."""',
},
'providers': {
'docstring': '"""This is the provider unit test for '
'__FEAT__."""',
},
},
'functional': {
'name': 'functional',
'docstring': '"""This is the functional test for __FEAT__."""',
},
},
}
def parser():
""""""
_parser = argparse.ArgumentParser(
description='Create a Clean Architecture environment.')
_parser.add_argument('--root_name', '-r', help='Root folder name')
_parser.add_argument('--feat_name', '-f', help='Feature name')
args = _parser.parse_args()
if not args.root_name or not args.feat_name:
_parser.print_help()
raise ValueError("Provide 'root_name' and 'feat_name'")
return args
def create_docstring(
file: str,
docstring: str,
name: str,
root_path: bool = False):
""""""
print(f'Add docstring in {file}')
_docstring = docstring.replace('__FEAT__', name)
if root_path:
_docstring = docstring.replace('__ROOT__', name)
with open(file, 'a') as f:
f.write(_docstring + "\n")
def create_env(root_name: str, feat_name: str):
""""""
if not os.path.exists(root_name):
print(f'Create {root_name}.')
os.makedirs(root_name)
init_file = os.path.join(root_name, '__init__.py')
if not os.path.isfile(init_file):
print(f'Create {init_file}')
docstring = CLEAN_FEAT_STRUCTURE['root_path']['init']['docstring']
create_docstring(
file=init_file, docstring=docstring, name=feat_name, root_path=True)
for folder in CLEAN_FEAT_STRUCTURE['structure'].keys():
docstring = CLEAN_FEAT_STRUCTURE['structure'][folder]['docstring']
_folder = os.path.join(root_name, folder)
if not os.path.exists(_folder):
print(f'Create {_folder}')
os.makedirs(_folder)
init_file = os.path.join(_folder, '__init__.py')
if not os.path.isfile(init_file):
print(f'Create {init_file}')
create_docstring(
file=init_file, docstring=docstring, name=feat_name)
feat_file = os.path.join(_folder, f'{feat_name}.py')
if not os.path.isfile(feat_file):
print(f'Create {feat_file}')
create_docstring(
file=feat_file, docstring=docstring, name=feat_name)
# TESTS
test_root = os.path.join(root_name, CLEAN_FEAT_STRUCTURE['tests']['name'])
if not os.path.exists(test_root):
print(f'Create {test_root}')
os.makedirs(test_root)
# Functional test
test_functional = os.path.join(
test_root, CLEAN_FEAT_STRUCTURE['tests']['functional']['name'])
test_functional_docstring = \
CLEAN_FEAT_STRUCTURE['tests']['functional']['docstring']
test_functional_file = os.path.join(test_functional, f'test_{feat_name}.py')
if not os.path.exists(test_functional):
print(f'Create {test_functional}')
os.makedirs(test_functional)
if not os.path.isfile(test_functional_file):
print(f'Create {test_functional_file}')
create_docstring(
file=test_functional_file,
docstring=test_functional_docstring, name=feat_name)
# Unit test
test_unit = os.path.join(
test_root, CLEAN_FEAT_STRUCTURE['tests']['unit']['name'])
if not os.path.exists(test_unit):
print(f'Create {test_unit}')
os.makedirs(test_unit)
for folder in CLEAN_FEAT_STRUCTURE['structure'].keys():
test_unit_docstring = \
CLEAN_FEAT_STRUCTURE['tests']['unit'][folder]['docstring']
test_unit_folder = os.path.join(test_unit, folder)
test_unit_file = os.path.join(test_unit_folder, f'test_{feat_name}.py')
if not os.path.exists(test_unit_folder):
print(f'Create {test_unit_folder}')
os.makedirs(test_unit_folder)
if not os.path.isfile(test_unit_file):
print(f'Create {test_unit_file}')
create_docstring(
file=test_unit_file,
docstring=test_unit_docstring, name=feat_name)
print(f'Clean architecture environment set for {root_name} '
f'feature: {feat_name}')
def main():
""""""
try:
args = parser()
create_env(root_name=args.root_name, feat_name=args.feat_name)
except ValueError as exc:
print(f'{exc}')
if __name__ == '__main__':
main()