-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdto-generator.py
224 lines (186 loc) · 7.58 KB
/
dto-generator.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
import json
import string
import inflection
import os
import dto_template
import functools
import shutil
import argparse
import sys
import math
class Config:
dto_rename_map = []
dto_ignore_fields = []
dto_fields_map = []
class CaseFormatter(string.Formatter):
def format_field(self, value, format_spec):
if format_spec == "pascal":
return inflection.camelize(value, True)
elif format_spec == "camel":
return inflection.camelize(value, False)
else:
return super().format_field(value, format_spec)
formatter = CaseFormatter()
class FieldDescription:
def __init__(self, name, type_name, generic=None):
self.name = name
self.type_name = type_name
self.generic = generic
def __hash__(self, *args, **kwargs):
return hash(str(self))
def __str__(self, *args, **kwargs):
return str(self.name) + "-" + str(self.type_name) + (
str(self.generic) if self.generic is not None else "")
def __eq__(self, other, *args, **kwargs):
return hash(self) == hash(other)
class DtoDescription:
def __init__(self, name, fields):
self.name = name
self.fields = fields
def __hash__(self, *args, **kwargs):
return hash(str(self))
def __str__(self, *args, **kwargs):
return str(self.name) + "-" + functools.reduce(lambda agr, field: agr + str(field) + "\n", self.fields, "")
def bytes_needed(n):
if n == 0:
return 1
return int(math.log(abs(n * 2), 256)) + 1
def get_type_name(value):
type_name = type(value).__name__
return type_name
def update_dtos(known_dtos, new_dto):
fields_set = set(new_dto.fields)
for known_dto in known_dtos:
known_fields_set = set(known_dto.fields)
if known_dto.name is not None and known_dto.name == new_dto.name:
if known_fields_set.issuperset(fields_set):
if known_dto.name is None:
known_dto.name = new_dto.name
return
elif known_fields_set.issubset(fields_set):
known_dto.fields = new_dto.fields
if known_dto.name is None:
known_dto.name = new_dto.name
return
else:
known_dto.fields = known_fields_set.union(fields_set)
return
known_dtos.add(new_dto)
def reduce_flatten_dto(dto, known_dto, config, suggested_name=None):
if isinstance(dto, list):
elements_type = None
for item in dto:
if isinstance(item, dict):
new_dto = reduce_flatten_dto(item, known_dto, config, suggested_name)
element_type = new_dto.name
else:
element_type = get_type_name(item)
if element_type:
if not elements_type:
elements_type = element_type
elif elements_type != element_type:
elements_type = get_type_name(object)
return elements_type
elif isinstance(dto, dict):
fields = []
dto_name = suggested_name
for key, value in dto.items():
if value is not None:
field = None
if isinstance(value, dict):
element_name = key
# parse special attribute to get dto name
if all(d.name != element_name for d in known_dto):
new_dto = reduce_flatten_dto(value, known_dto, config, element_name)
element_name = new_dto.name
field = FieldDescription(key, element_name)
elif isinstance(value, list):
element_name = inflection.singularize(key)
elements_type = reduce_flatten_dto(value, known_dto, config, element_name)
if not elements_type:
elements_type = inflection.singularize(key)
field = FieldDescription(key, get_type_name(value), elements_type)
else:
if key == "__name__":
dto_name = value
else:
field = FieldDescription(key, get_type_name(value))
if field is not None:
fields.append(field)
new_dto = DtoDescription(dto_name, fields)
update_dtos(known_dto, new_dto)
return new_dto
return known_dto
def rename_dto(dtos, dto_rename_map):
for dto in dtos:
if dto.name in dto_rename_map:
dto.name = dto_rename_map[dto.name]
for field in dto.fields:
if field.type_name in dto_rename_map:
field.type_name = dto_rename_map[field.type_name]
if field.generic in dto_rename_map:
field.generic = dto_rename_map[field.generic]
def remove_fields(dtos, ignored_fields):
for dto in dtos:
if dto.name in ignored_fields:
ignored_field = ignored_fields[dto.name]
field = next(field for field in dto.fields if field.name == ignored_field)
dto.fields.remove(field)
def rename_fields(dtos, fields_map):
for dto in dtos:
for field in dto.fields:
if (dto.name, field.name) in fields_map:
ignored_field = fields_map[(dto.name, field.name)]
field.type_name = ignored_field
def apply_config(dtos, config):
remove_fields(dtos, config.dto_ignore_fields)
rename_dto(dtos, config.dto_rename_map)
rename_fields(dtos, config.dto_fields_map)
def generate_field(field):
out_type = dto_template.types_map.get(field.type_name)
if out_type is None:
out_type = formatter.format(dto_template.default_type_template, type=field.type_name)
if field.generic is not None:
generic_out_type = dto_template.types_map.get(field.generic)
if generic_out_type is None:
generic_out_type = formatter.format(dto_template.default_type_template, type=field.generic)
out_type = formatter.format(out_type, type=generic_out_type)
return formatter.format(dto_template.field_template, name=field.name, type=out_type)
def generate_class(dto):
body = []
for field in sorted(dto.fields, key=lambda field: field.name):
body.append(generate_field(field))
return formatter.format(dto_template.class_template, body="".join(body), name=dto.name)
parser = argparse.ArgumentParser(description='Generate dto classes.')
parser.add_argument("-i", "--input", type=str, help='Input JSON file', default="input")
parser.add_argument("-o", "--output", type=str, required=True, help='Directory where classes will be generated')
args = parser.parse_args()
if not os.path.exists(args.input):
print("Input folder doesn't exist")
sys.exit(3)
config = Config()
config_path = os.path.join(args.input, "config")
if os.path.exists(config_path):
g = {}
with open(config_path, "r") as f:
exec(f.read(), g)
for name, value in g.items():
setattr(config, name, value)
dtos = set()
for input_file in os.listdir(args.input):
if input_file.endswith(".json"):
with open(os.path.join(args.input, input_file)) as json_dto:
data = json.load(json_dto)
reduce_flatten_dto(data, dtos, config)
print("Precessed " + input_file)
apply_config(dtos, config)
dir = args.output
if os.path.exists(dir):
shutil.rmtree(dir)
os.makedirs(dir)
for dto in dtos:
if dto.name is not None:
path = os.path.join(dir, formatter.format(dto_template.file_name_template, name=dto.name))
out_class = generate_class(dto)
with open(path, mode="w") as file:
file.write(out_class)