opendbc/generator/generator.py

50 lines
1.4 KiB
Python
Raw Normal View History

2019-09-26 17:37:00 -07:00
#!/usr/bin/env python3
2018-01-25 14:46:50 -08:00
import os
import re
2018-06-17 08:08:41 -07:00
cur_path = os.path.dirname(os.path.realpath(__file__))
generator_path = os.path.join(cur_path, '../')
include_pattern = re.compile(r'CM_ "IMPORT (.*?)"')
def read_dbc(dir_name, filename):
with open(os.path.join(dir_name, filename)) as file_in:
2018-06-12 20:51:46 -07:00
return file_in.read()
def create_dbc(dir_name, filename):
dbc_file_in = read_dbc(dir_name, filename)
2018-01-25 14:46:50 -08:00
includes = include_pattern.findall(dbc_file_in)
output_filename = filename.replace('.dbc', '_generated.dbc')
output_file_location = os.path.join(generator_path, output_filename)
with open(output_file_location, 'w') as dbc_file_out:
2018-06-12 20:51:46 -07:00
dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n')
2018-06-12 20:51:46 -07:00
for include_filename in reversed(includes):
include_file_header = '\n\nCM_ "Imported file %s starts here"\n' % include_filename
dbc_file_out.write(include_file_header)
2018-01-25 14:46:50 -08:00
include_file = read_dbc(dir_name, include_filename)
2018-06-12 20:51:46 -07:00
dbc_file_out.write(include_file)
2018-01-26 11:47:55 -08:00
2018-06-12 20:51:46 -07:00
dbc_file_out.write('\nCM_ "%s starts here"\n' % filename)
2018-06-12 20:51:46 -07:00
core_dbc = include_pattern.sub('', dbc_file_in)
dbc_file_out.write(core_dbc)
2018-06-12 20:51:46 -07:00
for dir_name, _, filenames in os.walk(cur_path):
if dir_name == cur_path:
continue
2019-09-26 17:37:00 -07:00
print(dir_name)
2018-06-12 20:51:46 -07:00
for filename in filenames:
if filename.startswith('_'):
continue
2019-09-26 17:37:00 -07:00
print(filename)
create_dbc(dir_name, filename)