opendbc/generator/generator.py

36 lines
1.2 KiB
Python
Raw Normal View History

2018-01-25 15:46:50 -07:00
#!/usr/bin/env python2
import os
import re
cur_path = os.path.dirname(os.path.realpath(__file__))
2018-01-26 12:47:55 -07:00
generator_path = os.path.join(cur_path, '../')
2018-01-25 15:46:50 -07:00
for dir_name, _, _ in os.walk(cur_path):
if dir_name == cur_path:
continue
print dir_name
for filename in os.listdir(dir_name):
if filename.startswith('_'):
continue
print filename
dbc_file = open(os.path.join(dir_name, filename)).read()
include = re.search(r'CM_ "IMPORT (.*?)"', dbc_file)
if include is not None:
dbc_file = dbc_file.replace(include.group(0), '\nCM_ "%s starts here"' % filename)
include_path = os.path.join(dir_name, include.group(1))
# Load included file
include_file = open(include_path).read()
include_file = 'CM_ "Imported file %s starts here"\n' % include.group(1) + include_file
dbc_file = include_file + dbc_file
dbc_file = 'CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n' + dbc_file
2018-01-26 12:47:55 -07:00
output_filename = filename.replace('.dbc', '_generated.dbc')
output_dbc_file = open(os.path.join(generator_path, output_filename), 'w')
2018-01-25 15:46:50 -07:00
output_dbc_file.write(dbc_file)
output_dbc_file.close()