From cf6d3676c405574bad15f87321f318e88f52fc90 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 11 Jul 2020 11:25:59 +0200 Subject: [PATCH] Ensure items in a connection set alternate between connectors and cables --- src/wireviz/wireviz.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 84fb1821..672dbdd4 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -67,19 +67,46 @@ def check_designators(what, where): # helper function autogenerated_ids = {} for connection in yaml_data['connections']: - # TODO: check that items are of alternating type CONNECTOR/FERRULE/FERRULE_LIST and CABLE/WIRE - # TODO: special case: loops! + # find first component (potentially nested inside list or dict) + first_item = connection[0] + if isinstance(first_item, list): + first_item = first_item[0] + elif isinstance(first_item, dict): + first_item = list(first_item.keys())[0] + elif isinstance(first_item, str): + pass + + # check which section the first item belongs to + alternating_sections = ['connectors','cables'] + for index, section in enumerate(alternating_sections): + if first_item in yaml_data[section]: + expected_index = 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 # check that all iterable items (lists and dicts) are the same length + # and that they are alternating between connectors and cables/bundles, starting with either itemcount = None for item in connection: + expected_index = 1 - expected_index # make sure items alternate between connectors and cables + expected_section = alternating_sections[expected_index] if isinstance(item, list): itemcount_new = len(item) + for subitem in item: + if not subitem in yaml_data[expected_section]: + raise Exception(f'{subitem} is not in {expected_section}') elif isinstance(item, dict): if len(item.keys()) != 1: - raise Exception('Dicts may contain only one item here!') + raise Exception('Dicts may contain only one key here!') itemcount_new = len(expand(list(item.values())[0])) + subitem = list(item.keys())[0] + if not subitem in yaml_data[expected_section]: + raise Exception(f'{subitem} is not in {expected_section}') elif isinstance(item, str): + if not item in yaml_data[expected_section]: + raise Exception(f'{item} is not in {expected_section}') continue if itemcount is not None and itemcount_new != itemcount: raise Exception('All lists and dict lists must be the same length!') @@ -119,12 +146,10 @@ def check_designators(what, where): # helper function for pin in pins: sublist.append([id, pin]) connection_list.append(sublist) - elif False: # TODO: placeholer; a loop inside a connector was specified - pass else: raise Exception('Unexpected item in connection list') - # actually connect things using connection list + # 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: