Add get_single_key_and_value() helper function

This commit is contained in:
Daniel Rojas 2020-11-15 17:45:22 +01:00
parent 81940469ac
commit 39698a3d61
2 changed files with 16 additions and 18 deletions

View File

@ -14,7 +14,7 @@ if __name__ == '__main__':
from wireviz import __version__ from wireviz import __version__
from wireviz.Harness import Harness from wireviz.Harness import Harness
from wireviz.wv_helper import expand, open_file_read, isarrow from wireviz.wv_helper import expand, open_file_read, isarrow, get_single_key_and_value
def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any: def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any:
@ -190,33 +190,25 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
if designator in harness.cables: if designator in harness.cables:
if index_item == 0: # list started with a cable, no connector to join on left side if index_item == 0: # list started with a cable, no connector to join on left side
from_name = None from_name, from_pin = (None, None)
from_pin = None
else: else:
from_name = list(connection_set[index_entry][index_item-1].keys())[0] from_name, from_pin = get_single_key_and_value(connection_set[index_entry][index_item-1])
from_pin = connection_set[index_entry][index_item-1][from_name] via_name, via_pin = (designator, item[designator])
via_name = designator
via_pin = item[designator]
if index_item == len(entry) - 1: # list ends with a cable, no connector to join on right side if index_item == len(entry) - 1: # list ends with a cable, no connector to join on right side
to_name = None to_name, to_pin = (None, None)
to_pin = None
else: else:
to_name = list(connection_set[index_entry][index_item+1].keys())[0] to_name, to_pin = get_single_key_and_value(connection_set[index_entry][index_item+1])
to_pin = connection_set[index_entry][index_item+1][to_name]
harness.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)
elif isarrow(designator): elif isarrow(designator):
if index_item == 0: # list startess with an arrow if index_item == 0: # list starts with an arrow
raise Exception('An arrow cannot be at the start of a connection set') raise Exception('An arrow cannot be at the start of a connection set')
elif index_item == len(entry) - 1: # list ends with an arrow elif index_item == len(entry) - 1: # list ends with an arrow
raise Exception('An arrow cannot be at the end of a connection set') raise Exception('An arrow cannot be at the end of a connection set')
from_name = list(connection_set[index_entry][index_item-1].keys())[0] from_name, from_pin = get_single_key_and_value(connection_set[index_entry][index_item-1])
from_pin = connection_set[index_entry][index_item-1][from_name] via_name, via_pin = (designator, None)
via_name = designator to_name, to_pin = get_single_key_and_value(connection_set[index_entry][index_item+1])
via_pin = None
to_name = list(connection_set[index_entry][index_item+1].keys())[0]
to_pin = connection_set[index_entry][index_item+1][to_name]
if '-' in designator: # mate pin by pin if '-' in designator: # mate pin by pin
harness.add_mate_pin(from_name, from_pin, to_name, to_pin, designator) harness.add_mate_pin(from_name, from_pin, to_name, to_pin, designator)
elif '=' in designator and index_entry == 0: # mate two connectors as a whole elif '=' in designator and index_entry == 0: # mate two connectors as a whole

View File

@ -66,6 +66,12 @@ def expand(yaml_data):
return output return output
def get_single_key_and_value(d: dict):
k = list(d.keys())[0]
v = d[k]
return (k, v)
def int2tuple(inp): def int2tuple(inp):
if isinstance(inp, tuple): if isinstance(inp, tuple):
output = inp output = inp