Update generator.py

- regex compiled as object
- files write in append mode for speed
- erased "IMPORT" lines in dbc
master
dekerr 2018-06-12 20:14:18 -04:00 committed by Willem Melching
parent 19a4249b72
commit 031acc58e0
1 changed files with 11 additions and 5 deletions

View File

@ -9,7 +9,7 @@ def read_dbc(filename):
cur_path = os.path.dirname(os.path.realpath(__file__))
generator_path = os.path.join(cur_path, '../')
include_pattern = r'CM_ "IMPORT (.*?)"'
include_pattern = re.compile(r'CM_ "IMPORT (.*?)"')
for dir_name, _, filenames in os.walk(cur_path):
if dir_name == cur_path:
@ -24,11 +24,15 @@ for dir_name, _, filenames in os.walk(cur_path):
print filename
dbc_file_in = read_dbc(filename)
includes = re.findall(include_pattern, dbc_file_in)
includes = include_pattern.findall(dbc_file_in)
output_filename = filename.replace('.dbc', '_generated.dbc')
with open(os.path.join(generator_path, output_filename), 'w') as dbc_file_out:
output_file_location = os.path.join(generator_path, output_filename)
if os.isfile(output_file_location):
os.remove(output_file_location)
with open(output_file_location, 'a') as dbc_file_out:
dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n')
for include_filename in reversed(includes):
@ -40,4 +44,6 @@ for dir_name, _, filenames in os.walk(cur_path):
dbc_file_out.write(include_file)
dbc_file_out.write('\nCM_ "%s starts here"\n' % filename)
dbc_file_out.write(dbc_file_in)
core_dbc = include_pattern.sub('', dbc_file_in)
dbc_file_out.write(core_dbc)