diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index d89f04a..2368738 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -190,162 +190,220 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st + print('TRANSPOSE!!') + # print(connection_set) + # print(list(zip(connection_set))) - quit() - - connections = [] - for yaml_connection in yaml_data['connections']: - - print('connection set:') - - connection = [] - for entry in yaml_connection: - if isinstance(entry, list): - itemlist = entry - entrytype = 'list' - elif isinstance(entry, dict): - itemlist = [list(entry.keys())[0]] - entrytype = 'dict' - elif isinstance(entry, str): - itemlist = [entry] - entrytype = 'str' - - print(' ', itemlist) - - arrows = ['--', '<--', '<->', '-->','==', '<==', '<=>', '==>'] - - outputlist = [] - for item in itemlist: - - if item in arrows: - print(' ', item, 'arrow!') - else: - if '.' in item: # generate a new instance of an item - template, designator = item.split('.') # TODO: handle more than one `.` - if designator =='': - autogenerated_designators[template] = autogenerated_designators.get(template, 0) + 1 - designator = f'_{template}_{autogenerated_designators[template]}' - print('new autogen id', designator, 'for', template) - print(' ', template, '-->', designator) - else: # use the item directly - template = item - designator = item - print(' ', designator) - - if designator in harness.connectors: - print(' ', designator, 'is an existing connector instance') - elif template in template_connector_names: - print(' ', designator, 'is a new connector instance of type', template) - # print(template_connectors[template]) - harness.add_connector(name = designator, **template_connectors[template]) - # harness.connectors[designator] = templates.connectors[template] - # harness.connectors[designator].name = designator - - elif designator in harness.cables: - print(' ', designator, 'is an existing cable instance') - elif template in template_cable_names: - print(' ', designator, 'is a new cable instance of type', template) - harness.add_cable(name = designator, **template_cables[template]) - # harness.cables[designator] = templates.cables[template] - # harness.cables[designator].name = designator - - else: - print(f' {template} TEMPLATE not found, neither in connectors nor in cables') - - outputlist.append(designator) - - if not itemlist[0] in arrows: - if entrytype == 'list': - connection_entry = outputlist - elif entrytype == 'dict': - connection_entry = {outputlist[0]: list(entry.values())[0]} - elif entrytype == 'str': - connection_entry = outputlist[0] - print(connection_entry) - - connection.append(connection_entry) - - print('connection', connection) - - - - # check which section the first item belongs to - alternating_sections = ['connectors','cables'] - alternating_section_keys = [list(section.keys()) for section in [harness.connectors, harness.cables]] - print('alt_sec_keys', alternating_section_keys) - - for index, section in enumerate(alternating_section_keys): - print('1st', first_item) - if first_item in section: - expected_index = index - print(first_item, 'found in ', alternating_sections[index]) - break - else: - raise Exception('First item not found anywhere.') - expected_index = 1 - expected_index # flip once since it is flipped back at the *beginning* of every loop - - - # populate connection list - connection_list = [] - for i, item in enumerate(connection): - if isinstance(item, str): # one single-pin component was specified - sublist = [] - for i in range(1, itemcount + 1): - # if yaml_data['connectors'][item].get('autogenerate'): - # autogenerated_designators[item] = autogenerated_designators.get(item, 0) + 1 - # new_id = f'_{item}_{autogenerated_designators[item]}' - # harness.add_connector(new_id, **yaml_data['connectors'][item]) - # sublist.append([new_id, 1]) - # else: - sublist.append([item, 1]) - connection_list.append(sublist) - elif isinstance(item, list): # a list of single-pin components were specified - sublist = [] - for subitem in item: - # if yaml_data['connectors'][subitem].get('autogenerate'): - # autogenerated_designators[subitem] = autogenerated_designators.get(subitem, 0) + 1 - # new_id = f'_{subitem}_{autogenerated_designators[subitem]}' - # harness.add_connector(new_id, **yaml_data['connectors'][subitem]) - # sublist.append([new_id, 1]) - # else: - sublist.append([subitem, 1]) - connection_list.append(sublist) - elif isinstance(item, dict): # a component with multiple pins was specified - sublist = [] - id = list(item.keys())[0] - pins = expand(list(item.values())[0]) - for pin in pins: - sublist.append([id, pin]) - connection_list.append(sublist) - else: - raise Exception('Unexpected item in connection list') - - print('connection list') - print(connection_list) - print('') + connection_set = list(map(list, zip(*connection_set))) # transpose list + print(connection_set) # actually connect components using connection list - for i, item in enumerate(connection_list): - id = item[0][0] # TODO: make more elegant/robust/pythonic - if id in harness.cables: - for j, con in enumerate(item): - if i == 0: # list started with a cable, no connector to join on left side + for index_connection, connection in enumerate(connection_set): + print(f' connection ic {index_connection}', connection) + for index_item, item in enumerate(connection): + print(f' item ii {index_item}', item) + designator = list(item.keys())[0] + if designator in harness.cables: + print(f' - {designator} is a known cable') + if index_item == 0: # list started with a cable, no connector to join on left side from_name = None from_pin = None else: - from_name = connection_list[i-1][j][0] - from_pin = connection_list[i-1][j][1] - via_name = item[j][0] - via_pin = item[j][1] - if i == len(connection_list) - 1: # list ends with a cable, no connector to join on right side + from_name = list(connection_set[index_connection][index_item-1].keys())[0] + from_pin = connection_set[index_connection][index_item-1][from_name] + via_name = designator + via_pin = item[designator] + if index_item == len(connection) - 1: # list ends with a cable, no connector to join on right side to_name = None to_pin = None else: - to_name = connection_list[i+1][j][0] - to_pin = connection_list[i+1][j][1] - print('connect ', from_name, from_pin, via_name, via_pin, to_name, to_pin) + to_name = list(connection_set[index_connection][index_item+1].keys())[0] + to_pin = connection_set[index_connection][index_item+1][to_name] + print(' > connect ', from_name, from_pin, via_name, via_pin, to_name, to_pin) harness.connect(from_name, from_pin, via_name, via_pin, to_name, to_pin) + + + + # for i, item in enumerate(connection_set): + # print('item', item) + # id = list(item[0].keys())[0] # TODO: make more elegant/robust/pythonic + # print('id', id) + # if id in harness.cables: + # for j, con in enumerate(item): + # if i == 0: # list started with a cable, no connector to join on left side + # from_name = None + # from_pin = None + # else: + # from_name = list(connection_set[i-1].keys())[0] + # from_pin = connection_set[i-1][from_name] + # via_name = id + # via_pin = item[j][1] + # if i == len(connection_set) - 1: # list ends with a cable, no connector to join on right side + # to_name = None + # to_pin = None + # else: + # to_name = list(connection_set[i+1].keys())[0] + # to_pin = connection_set[i-1][to_name] + # print('connect ', from_name, from_pin, via_name, via_pin, to_name, to_pin) + # harness.connect(from_name, from_pin, via_name, via_pin, to_name, to_pin) + + + # quit() + + # connections = [] + # for yaml_connection in yaml_data['connections']: + # + # print('connection set:') + # + # connection = [] + # for entry in yaml_connection: + # if isinstance(entry, list): + # itemlist = entry + # entrytype = 'list' + # elif isinstance(entry, dict): + # itemlist = [list(entry.keys())[0]] + # entrytype = 'dict' + # elif isinstance(entry, str): + # itemlist = [entry] + # entrytype = 'str' + # + # print(' ', itemlist) + # + # arrows = ['--', '<--', '<->', '-->','==', '<==', '<=>', '==>'] + # + # outputlist = [] + # for item in itemlist: + # + # if item in arrows: + # print(' ', item, 'arrow!') + # else: + # if '.' in item: # generate a new instance of an item + # template, designator = item.split('.') # TODO: handle more than one `.` + # if designator =='': + # autogenerated_designators[template] = autogenerated_designators.get(template, 0) + 1 + # designator = f'_{template}_{autogenerated_designators[template]}' + # print('new autogen id', designator, 'for', template) + # print(' ', template, '-->', designator) + # else: # use the item directly + # template = item + # designator = item + # print(' ', designator) + # + # if designator in harness.connectors: + # print(' ', designator, 'is an existing connector instance') + # elif template in template_connector_names: + # print(' ', designator, 'is a new connector instance of type', template) + # # print(template_connectors[template]) + # harness.add_connector(name = designator, **template_connectors[template]) + # # harness.connectors[designator] = templates.connectors[template] + # # harness.connectors[designator].name = designator + # + # elif designator in harness.cables: + # print(' ', designator, 'is an existing cable instance') + # elif template in template_cable_names: + # print(' ', designator, 'is a new cable instance of type', template) + # harness.add_cable(name = designator, **template_cables[template]) + # # harness.cables[designator] = templates.cables[template] + # # harness.cables[designator].name = designator + # + # else: + # print(f' {template} TEMPLATE not found, neither in connectors nor in cables') + # + # outputlist.append(designator) + # + # if not itemlist[0] in arrows: + # if entrytype == 'list': + # connection_entry = outputlist + # elif entrytype == 'dict': + # connection_entry = {outputlist[0]: list(entry.values())[0]} + # elif entrytype == 'str': + # connection_entry = outputlist[0] + # print(connection_entry) + # + # connection.append(connection_entry) + # + # print('connection', connection) + # + # + # + # # check which section the first item belongs to + # alternating_sections = ['connectors','cables'] + # alternating_section_keys = [list(section.keys()) for section in [harness.connectors, harness.cables]] + # print('alt_sec_keys', alternating_section_keys) + # + # for index, section in enumerate(alternating_section_keys): + # print('1st', first_item) + # if first_item in section: + # expected_index = index + # print(first_item, 'found in ', alternating_sections[index]) + # break + # else: + # raise Exception('First item not found anywhere.') + # expected_index = 1 - expected_index # flip once since it is flipped back at the *beginning* of every loop + # + # + # # populate connection list + # connection_list = [] + # for i, item in enumerate(connection): + # if isinstance(item, str): # one single-pin component was specified + # sublist = [] + # for i in range(1, itemcount + 1): + # # if yaml_data['connectors'][item].get('autogenerate'): + # # autogenerated_designators[item] = autogenerated_designators.get(item, 0) + 1 + # # new_id = f'_{item}_{autogenerated_designators[item]}' + # # harness.add_connector(new_id, **yaml_data['connectors'][item]) + # # sublist.append([new_id, 1]) + # # else: + # sublist.append([item, 1]) + # connection_list.append(sublist) + # elif isinstance(item, list): # a list of single-pin components were specified + # sublist = [] + # for subitem in item: + # # if yaml_data['connectors'][subitem].get('autogenerate'): + # # autogenerated_designators[subitem] = autogenerated_designators.get(subitem, 0) + 1 + # # new_id = f'_{subitem}_{autogenerated_designators[subitem]}' + # # harness.add_connector(new_id, **yaml_data['connectors'][subitem]) + # # sublist.append([new_id, 1]) + # # else: + # sublist.append([subitem, 1]) + # connection_list.append(sublist) + # elif isinstance(item, dict): # a component with multiple pins was specified + # sublist = [] + # id = list(item.keys())[0] + # pins = expand(list(item.values())[0]) + # for pin in pins: + # sublist.append([id, pin]) + # connection_list.append(sublist) + # else: + # raise Exception('Unexpected item in connection list') + # + # print('connection list') + # print(connection_list) + # print('') + # + # # actually connect components using connection list + # for i, item in enumerate(connection_list): + # id = item[0][0] # TODO: make more elegant/robust/pythonic + # if id in harness.cables: + # for j, con in enumerate(item): + # if i == 0: # list started with a cable, no connector to join on left side + # from_name = None + # from_pin = None + # else: + # from_name = connection_list[i-1][j][0] + # from_pin = connection_list[i-1][j][1] + # via_name = item[j][0] + # via_pin = item[j][1] + # if i == len(connection_list) - 1: # list ends with a cable, no connector to join on right side + # to_name = None + # to_pin = None + # else: + # to_name = connection_list[i+1][j][0] + # to_pin = connection_list[i+1][j][1] + # print('connect ', from_name, from_pin, via_name, via_pin, to_name, to_pin) + # harness.connect(from_name, from_pin, via_name, via_pin, to_name, to_pin) + # quit () if "additional_bom_items" in yaml_data: