-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
166 lines (122 loc) · 6.11 KB
/
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
""" Resposible for generating code related to a language """
import os
from parse import DataParser
from util import camel_case, first_upper, first_chars, spinal_case, pyinstaller_get_full_path
from languagetype import Language
DEFINITION_TEMPLATE = '\tprivate {0} {1};'
GETTER_TEMPLATE = '\tpublic {0} get{1}() {{\n\t\treturn this.{2};\n\t}}'
SETTER_TEMPLATE = '\tpublic void set{0}({1} {2}) {{\n\t\tthis.{2} = {2};\n\t}}'
TOSTRING_TEMPLATE = '"[{0}] [{1}]"'
TOSTRING_INNER_TEMPLATE = '{}=" + {} + "'
ROWMAP_SETTER_TEMPLATE = '\t\t{0}.set{1}(rs.get{1}("{2}"));'
TYPESCRIPT_DEFINITION_TEMPLATE = ' {0}: {1};'
class Generator(object):
data = []
types = {}
def __init__(self):
self.parser = DataParser()
def load_file(self, data_file):
# Reset information about a file
self.data = []
self.types = {}
with open(data_file, 'r') as in_file:
lines = in_file.read().splitlines()
header = lines[0].split('|')
for item in header:
self.types[item] = None
line_data = []
for line in lines[1:]:
line_data = [c for c in line.split('|')]
if len(line_data) is len(lines[0]):
break
for idx, cell in enumerate(line_data):
self.types[header[idx]] = self.parser.parse_type(cell), camel_case(header[idx])
#print(self.types)
self._pretty_print_types()
def generate_entity_files(self, entity_name, folder_path, create_typescript=False):
if not os.path.isdir(folder_path):
os.mkdir(folder_path)
self._make_bean(entity_name, folder_path)
self._make_bean_rowmapper(entity_name, folder_path)
if create_typescript:
self._make_typescript(entity_name, folder_path)
def _make_bean(self, entity_name, folder_path):
if not entity_name[0].isupper():
entity_name = first_upper(entity_name)
print('Generating {0}.java'.format(entity_name))
declarations = '\n'.join(self._generate_definitions())
getter_setter = '\n'.join(self._generate_getters_setters())
to_string = self._generate_tostring(entity_name)
template = ''
path = pyinstaller_get_full_path('templates/Entity.java')
with open(path, 'r') as in_file:
template = in_file.read()
template = template.replace('[%ENTITY%]', entity_name).replace('[%DECLARATION%]', declarations).replace('[%GETTER_SETTER%]', getter_setter).replace('[%TOSTRING%]', to_string)
output_path = os.path.join(folder_path, entity_name + '.java')
with open(output_path, 'w+') as out_file:
out_file.write(template)
def _make_bean_rowmapper(self, entity_name, folder_path):
if not entity_name[0].isupper():
entity_name = first_upper(entity_name)
print('Generating {0}RowMapper.java'.format(entity_name))
template = ''
path = pyinstaller_get_full_path('templates/EntityRowMapper.java')
with open(path, 'r') as in_file:
template = in_file.read()
row_maps = '\n'.join(self._generate_rowmap(entity_name))
template = template.replace('[%ENTITY%]', first_upper(entity_name)).replace('[%ENTITYVAR%]', first_chars(entity_name)).replace('[%ROWMAPPER%]', row_maps)
output_path = os.path.join(folder_path, entity_name + 'RowMapper.java')
with open(output_path, 'w+') as out_file:
out_file.write(template)
def _make_typescript(self, entity_name, folder_path):
print('Generating {0}.ts'.format(spinal_case(entity_name).lower()))
template = ''
path = pyinstaller_get_full_path('templates/entity.ts')
with open(path, 'r') as in_file:
template = in_file.read()
definitions = '\n'.join(self._generate_typescript_definitions())
template = template.replace('[%ENTITY%]', first_upper(entity_name)).replace('[%DEFINITIONS%]', definitions)
output_path = os.path.join(folder_path, spinal_case(entity_name).lower() + '.ts')
with open(output_path, 'w+') as out_file:
out_file.write(template)
def _generate_typescript_definitions(self):
code_lines = []
for key in self.types:
var = self.types[key]
code_lines.append(TYPESCRIPT_DEFINITION_TEMPLATE.format(var[1], self.parser.translate_type(var[0], Language.Typescript)))
return code_lines
def _generate_rowmap(self, entity_name):
entity_var_name = first_chars(entity_name)
code_lines = []
for key in self.types:
var = self.types[key]
code_lines.append(ROWMAP_SETTER_TEMPLATE.format(entity_var_name, first_upper(var[1]), key))
return code_lines
def _generate_getters_setters(self):
code_lines = []
for key in self.types:
var = self.types[key]
java_type = self.parser.translate_type(var[0], Language.Java)
code_lines.append(GETTER_TEMPLATE.format(java_type, first_upper(var[1]), var[1]))
code_lines.append(SETTER_TEMPLATE.format(first_upper(java_type), java_type, var[1]))
return code_lines
def _generate_definitions(self):
code_lines = []
for key in self.types:
var = self.types[key]
code_lines.append(DEFINITION_TEMPLATE.format(self.parser.translate_type(var[0], Language.Java), var[1]))
return code_lines
def _generate_tostring(self, entity_name):
variables = []
for key in self.types:
var = self.types[key]
variables.append(first_upper(var[1]))
variables.append(var[1])
inner_format_templates = ', '.join([TOSTRING_INNER_TEMPLATE] * len(self.types.keys()))
return TOSTRING_TEMPLATE.format(entity_name, inner_format_templates.format(*variables))
def _pretty_print_types(self):
print('Parsed type info:')
# \tSQL -> camelCase: type
for key in self.types:
var = self.types[key]
print('\t{} -> {}: {}'.format(key, var[1], var[0]))