-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
133 lines (113 loc) · 4.88 KB
/
builder.py
File metadata and controls
133 lines (113 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from typing import Optional, Dict, List
from data_types import ISA51BaseComponentsMatch, MatchTable, ISA51Id, \
ISA51Component
from static_source import id_letters, active_full_name_headers, \
passive_full_name_headers, refs_codes, VARIABLE_MODIFIERS, \
ACTIVE_FUNCTIONS, FUNCTION_MODIFIERS, PASSIVE_FUNCTIONS, VARIABLES
class ComponentBuilder:
"""
A utility class for building ISA51Component instances based on provided
names and letter identifiers.
This class provides methods to construct ISA51Component objects with the
associated reference codes and text descriptions. It validates the input
data against predefined mappings and ensures components are created accurately.
"""
@classmethod
def build_component(cls, name: str, letters: Optional[str]) -> Optional[
ISA51Component]:
if letters in [None, ""]:
return None
if name not in id_letters or letters not in id_letters[name]:
return None
ref_codes = id_letters[name][letters]["refs"]
return ISA51Component(
name=id_letters[name][letters]["name"],
refs_codes=ref_codes,
refs_text=cls._get_refs_text(ref_codes),
letter=letters
)
@staticmethod
def _get_refs_text(refs: List[str]) -> Dict[str, str]:
out = {}
for ref in refs:
if ref in refs_codes:
out[ref] = refs_codes[ref]
return out
class ISA51Builder:
"""
Responsible for building ISA-51 identifiers by processing various components
such as variable modifiers, passive and active functions, and constructing
a structured identifier object.
The class utilizes predefined match tables and components, builds the required
attributes, and organizes them into an ISA-51 identifier, ensuring all necessary
components are assigned appropriately.
:ivar cb: An instance of ComponentBuilder used to process and build individual
components such as function modifiers, variables, etc.
:type cb: ComponentBuilder
"""
cb = ComponentBuilder()
def build_isa_51_id(self,
match: ISA51BaseComponentsMatch,
table: MatchTable) -> ISA51Id:
func_name = self._get_function_name(match, table)
variable_modifiers = []
for vm in match.bc.variable_modifiers_letters:
val = self.cb.build_component(VARIABLE_MODIFIERS, vm)
if val is not None:
variable_modifiers.append(val)
if len(variable_modifiers) == 0:
variable_modifiers = None
function_modifiers = []
for fm_letter in match.bc.function_modifier_letters:
fm = self.cb.build_component(
FUNCTION_MODIFIERS, fm_letter
)
if fm is not None:
function_modifiers.append(fm)
if len(function_modifiers) == 0:
function_modifiers = None
passive_functions = []
for pf in match.bc.passive_function_letters:
val = self.cb.build_component(PASSIVE_FUNCTIONS, pf)
if val is not None:
passive_functions.append(val)
if len(passive_functions) == 0:
passive_functions = None
full_name = self._build_full_name(func_name, function_modifiers, match)
full_name = full_name.replace(" ", " ")
isa_id = ISA51Id(
is_in_allowed_table=match.is_allowed,
full_name=full_name,
variable=self.cb.build_component(
VARIABLES, match.bc.variable_letter
),
variable_modifiers=variable_modifiers,
passive_functions=passive_functions,
active_function=ComponentBuilder.build_component(
ACTIVE_FUNCTIONS, match.bc.active_function_letter
),
function_modifiers=function_modifiers
)
return isa_id
@staticmethod
def _get_function_name(match: ISA51BaseComponentsMatch,
table: MatchTable) -> str:
func_name = ""
if table == MatchTable.ACTIVE_FUNCTION:
func_name = active_full_name_headers[match.index]
if table == MatchTable.PASSIVE_FUNCTION:
func_name = passive_full_name_headers[match.index]
return func_name
@staticmethod
def _build_full_name(func_name: str, function_modifier: Optional[List[
ISA51Component]], match: ISA51BaseComponentsMatch) -> str:
if function_modifier is not None and len(function_modifier) > 0:
function_modifier_name = ""
for fm in function_modifier:
if fm.name in func_name:
continue
function_modifier_name += fm.name + " "
full_name = f"{match.var_name} {function_modifier_name} {func_name}"
else:
full_name = f"{match.var_name} {func_name}"
return full_name