SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
id_matcher.py
1import json
2from multiprocessing import BoundedSemaphore
3
4import script.error as Error
5import script.util as Util
6
7
8LIMIT_TAG = 65535
9LIMIT_ID = 65535
10BLACKLIST_IDS = (0, 1)
11
12TAG_RANGE_STATE = range(2, 256)
13TAG_RANGE_VALUE = range(256, 4096)
14TAG_RANGE_ELSE = range(4096, LIMIT_TAG)
15
16ID_RANGE_VALUE = range(256, 4096)
17ID_RANGE_ELSE = list(range(2, 256)) + list(range(4096, LIMIT_ID))
18
19TAGs = {}
20IDs = {}
21Files = {}
22
23TAG_SEM = BoundedSemaphore(1)
24ID_SEM = BoundedSemaphore(1)
25FILE_SEM = BoundedSemaphore(1)
26
27
28async def map_unique_pair(string_tag: str, string_id: str, map_range: range) -> int:
29 """Allocates both a TAG and ID mapping where they both map to the same number.
30
31 Args:
32 string_tag (str): The string tag to map
33 string_id (str): The string message to map
34 map_range (range): The range the mapping should be in
35
36 Raises:
37 Error.TAGIDMismatchException: If both mappings already exist and do not match in value
38 Error.OutOfRangeException: If either mapping is unable to allocate another value within the given range
39
40 Returns:
41 int: The value mapping to both the string tag and message
42 """
43 if TAGs.get(string_tag) and IDs.get(string_id): # TODO: attempt to match one mapping to the other if one already exists
44 if TAGs[string_tag] != IDs[string_id]:
45 raise Error.TAGIDMismatchException(f"{string_tag} : {string_id}")
46 return TAGs[string_tag]
47
48 with ID_SEM and TAG_SEM:
49 ids = set(IDs.values())
50 tags = set(TAGs.values())
51
52 for i in map_range:
53 if i not in BLACKLIST_IDS and i not in ids and i not in tags:
54 IDs[string_id] = i
55 TAGs[string_tag] = i
56 return i
57
58 raise Error.OutOfRangeException("Either IDs or TAGs are out")
59
60
61async def map_unique_id(string_id: str, map_range: range = ID_RANGE_ELSE) -> int:
62 """Allocates an ID mapping
63
64 Args:
65 string_id (str): The string message to map
66 map_range (range, optional): The range the mapping should be in. Defaults to ID_RANGE_ELSE.
67
68 Raises:
69 Error.OutOfRangeException: If mapping is unable to allocate another value within the given range
70
71 Returns:
72 int: The value mapping to the string message
73 """
74 if IDs.get(string_id):
75 return IDs[string_id]
76
77 with ID_SEM:
78 values = set(IDs.values())
79
80 for i in map_range:
81 if i not in BLACKLIST_IDS and i not in values:
82 IDs[string_id] = i
83 return i
84
85 # Attempt to clear up some IDs
86 raise Error.OutOfRangeException("IDs")
87
88
89async def map_unique_tag(string_tag: str, map_range: range = TAG_RANGE_ELSE) -> int:
90 """Allocates a TAG mapping
91
92 Args:
93 string_tag (str): The string tag to map
94 map_range (range, optional): The range the mapping should be in. Defaults to TAG_RANGE_ELSE.
95
96 Raises:
97 Error.OutOfRangeException: If mapping is unable to allocate another value within the given range
98
99 Returns:
100 int: The value mapping to the string tag
101 """
102 if TAGs.get(string_tag):
103 return TAGs[string_tag]
104
105 with TAG_SEM:
106 values = set(TAGs.values())
107
108 for i in map_range:
109 if i not in BLACKLIST_IDS and i not in values:
110 TAGs[string_tag] = i
111 return i
112
113 # Attempt to clear up some IDs
114 raise Error.OutOfRangeException("TAGs")
115
116
117def clear_blanks() -> None:
118 """Remove empty strings mappings, if any"""
119 try:
120 IDs.pop("")
121 except KeyError:
122 pass
123 try:
124 TAGs.pop("")
125 except KeyError:
126 pass
127
128
129def save_lookup(file_path: str) -> None:
130 """Saves current mappings
131
132 Args:
133 file_path (str): Valid path to a file to save to
134 """
135 to_save = (TAGs, IDs)
136 Util.touch(file_path)
137 with open(file_path, "w", encoding="utf-8") as file:
138 json.dump(to_save, file, indent=4, separators=(",", ": "))
Error related items.
Definition error.py:1
Misc.
Definition util.py:1