Skip to content

Commit 031acc5

Browse files
dek3rrpd0wm
authored andcommittedJun 17, 2018
Update generator.py
- regex compiled as object - files write in append mode for speed - erased "IMPORT" lines in dbc
1 parent 19a4249 commit 031acc5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed
 

‎generator/generator.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def read_dbc(filename):
99
cur_path = os.path.dirname(os.path.realpath(__file__))
1010
generator_path = os.path.join(cur_path, '../')
1111

12-
include_pattern = r'CM_ "IMPORT (.*?)"'
12+
include_pattern = re.compile(r'CM_ "IMPORT (.*?)"')
1313

1414
for dir_name, _, filenames in os.walk(cur_path):
1515
if dir_name == cur_path:
@@ -24,11 +24,15 @@ def read_dbc(filename):
2424
print filename
2525
dbc_file_in = read_dbc(filename)
2626

27-
includes = re.findall(include_pattern, dbc_file_in)
27+
includes = include_pattern.findall(dbc_file_in)
2828

2929
output_filename = filename.replace('.dbc', '_generated.dbc')
30-
31-
with open(os.path.join(generator_path, output_filename), 'w') as dbc_file_out:
30+
output_file_location = os.path.join(generator_path, output_filename)
31+
32+
if os.isfile(output_file_location):
33+
os.remove(output_file_location)
34+
35+
with open(output_file_location, 'a') as dbc_file_out:
3236
dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n')
3337

3438
for include_filename in reversed(includes):
@@ -40,4 +44,6 @@ def read_dbc(filename):
4044
dbc_file_out.write(include_file)
4145

4246
dbc_file_out.write('\nCM_ "%s starts here"\n' % filename)
43-
dbc_file_out.write(dbc_file_in)
47+
48+
core_dbc = include_pattern.sub('', dbc_file_in)
49+
dbc_file_out.write(core_dbc)

0 commit comments

Comments
 (0)