From 71f547219c1e46b509dcf205853afd9c6c304250 Mon Sep 17 00:00:00 2001 From: Vehicle Researcher Date: Tue, 19 Jun 2018 01:38:16 +0000 Subject: [PATCH] Squashed 'opendbc/' changes from 5db3dfe..f034dee f034dee pass dirname explicitly to generator helperfunctions and whitespace d2fb5f0 small generator cleanup ab04e55 Update generator.py d86d7f1 Update generator.py 031acc5 Update generator.py 19a4249 Update generator.py 9c49b88 Update generator.py git-subtree-dir: opendbc git-subtree-split: f034deec7dda00c4c33a614ad104ce51aeb604b9 --- generator/generator.py | 56 +++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/generator/generator.py b/generator/generator.py index e5f69e40bb..85f3f009e2 100755 --- a/generator/generator.py +++ b/generator/generator.py @@ -4,34 +4,46 @@ import re cur_path = os.path.dirname(os.path.realpath(__file__)) generator_path = os.path.join(cur_path, '../') +include_pattern = re.compile(r'CM_ "IMPORT (.*?)"') -for dir_name, _, _ in os.walk(cur_path): + +def read_dbc(dir_name, filename): + with open(os.path.join(dir_name, filename)) as file_in: + return file_in.read() + + +def create_dbc(dir_name, filename): + dbc_file_in = read_dbc(dir_name, filename) + + 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: + dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n') + + 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) + + include_file = read_dbc(dir_name, include_filename) + dbc_file_out.write(include_file) + + dbc_file_out.write('\nCM_ "%s starts here"\n' % filename) + + core_dbc = include_pattern.sub('', dbc_file_in) + dbc_file_out.write(core_dbc) + + +for dir_name, _, filenames in os.walk(cur_path): if dir_name == cur_path: continue print dir_name - - for filename in os.listdir(dir_name): + for filename in filenames: if filename.startswith('_'): continue print filename - dbc_file = open(os.path.join(dir_name, filename)).read() - dbc_file = '\nCM_ "%s starts here"\n' % filename + dbc_file - - includes = re.finditer(r'CM_ "IMPORT (.*?)"', dbc_file) - for include in includes: - dbc_file = dbc_file.replace(include.group(0), '') - include_path = os.path.join(dir_name, include.group(1)) - - # Load included file - include_file = open(include_path).read() - include_file = '\n\nCM_ "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 - - output_filename = filename.replace('.dbc', '_generated.dbc') - output_dbc_file = open(os.path.join(generator_path, output_filename), 'w') - output_dbc_file.write(dbc_file) - output_dbc_file.close() + create_dbc(dir_name, filename)