diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 84fb1821..57016487 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!') @@ -124,7 +151,7 @@ def check_designators(what, where): # helper function 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: