Skip to content

Commit

Permalink
Ensure items in a connection set alternate between connectors and cables
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Jul 11, 2020
1 parent b988e9b commit 0c8bf82
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0c8bf82

Please sign in to comment.