hil2.net_map
1import csv 2 3from . import hil_errors 4 5 6# Board net pairing -------------------------------------------------------------------# 7class BoardNet: 8 """ 9 Represents a board/net combination in the net map (ex: 'Dashboard/BRK_STAT'). 10 Can be used as a key in a dictionary. 11 """ 12 13 def __init__(self, board: str, net: str): 14 """ 15 :param board: The name of the board (ex: 'Dashboard') 16 :param net: The name of the net (ex: 'BRK_STAT') 17 """ 18 self.board: str = board 19 self.net: str = net 20 21 def __hash__(self): 22 return hash((self.board, self.net)) 23 24 def __eq__(self, other): 25 return self.board == other.board and self.net == other.net 26 27 def __neq__(self, other): 28 return not (self == other) 29 30 31# CSV entry ---------------------------------------------------------------------------# 32class NetMapEntry: 33 """Represents a row in the net map CSV file.""" 34 35 def __init__(self, board: str, net: str, component: str, designator: int): 36 """ 37 :param board: The name of the board (ex: 'Dashboard') 38 :param net: The name of the net (ex: 'BRK_STAT') 39 :param component: The name of the component (ex: 'J3') 40 :param designator: The designator number (ex: 9) 41 """ 42 self.board = board 43 self.net = net 44 self.component = component 45 self.designator = designator 46 47 48# Net map -----------------------------------------------------------------------------# 49class NetMap: 50 def __init__(self, entries: dict[BoardNet, NetMapEntry]): 51 """ 52 :param entries: A dictionary mapping board/net combinations to their net map 53 entries 54 """ 55 self._entries: dict[BoardNet, NetMapEntry] = entries 56 57 def get_entry(self, board: str, net: str) -> NetMapEntry: 58 """ 59 Retrieves a net map entry by board and net name. 60 61 :param board: The name of the board (ex: 'Dashboard') 62 :param net: The name of the net (ex: 'BRK_STAT') 63 :return: The net map entry for the specified board and net 64 """ 65 board_net = BoardNet(board, net) 66 if board_net in self._entries: 67 return self._entries[board_net] 68 else: 69 raise hil_errors.ConnectionError(f"No net map entry found for: {board_net}") 70 71 @classmethod 72 def from_csv(cls, file_path: str) -> "NetMap": 73 """ 74 Creates a NetMap instance from a CSV file. 75 76 :param file_path: The path to the CSV file 77 :return: A NetMap instance 78 """ 79 entries = {} 80 with open(file_path, newline="", encoding="utf-8") as net_map_file: 81 reader = csv.DictReader(net_map_file) 82 for row in reader: 83 match row: 84 case { 85 "Board": board, 86 "Net": net, 87 "Component": component, 88 "Designator": designator_str, 89 }: 90 entry = NetMapEntry( 91 board=board, 92 net=net, 93 component=component, 94 designator=int(designator_str), 95 ) 96 board_net = BoardNet(entry.board, entry.net) 97 entries[board_net] = entry 98 case _: 99 raise hil_errors.NetMapParseError(f"Invalid net map row: {row}") 100 return cls(entries)
class
BoardNet:
8class BoardNet: 9 """ 10 Represents a board/net combination in the net map (ex: 'Dashboard/BRK_STAT'). 11 Can be used as a key in a dictionary. 12 """ 13 14 def __init__(self, board: str, net: str): 15 """ 16 :param board: The name of the board (ex: 'Dashboard') 17 :param net: The name of the net (ex: 'BRK_STAT') 18 """ 19 self.board: str = board 20 self.net: str = net 21 22 def __hash__(self): 23 return hash((self.board, self.net)) 24 25 def __eq__(self, other): 26 return self.board == other.board and self.net == other.net 27 28 def __neq__(self, other): 29 return not (self == other)
Represents a board/net combination in the net map (ex: 'Dashboard/BRK_STAT'). Can be used as a key in a dictionary.
BoardNet(board: str, net: str)
14 def __init__(self, board: str, net: str): 15 """ 16 :param board: The name of the board (ex: 'Dashboard') 17 :param net: The name of the net (ex: 'BRK_STAT') 18 """ 19 self.board: str = board 20 self.net: str = net
Parameters
- board: The name of the board (ex: 'Dashboard')
- net: The name of the net (ex: 'BRK_STAT')
class
NetMapEntry:
33class NetMapEntry: 34 """Represents a row in the net map CSV file.""" 35 36 def __init__(self, board: str, net: str, component: str, designator: int): 37 """ 38 :param board: The name of the board (ex: 'Dashboard') 39 :param net: The name of the net (ex: 'BRK_STAT') 40 :param component: The name of the component (ex: 'J3') 41 :param designator: The designator number (ex: 9) 42 """ 43 self.board = board 44 self.net = net 45 self.component = component 46 self.designator = designator
Represents a row in the net map CSV file.
NetMapEntry(board: str, net: str, component: str, designator: int)
36 def __init__(self, board: str, net: str, component: str, designator: int): 37 """ 38 :param board: The name of the board (ex: 'Dashboard') 39 :param net: The name of the net (ex: 'BRK_STAT') 40 :param component: The name of the component (ex: 'J3') 41 :param designator: The designator number (ex: 9) 42 """ 43 self.board = board 44 self.net = net 45 self.component = component 46 self.designator = designator
Parameters
- board: The name of the board (ex: 'Dashboard')
- net: The name of the net (ex: 'BRK_STAT')
- component: The name of the component (ex: 'J3')
- designator: The designator number (ex: 9)
class
NetMap:
50class NetMap: 51 def __init__(self, entries: dict[BoardNet, NetMapEntry]): 52 """ 53 :param entries: A dictionary mapping board/net combinations to their net map 54 entries 55 """ 56 self._entries: dict[BoardNet, NetMapEntry] = entries 57 58 def get_entry(self, board: str, net: str) -> NetMapEntry: 59 """ 60 Retrieves a net map entry by board and net name. 61 62 :param board: The name of the board (ex: 'Dashboard') 63 :param net: The name of the net (ex: 'BRK_STAT') 64 :return: The net map entry for the specified board and net 65 """ 66 board_net = BoardNet(board, net) 67 if board_net in self._entries: 68 return self._entries[board_net] 69 else: 70 raise hil_errors.ConnectionError(f"No net map entry found for: {board_net}") 71 72 @classmethod 73 def from_csv(cls, file_path: str) -> "NetMap": 74 """ 75 Creates a NetMap instance from a CSV file. 76 77 :param file_path: The path to the CSV file 78 :return: A NetMap instance 79 """ 80 entries = {} 81 with open(file_path, newline="", encoding="utf-8") as net_map_file: 82 reader = csv.DictReader(net_map_file) 83 for row in reader: 84 match row: 85 case { 86 "Board": board, 87 "Net": net, 88 "Component": component, 89 "Designator": designator_str, 90 }: 91 entry = NetMapEntry( 92 board=board, 93 net=net, 94 component=component, 95 designator=int(designator_str), 96 ) 97 board_net = BoardNet(entry.board, entry.net) 98 entries[board_net] = entry 99 case _: 100 raise hil_errors.NetMapParseError(f"Invalid net map row: {row}") 101 return cls(entries)
NetMap(entries: dict[BoardNet, NetMapEntry])
51 def __init__(self, entries: dict[BoardNet, NetMapEntry]): 52 """ 53 :param entries: A dictionary mapping board/net combinations to their net map 54 entries 55 """ 56 self._entries: dict[BoardNet, NetMapEntry] = entries
Parameters
- entries: A dictionary mapping board/net combinations to their net map entries
58 def get_entry(self, board: str, net: str) -> NetMapEntry: 59 """ 60 Retrieves a net map entry by board and net name. 61 62 :param board: The name of the board (ex: 'Dashboard') 63 :param net: The name of the net (ex: 'BRK_STAT') 64 :return: The net map entry for the specified board and net 65 """ 66 board_net = BoardNet(board, net) 67 if board_net in self._entries: 68 return self._entries[board_net] 69 else: 70 raise hil_errors.ConnectionError(f"No net map entry found for: {board_net}")
Retrieves a net map entry by board and net name.
Parameters
- board: The name of the board (ex: 'Dashboard')
- net: The name of the net (ex: 'BRK_STAT')
Returns
The net map entry for the specified board and net
72 @classmethod 73 def from_csv(cls, file_path: str) -> "NetMap": 74 """ 75 Creates a NetMap instance from a CSV file. 76 77 :param file_path: The path to the CSV file 78 :return: A NetMap instance 79 """ 80 entries = {} 81 with open(file_path, newline="", encoding="utf-8") as net_map_file: 82 reader = csv.DictReader(net_map_file) 83 for row in reader: 84 match row: 85 case { 86 "Board": board, 87 "Net": net, 88 "Component": component, 89 "Designator": designator_str, 90 }: 91 entry = NetMapEntry( 92 board=board, 93 net=net, 94 component=component, 95 designator=int(designator_str), 96 ) 97 board_net = BoardNet(entry.board, entry.net) 98 entries[board_net] = entry 99 case _: 100 raise hil_errors.NetMapParseError(f"Invalid net map row: {row}") 101 return cls(entries)
Creates a NetMap instance from a CSV file.
Parameters
- file_path: The path to the CSV file
Returns
A NetMap instance