-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileGenerator.py
334 lines (263 loc) · 11.6 KB
/
FileGenerator.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
#
# Beautiful Capi generates beautiful C API wrappers for your C++ classes
# Copyright (C) 2015 Petr Petrovich Petrov
#
# This file is part of Beautiful Capi.
#
# Beautiful Capi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Beautiful Capi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Beautiful Capi. If not, see <http://www.gnu.org/licenses/>.
#
import os
import io
import hashlib
from Helpers import fix_name, replace_double_greater
class AtomicString(object):
def __init__(self, string_value):
self.value = string_value
def get_lines(self) -> [str]:
return [self.value]
def line_count(self):
return self.value.count('\n')
class IncludeHeaders(object):
def __init__(self, included_files, indent: str):
self.included_files = included_files
self.indent = indent
def get_lines(self):
result_lines = []
already_included_files = {}
for header_file, is_system in self.included_files:
if header_file not in already_included_files:
if is_system:
result_lines.append('{0}#include <{1}>\n'.format(self.indent, header_file))
else:
result_lines.append('{0}#include "{1}"\n'.format(self.indent, header_file))
already_included_files.update({header_file: is_system})
return result_lines
def line_count(self):
count = 0
already_included_files = {}
for header_file, is_system in self.included_files:
if header_file not in already_included_files:
already_included_files.update({header_file: is_system})
else:
count += 1
return count
class FileGenerator(object):
def __init__(self, filename):
self.filename = filename
self.cur_indent = 0
self.file_header = None
self.copyright_header = None
self.automatic_generation_warning = None
self.lines = []
self.included_files = []
self.included_files_were_included = False
self.indent_size = 4
def __del__(self):
self.__write()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.__write()
@staticmethod
def get_hash(lines: io.IOBase or [str]):
hash_obj = hashlib.sha1()
for line in lines:
hash_obj.update(line.encode())
return hash_obj.hexdigest()
def __write(self):
if self.filename:
lines = []
if self.file_header:
lines.append(self.file_header + '\n')
if self.copyright_header:
lines.append(self.copyright_header + '\n\n')
if self.automatic_generation_warning:
lines.append(self.automatic_generation_warning + '\n\n')
lines += self.get_lines()
dir_name = os.path.dirname(self.filename)
if dir_name and not os.path.exists(dir_name):
os.makedirs(dir_name)
if os.path.exists(self.filename):
new_hash = FileGenerator.get_hash(lines)
file = open(self.filename, 'r')
old_hash = FileGenerator.get_hash(file)
if new_hash == old_hash:
return
with open(self.filename, 'w') as output_file:
for line in lines:
output_file.write(line)
def empty(self):
return not self.lines
def line_count(self) -> int:
count = 0
if self.file_header:
count += self.file_header.count('\n') + 1
if self.copyright_header:
count += self.copyright_header.count('\n') + 2
if self.automatic_generation_warning:
count += self.automatic_generation_warning.count('\n') + 2
for line in self.lines:
count += line.line_count()
return count
def get_lines(self):
result = []
for line in self.lines:
for elementary_line in line.get_lines():
result.append(elementary_line)
return result
def increase_indent(self):
self.cur_indent += self.indent_size
def decrease_indent(self):
self.cur_indent -= self.indent_size
if self.cur_indent < 0:
raise IndentationError
def get_indent_str(self):
return ' ' * self.cur_indent
def include_header(self, header_file, is_system):
if header_file:
self.included_files.append((header_file, is_system))
def include_user_header(self, header_file):
self.include_header(header_file, False)
def include_system_header(self, header_file):
self.include_header(header_file, True)
def put_line(self, line, eol='\n'):
result_line = self.get_indent_str() + line if line else ''
result_line = replace_double_greater(result_line)
self.lines.append(AtomicString(result_line + eol))
def put_lines(self, lines: [str], eol='\n'):
for line in lines:
self.put_line(line, eol)
def put_return_cpp_statement(self, expression: str):
if expression:
self.put_line('return {expression};'.format(expression=expression))
def put_file(self, another_file):
self.lines.append(another_file)
def put_include_files(self, add_empty_line: bool = True):
if not self.included_files_were_included:
self.lines.append(IncludeHeaders(self.included_files, self.get_indent_str()))
self.included_files_were_included = True
if add_empty_line:
self.put_line('')
def put_python_header(self):
self.file_header = '#!/usr/bin/env python'
def put_python_gnu_gpl_copyright_header(self):
self.put_copyright_header(
'#\n'
'# Beautiful Capi generates beautiful C API wrappers for your C++ classes\n'
'# Copyright (C) 2015 Petr Petrovich Petrov\n'
'#\n'
'# This file is part of Beautiful Capi.\n'
'#\n'
'# Beautiful Capi is free software: you can redistribute it and/or modify\n'
'# it under the terms of the GNU General Public License as published by\n'
'# the Free Software Foundation, either version 3 of the License, or\n'
'# (at your option) any later version.\n'
'#\n'
'# Beautiful Capi is distributed in the hope that it will be useful,\n'
'# but WITHOUT ANY WARRANTY; without even the implied warranty of\n'
'# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n'
'# GNU General Public License for more details.\n'
'#\n'
'# You should have received a copy of the GNU General Public License\n'
'# along with Beautiful Capi. If not, see <http://www.gnu.org/licenses/>.\n'
'#\n'
)
def put_copyright_header(self, copyright_header_text):
self.copyright_header = copyright_header_text
def put_python_automatic_generation_warning(self):
self.put_automatic_generation_warning(
'#\n'
'# WARNING: This file was automatically generated by Xsd2Python3.py program!\n'
'# Do not edit this file! Please edit the source XSD schema.\n'
'#\n'
)
def put_automatic_generation_warning(self, warning_text):
self.automatic_generation_warning = warning_text
def put_begin_cpp_comments(self, params_description):
self.put_copyright_header('\n'.join(params_description.copyright_headers))
self.put_automatic_generation_warning('\n'.join(params_description.automatic_generated_warnings))
class Indent(object):
def __init__(self, file_generator):
self.file_generator = file_generator
def __enter__(self):
self.file_generator.increase_indent()
def __exit__(self, exc_type, exc_val, exc_tb):
self.file_generator.decrease_indent()
class Unindent(object):
def __init__(self, file_generator):
self.file_generator = file_generator
def __enter__(self):
self.file_generator.decrease_indent()
def __exit__(self, exc_type, exc_val, exc_tb):
self.file_generator.increase_indent()
class UnindentAll(object):
def __init__(self, file_generator):
self.file_generator = file_generator
self.previous_indent = 0
def __enter__(self):
self.previous_indent = self.file_generator.cur_indent
self.file_generator.cur_indent = 0
def __exit__(self, exc_type, exc_val, exc_tb):
self.file_generator.cur_indent = self.previous_indent
class IndentScope(Indent):
def __init__(self, file_generator, ending='}'):
super().__init__(file_generator)
self.ending = ending
def __enter__(self):
self.file_generator.put_line('{')
super().__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
super().__exit__(exc_type, exc_val, exc_tb)
self.file_generator.put_line(self.ending)
class WatchdogScope(object):
def __init__(self, file_generator, watchdog_string):
self.file_generator = file_generator
self.watchdog_string = fix_name(watchdog_string)
def __enter__(self):
self.file_generator.put_line('#ifndef {0}'.format(self.watchdog_string))
self.file_generator.put_line('#define {0}'.format(self.watchdog_string))
self.file_generator.put_line('')
def __exit__(self, exc_type, exc_val, exc_tb):
self.file_generator.put_line('')
self.file_generator.put_line('#endif /* {0} */'.format(self.watchdog_string))
self.file_generator.put_line('')
class IfDefScope(object):
def __init__(self, file_generator, condition_string, add_empty_lines: bool=True):
self.file_generator = file_generator
self.condition_string = condition_string
self.add_empty_lines = add_empty_lines
def __enter__(self):
self.file_generator.put_line('#ifdef {0}'.format(self.condition_string))
if self.add_empty_lines:
self.file_generator.put_line('')
def __exit__(self, exc_type, exc_val, exc_tb):
if self.add_empty_lines:
self.file_generator.put_line('')
self.file_generator.put_line('#endif /* {0} */'.format(self.condition_string))
def if_then_else_impl(out: FileGenerator, if_string: str, condition_string: str, then_call, else_call):
out.put_line('{if_string} {condition_string}'.format(if_string=if_string, condition_string=condition_string))
with Indent(out):
then_call(out)
if else_call:
out.put_line('#else /* {condition_string} */'.format(condition_string=condition_string))
with Indent(out):
else_call(out)
out.put_line('#endif /* {condition_string} */'.format(condition_string=condition_string))
def if_then_else(out: FileGenerator, condition_string: str, then_call, else_call):
if_then_else_impl(out, '#if', condition_string, then_call, else_call)
def if_def_then_else(out: FileGenerator, condition_string: str, then_call, else_call):
if_then_else_impl(out, '#ifdef', condition_string, then_call, else_call)
def if_not_def_then_else(out: FileGenerator, condition_string: str, then_call, else_call):
if_then_else_impl(out, '#ifndef', condition_string, then_call, else_call)