-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdto_class_generator.py
More file actions
284 lines (211 loc) · 9.63 KB
/
dto_class_generator.py
File metadata and controls
284 lines (211 loc) · 9.63 KB
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
''' Reads each C# class file in specified dir, and extracts the public
properties generating DTO (classes used for serialization and ORM)
Params:
0: (Optional)
Parent namespace for the resultant DTO's
ex: 'Synchronizer'
result namespace: 'namespace Synchronizer.Data.DTO'
1: Input dir containing C# classes
2: Output dir for DTO classes
Output: For each input class a 'class_name' + DTO file with
the public properties.
Example:
If we have: Person.cs and Request.cs in Models/
doing:
dto_class_generator.py Models/ DTO/
we get:
PersonDTO.cs and RequestDTO.cs
'''
import re
import sys
import os
DEBUG = False
# Regex matching public properties
property_regex = re.compile('(?:public)\s(?!class)(?!override|abstract|static|readonly)(?P<type>[\w<>]+) (?P<name>\w+)(?![\(\w])')
# Regex matching using statements
import_regex = re.compile('(?:using) (?P<import>[\w\.]+);')
# Regex matching public and internal classes
class_name_regex = re.compile('(public|internal) (?P<modifier>abstract|static)?(\s)?(class) (?P<classname>\w+)')
def match_properties(text):
return property_regex.search(text)
def match_class_name(text):
return class_name_regex.search(text)
# Taken from StackOverflow, made by mg.
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
def indent(string, level):
return ' ' * level + string
def generate_dto(project_namespace, filepath, result_filepath):
''' Parse the input class file and extract classes
and properties for each '''
identation = 0 # Level of indent
data = parse_file(filepath)
imports = data[0]
result = data[1]
# Show classes read from file and properties found
if DEBUG:
print ('\nShowing results:')
print (imports)
print (result)
for class_found in result:
print ('Class:' + class_found)
print ('Inside:')
for property_found in result[class_found]:
print (property_found)
# Create result file for writing the results
with open(result_filepath, 'w') as result_file:
output = ''
# Using statements
for import_statement in imports:
output = ''.join(output + 'using ' + import_statement + ';\n')
# Namespace start
output = ''.join(output + '\nnamespace ' + project_namespace + '.Data.DTO\n' +\
'{' + '\n')
for class_found in result:
# Class header
indent_level = 4
output = ''.join(output +
indent('/// <summary>\n', indent_level) +
indent('/// DTO generated from <see cref="' + class_found + '"/> for storage.\n', indent_level) +
indent('/// </summary>\n', indent_level) +
indent('public class ' + class_found + 'DTO' + '\n', indent_level) +
indent('{\n', indent_level)
)
# Empty constructor
indent_level += 4
output += indent('public ' + class_found + 'DTO' + '()' + '{}\n\n', indent_level)
# Inside properties
for property_found in result[class_found]:
output = ''.join(output +
indent('public ' + property_found['type'] + ' ' + property_found['name'] +\
' { get; set; }\n', indent_level))
# Write class to DTO generator (copies properties with reflection)
output = ''.join(output + '\n' +
indent('public ' + class_found + 'DTO' + '(' + class_found + ' other)\n', indent_level) +
indent('{\n', indent_level) +
indent('foreach(var property in other.GetType().GetProperties())\n', indent_level + 4) +
indent('{\n', indent_level + 4) +
indent('this.GetType()\n', indent_level + 8) +
indent('.GetProperty(property.Name)\n', indent_level + 8) +
indent('.SetValue(this, property.GetValue(other, null), null);\n', indent_level + 8) +
indent('}\n', indent_level + 4) +
indent('}\n', indent_level)
)
# Write DTO to class generator (recovers a class instance from the DTO)
output = ''.join(output + '\n' +
indent('/// <summary>\n', indent_level) +
indent('/// Returns a <see cref="' + class_found + '"/> from this DTO.\n', indent_level) +
indent('/// </summary>\n', indent_level) +
indent('public ' + class_found + ' Reconstruct()\n', indent_level) +
indent('{\n', indent_level) +
indent(class_found + ' result = new ' + class_found + '()\n', indent_level + 4) +
indent('{\n', indent_level + 4) )
for property_found in result[class_found]:
output = ''.join(output +
indent(property_found['name'] + ' = ' + 'this.' + property_found['name'] + ',\n', indent_level + 8) )
output = rreplace(output, ',', '', 1) # Remove last ','
# Return result
output = ''.join(output + indent('}\n', indent_level + 4) + indent('return result;\n', indent_level + 4))
# Close Reconstruct() method
output = ''.join(output +
indent('}\n', indent_level))
# Close class
indent_level -= 4
output = ''.join(output + indent('}\n' +\
# Close namespace
'}', indent_level))
if DEBUG:
print('Result file:\n' + output)
# Write the result file
result_file.write(output)
def parse_file(filepath):
''' Returns classes and properties found in a class file '''
''' Input: string path to the file to parse '''
''' Output: tuple(list of imports strings, dict (class, properties[])) '''
classes = dict()
imports = list()
current_class = None
inside_class = False
text = open(filepath, 'r').read()
if DEBUG:
print('Input:\n' + text)
property_match = None
class_match = None
for line in text.split('\n'):
if (len(line) < 1):
continue
if DEBUG: # Show line
print ('Line:\n' + line)
import_match = import_regex.search(line)
if (import_match != None):
imports.append(import_match.groupdict()['import'])
continue
class_match = match_class_name(line)
if (class_match != None):
inside_class = True
current_class = class_match.groupdict()['classname']
classes[current_class] = list()
if DEBUG:
print (classes)
print ('Got: ')
print (class_match.groupdict())
print ('\n')
continue
if (line == '{' or line == '}'):
if DEBUG:
print('Skipped bracket line\n')
if (line == '{'):
inside_class = True
if (line == '}'):
inside_class = False #Exiting class
continue
if (inside_class):
property_match = match_properties(line)
if (property_match != None):
if DEBUG:
print ('Got: ')
print (property_match.groupdict())
print ('\n')
# Append property found to current class
try:
classes[current_class].append(property_match.groupdict())
except KeyError as k:
print ('Got key error:{0}'.format(k) + ' when\n' + line)
if DEBUG:
print ('\n')
return (imports, classes)
if __name__ == '__main__':
namespace_index = None
# Params: Namespace Input_dir Output_dir
if (len(sys.argv) > 3):
namespace_index = 1
input_index = 2
output_index = 3
# Params: Input_dir Output_dir
elif (len(sys.argv) > 2):
input_index = 1
output_index = 2
if namespace_index != None:
# Parent namespace for DTO
project_namespace = sys.argv[namespace_index]
else:
project_namespace = 'MyProject'
if input_index != None:
# Read all class files in input_dir
input_dir = sys.argv[input_index]
# And make DTO result class on output
output_dir = sys.argv[output_index]
if os.path.isfile(input_dir):
input_filepath = input_dir
output_filepath = output_dir
generate_dto(project_namespace, input_filepath, output_filepath)
else:
# For each class file found in input_dir
# read generate a DTO for each one
for class_filepath in os.listdir(input_dir):
# File has to be C# code
if class_filepath.endswith('.cs'):
input_filepath = input_dir + '\\' + class_filepath
output_filepath = output_dir + '\\' + class_filepath.split('.')[0] + 'DTO' + '.cs'
generate_dto(project_namespace, input_filepath, output_filepath)