WIP: first working example of new . notation
This commit is contained in:
parent
c6b2375e5c
commit
a50c7794c5
@ -34,7 +34,9 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
|
||||
yaml_data = yaml.safe_load(yaml_input)
|
||||
|
||||
harness = Harness()
|
||||
# templates = Harness() # store templates from YAML `connectors` and `cables` sections
|
||||
template_connectors = {}
|
||||
template_cables = {}
|
||||
|
||||
# add items
|
||||
sections = ['connectors', 'cables', 'connections']
|
||||
@ -50,10 +52,11 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
image['gv_dir'] = Path(file_out if file_out else '').parent # Inject context
|
||||
|
||||
if sec == 'connectors':
|
||||
if not attribs.get('autogenerate', False):
|
||||
harness.add_connector(name=key, **attribs)
|
||||
print(key, 'con')
|
||||
template_connectors[key] = attribs
|
||||
elif sec == 'cables':
|
||||
harness.add_cable(name=key, **attribs)
|
||||
print(key, 'cbl')
|
||||
template_cables[key] = attribs
|
||||
else:
|
||||
pass # section exists but is empty
|
||||
else: # section does not exist, create empty section
|
||||
@ -64,8 +67,98 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
|
||||
# add connections
|
||||
|
||||
harness = Harness() # populate actual harness with instances generated in `connections` section
|
||||
|
||||
template_connector_names = list(template_connectors.keys())
|
||||
template_cable_names = list(template_cables.keys())
|
||||
|
||||
print('template connectors: ', template_connector_names)
|
||||
print('template cables: ', template_cable_names)
|
||||
|
||||
# print(template_connectors)
|
||||
|
||||
autogenerated_ids = {}
|
||||
for connection in yaml_data['connections']:
|
||||
connections = []
|
||||
for yaml_connection in yaml_data['connections']:
|
||||
|
||||
print('connection set:')
|
||||
|
||||
connectionset = []
|
||||
for entry in yaml_connection:
|
||||
if isinstance(entry, list):
|
||||
itemlist = entry
|
||||
elif isinstance(entry, dict):
|
||||
itemlist = [list(entry.keys())[0]]
|
||||
elif isinstance(entry, str):
|
||||
itemlist = [entry]
|
||||
|
||||
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_ids[template] = autogenerated_ids.get(template, 0) + 1
|
||||
designator = f'_{template}_{autogenerated_ids[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 isinstance(entry, list):
|
||||
connectionset_entry = outputlist
|
||||
elif isinstance(entry, dict):
|
||||
connectionset_entry = {outputlist[0]: list(entry.values())[0]}
|
||||
elif isinstance(entry, str):
|
||||
connectionset_entry = outputlist[0]
|
||||
print(connectionset_entry)
|
||||
|
||||
connectionset.append(connectionset_entry)
|
||||
|
||||
connections.append(connectionset)
|
||||
|
||||
print('--- Done building connection list ---')
|
||||
|
||||
print(connections)
|
||||
|
||||
print('')
|
||||
|
||||
for connection in connections:
|
||||
|
||||
print('conset', connection)
|
||||
|
||||
# find first component (potentially nested inside list or dict)
|
||||
first_item = connection[0]
|
||||
if isinstance(first_item, list):
|
||||
@ -77,9 +170,14 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
|
||||
# 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]:
|
||||
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.')
|
||||
@ -94,17 +192,17 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
if isinstance(item, list):
|
||||
itemcount_new = len(item)
|
||||
for subitem in item:
|
||||
if not subitem in yaml_data[expected_section]:
|
||||
if not subitem in alternating_section_keys[expected_index]:
|
||||
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 key here!')
|
||||
itemcount_new = len(expand(list(item.values())[0]))
|
||||
subitem = list(item.keys())[0]
|
||||
if not subitem in yaml_data[expected_section]:
|
||||
if not subitem in alternating_section_keys[expected_index]:
|
||||
raise Exception(f'{subitem} is not in {expected_section}')
|
||||
elif isinstance(item, str):
|
||||
if not item in yaml_data[expected_section]:
|
||||
if not item in alternating_section_keys[expected_index]:
|
||||
raise Exception(f'{item} is not in {expected_section}')
|
||||
continue
|
||||
if itemcount is not None and itemcount_new != itemcount:
|
||||
@ -119,23 +217,23 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
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_ids[item] = autogenerated_ids.get(item, 0) + 1
|
||||
new_id = f'_{item}_{autogenerated_ids[item]}'
|
||||
harness.add_connector(new_id, **yaml_data['connectors'][item])
|
||||
sublist.append([new_id, 1])
|
||||
else:
|
||||
# if yaml_data['connectors'][item].get('autogenerate'):
|
||||
# autogenerated_ids[item] = autogenerated_ids.get(item, 0) + 1
|
||||
# new_id = f'_{item}_{autogenerated_ids[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_ids[subitem] = autogenerated_ids.get(subitem, 0) + 1
|
||||
new_id = f'_{subitem}_{autogenerated_ids[subitem]}'
|
||||
harness.add_connector(new_id, **yaml_data['connectors'][subitem])
|
||||
sublist.append([new_id, 1])
|
||||
else:
|
||||
# if yaml_data['connectors'][subitem].get('autogenerate'):
|
||||
# autogenerated_ids[subitem] = autogenerated_ids.get(subitem, 0) + 1
|
||||
# new_id = f'_{subitem}_{autogenerated_ids[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
|
||||
@ -148,6 +246,10 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
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
|
||||
@ -167,8 +269,11 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
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:
|
||||
for line in yaml_data["additional_bom_items"]:
|
||||
harness.add_bom_item(line)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user