#!/usr/bin/env python3
import os
import re

cur_path = os.path.dirname(os.path.realpath(__file__))
opendbc_root = os.path.join(cur_path, '../')
include_pattern = re.compile(r'CM_ "IMPORT (.*?)";')


def read_dbc(src_dir, filename):
  with open(os.path.join(src_dir, filename)) as file_in:
    return file_in.read()


def create_dbc(src_dir, filename, output_path):
  dbc_file_in = read_dbc(src_dir, filename)

  includes = include_pattern.findall(dbc_file_in)

  output_filename = filename.replace('.dbc', '_generated.dbc')
  output_file_location = os.path.join(output_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 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(src_dir, 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)


def create_all(output_path):
  for src_dir, _, filenames in os.walk(cur_path):
    if src_dir == cur_path:
      continue

    #print(src_dir)
    for filename in filenames:
      if filename.startswith('_') or not filename.endswith('.dbc'):
        continue

      #print(filename)
      create_dbc(src_dir, filename, output_path)

if __name__ == "__main__":
  create_all(opendbc_root)

[((6, 15, 6, 44), 'os.path.join', 'os.path.join', ({(6, 28, 6, 36): 'cur_path', (6, 38, 6, 43): '"""../"""'}, {}), "(cur_path, '../')", False, 'import os\n'), ((7, 18, 7, 52), 're.compile', 're.compile', ({(7, 29, 7, 51): '"""CM_ "IMPORT (.*?)";"""'}, {}), '(\'CM_ "IMPORT (.*?)";\')', False, 'import re\n'), ((5, 27, 5, 53), 'os.path.realpath', 'os.path.realpath', ({(5, 44, 5, 52): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((21, 25, 21, 67), 'os.path.join', 'os.path.join', ({(21, 38, 21, 49): 'output_path', (21, 51, 21, 66): 'output_filename'}, {}), '(output_path, output_filename)', False, 'import os\n'), ((40, 31, 40, 48), 'os.walk', 'os.walk', ({(40, 39, 40, 47): 'cur_path'}, {}), '(cur_path)', False, 'import os\n'), ((11, 12, 11, 43), 'os.path.join', 'os.path.join', ({(11, 25, 11, 32): 'src_dir', (11, 34, 11, 42): 'filename'}, {}), '(src_dir, filename)', False, 'import os\n')]