Ensure items in a connection set alternate between connectors and cables
This commit is contained in:
parent
b988e9b063
commit
cf6d3676c4
@ -67,19 +67,46 @@ def parse(yaml_input, file_out=None, generate_bom=False, return_types: (None, st
|
||||
|
||||
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 parse(yaml_input, file_out=None, generate_bom=False, return_types: (None, st
|
||||
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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user