Skip to content

Commit 09bcc28

Browse files
committed
Refactored most of the generation of code to a class and greatly simplifed replacement logic instead of string concatination.
1 parent be147da commit 09bcc28

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,6 @@ dmypy.json
136136

137137
# Cython debug symbols
138138
cython_debug/
139+
140+
# Generated .java files
141+
*.java

easyjava.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from parse import DataParser
1313
from typeinfo import TypeInfo
1414
from util import camel_case, first_upper, first_chars, spinal_case, pyinstaller_get_full_path
15+
from generator import Generator
1516

1617
def get_argument(argument, default="None"):
1718
if argument:
@@ -127,6 +128,11 @@ def main(program_name='Easy Java', program_description='Generate POJOs and Row M
127128
entity = str(get_argument(args.entity))
128129
data_file = str(get_argument(args.data))
129130

131+
gen = Generator()
132+
gen.load_file(data_file)
133+
gen.generate_entity_files(entity, os.getcwd())
134+
exit()
135+
130136
types = parse_data(data_file)
131137

132138
generate_entity_file(entity, types)

easyjava.spec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ gooey_root = os.path.dirname(gooey.__file__)
77
gooey_languages = Tree(os.path.join(gooey_root, 'languages'), prefix = 'gooey/languages')
88
gooey_images = Tree(os.path.join(gooey_root, 'images'), prefix = 'gooey/images')
99

10+
import os
11+
path = os.getcwd()
1012

1113
a = Analysis(['easyjava.py'],
12-
pathex=['C:\\Users\\Joe\\Documents\\Projects\\Python\\easy-java'],
14+
pathex=[path],
1315
binaries=[],
1416
datas=[('templates/*.*', './templates'), ('words.txt', '.')],
1517
hiddenimports=['pkg_resources.py2_warn'],

generator.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
""" Resposible for generating code related to a language """
2+
import os
3+
4+
from parse import DataParser
5+
from util import camel_case, first_upper, first_chars, spinal_case, pyinstaller_get_full_path
6+
7+
DEFINITION_TEMPLATE = '\tprivate {0} {1};'
8+
GETTER_TEMPLATE = '\tpublic {0} get{1}() {{\n\t\treturn this.{2};\n\t}}'
9+
SETTER_TEMPLATE = '\tpublic void set{0}({1} {2}) {{\n\t\tthis.{2} = {2};\n\t}}'
10+
TOSTRING_TEMPLATE = '"[{0}] [{1}]"'
11+
TOSTRING_INNER_TEMPLATE = '{}=" + {} + "'
12+
13+
class Generator(object):
14+
data = []
15+
types = {}
16+
17+
def __init__(self):
18+
self.parser = DataParser()
19+
20+
21+
def load_file(self, data_file):
22+
# Reset information about a file
23+
self.data = []
24+
self.types = {}
25+
26+
with open(data_file, 'r') as in_file:
27+
lines = in_file.read().splitlines()
28+
29+
30+
header = lines[0].split('|')
31+
print(header)
32+
33+
for item in header:
34+
self.types[item] = None
35+
36+
line_data = []
37+
for line in lines[1:]:
38+
line_data = [c for c in line.split('|') if c != '' and c != ' ']
39+
if len(line_data) is len(lines[0]):
40+
break # TODO: Is there a better way to handle this?
41+
42+
for idx, cell in enumerate(line_data):
43+
self.types[header[idx]] = self.parser.parse_type(cell), camel_case(header[idx])
44+
45+
# {'ACTIONITEMIMPACTID': ('int', 'actionItemImpactId'), 'ACTIONITEMID': ('int', 'actionItemId'), 'APPID': ('int', 'appId'), 'APPNAME': (None, 'appName'), 'UPDATEDBY': ('String', 'updatedBy'), 'LASTUPDATED': ('String', 'lastUpdated'), 'DISTANCE': ('float', 'distance')}
46+
print(self.types)
47+
48+
def generate_entity_files(self, entity_name, folder_path):
49+
if not os.path.isdir(folder_path):
50+
# Create folder
51+
os.mkdir(folder_path)
52+
53+
# Make Bean
54+
self._make_bean(entity_name, folder_path)
55+
56+
def _make_bean(self, entity_name, folder_path):
57+
if not entity_name[0].isupper():
58+
entity_name = first_upper(entity_name)
59+
60+
print('Generating {0}.java'.format(entity_name))
61+
62+
declarations = '\n'.join(self._generate_definitions())
63+
64+
getter_setter = '\n'.join(self._generate_getters_setters())
65+
66+
to_string = self._generate_tostring(entity_name)
67+
68+
69+
#to_string = '" ' + entity_name + ' ['
70+
71+
72+
#for t in types:
73+
# "[%ENTITY%] [[%VARIABLE%]=" + [%VARIABLE%] + ", Name=" + name + ", Designation=" + designation + ", Salary=" + salary + "]";
74+
# to_string = to_string + t.var_name + '= " + ' + t.var_name + ' + ", '
75+
#to_string = ''.join(to_string[:len(to_string)-3]) + '"]";'
76+
77+
template = ''
78+
path = pyinstaller_get_full_path('templates/Entity.java')
79+
80+
with open(path, 'r') as in_file:
81+
template = in_file.read()
82+
83+
template = template.replace('[%ENTITY%]', entity_name).replace('[%DECLARATION%]', declarations).replace('[%GETTER_SETTER%]', getter_setter).replace('[%TOSTRING%]', to_string)
84+
85+
output_path = os.path.join(folder_path, entity_name + '.java')
86+
87+
with open(output_path, 'w+') as out_file:
88+
out_file.write(template)
89+
90+
def _generate_getters_setters(self):
91+
code_lines = []
92+
for key in self.types:
93+
var = self.types[key]
94+
code_lines.append(GETTER_TEMPLATE.format(var[0], first_upper(var[1]), var[1]))
95+
code_lines.append(SETTER_TEMPLATE.format(var[0], first_upper(var[1]), var[1]))
96+
print(code_lines)
97+
return code_lines
98+
99+
def _generate_definitions(self):
100+
code_lines = []
101+
for key in self.types:
102+
var = self.types[key]
103+
code_lines.append(DEFINITION_TEMPLATE.format(var[0], var[1]))
104+
print(code_lines)
105+
return code_lines
106+
107+
def _generate_tostring(self, entity_name):
108+
variables = []
109+
for key in self.types:
110+
var = self.types[key]
111+
variables.append(first_upper(var[1]))
112+
variables.append(var[1])
113+
inner_format_templates = ', '.join([TOSTRING_INNER_TEMPLATE] * len(self.types.keys()))
114+
return TOSTRING_TEMPLATE.format(entity_name, inner_format_templates.format(*variables))

testdata/rccb.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PROJECTRCCBID|RCCBID|PROJECTRERID|PROJECTID|RERSTEPID|RERSTATUSID|RERTYPEID|RCCBRESPONSEID|RCCBRESPONSE|RCCBRESPONSEDATE|RCCBREJCODE|RCCBREJECTIONREASONCODE|RCCBCOMMENTS|LPMRESOLUTIONDATE|LPMRESOLCOMMENTS|APPROVEDBY|CREATEDBY|CREATEDON|RCCBGROUP|RCCBNAME|RCCBAPPROVALDUEDATE|RCCBAPPROVALDATE|LASTUPDATEDBY|RELEASEIDTOADD|RELEASEIDTOREMOVE
2+
91214|7|17807|305269|88|85|16|10001|Approved|13-DEC-19||||||th7931|sk8335|05-DEC-19|GTTS|eCorp|20-DEC-19|13-DEC-19|th7931|r319740|
3+
91215|3|17807|305269|88|85|16|10001|Approved|09-DEC-19|||Validated RER Requirements.|||rw4411|sk8335|05-DEC-19|WTED|Supply Chain Systems|20-DEC-19|13-DEC-19|rw4411|r319750|

0 commit comments

Comments
 (0)