5 from typing
import Callable, Dict, List, Sequence, Set, Tuple
7 from AthenaConfiguration.iconfTool.models.element
import (
14 logger = logging.getLogger(__name__)
19 def __init__(self, flags_dict: Dict, checked_elements=
set()) ->
None:
20 self.flags_dict: Dict = flags_dict
21 self.structure_dict: Dict = {}
22 self.checked_elements: Set = (
28 """Split dictionary keys into nested structure
29 e.g. Test1.Test2: Value -> Test1: {Test2: Value}
31 structure_dict: Dict = {}
33 for flag, value
in self.flags_dict.
items():
34 groups = flag.split(
".")
35 structure_point = structure_dict
36 for i
in range(len(groups) - 1):
37 if groups[i]
not in structure_point:
38 structure_point[groups[i]] = {}
39 elif isinstance(structure_point[groups[i]], str):
40 structure_point[groups[i]] = {
41 structure_point[groups[i]]: {}
43 structure_point = structure_point[groups[i]]
47 last
in structure_point
48 and isinstance(structure_point[last], dict)
49 and isinstance(value, dict)
51 structure_point[last].
update(value)
53 structure_point[last] = value
57 self, grouping_element: GroupingElement, structure: Dict
59 child_x_pos = grouping_element.x_pos + INDENT
60 for child
in structure:
63 structure[child] = ast.literal_eval(structure[child])
64 except (ValueError, SyntaxError):
67 if isinstance(structure[child], dict):
68 group = GroupingElement(child, child_x_pos, grouping_element)
69 if group.hash
in self.checked_elements:
70 group.set_as_checked()
72 grouping_element.add_child(group)
73 elif isinstance(structure[child], list):
74 group = GroupingElement(child, child_x_pos, grouping_element)
75 if group.hash
in self.checked_elements:
76 group.set_as_checked()
77 for el
in structure[child]:
78 no_value_child = SingleElement(el, child_x_pos + INDENT)
79 if no_value_child.hash
in self.checked_elements:
80 no_value_child.set_as_checked()
81 group.add_child(no_value_child)
82 grouping_element.add_child(group)
85 child, child_x_pos, structure[child], grouping_element
87 if flag.hash
in self.checked_elements:
89 grouping_element.add_child(flag)
92 hash_value = element.hash
93 self.checked_elements.
add(hash_value)
96 hash_value = element.hash
97 if hash_value
in self.checked_elements:
98 self.checked_elements.
remove(hash_value)
102 def __init__(self, flags_dict: Dict, checked_elements: Set) ->
None:
103 super().
__init__(flags_dict, checked_elements)
104 self.roots_dict: Dict = {}
106 self.
filter: ComponentsStructureFilter
109 element.replaced =
True
110 for i, child
in enumerate(element.children):
111 if isinstance(child, SingleElement):
112 ref_name = child.get_reference_name()
113 if ref_name
in self.roots_dict
and not element.has_parent(
114 self.roots_dict[ref_name].get_name()
116 element.children[i] = self.roots_dict[ref_name]
118 element.children[i].parent = element
119 element.update_xpos(element.x_pos)
121 def generate(self, x_pos: int = 1, structure_dict=
None) ->
None:
126 root_group = GroupingElement(root_name, x_pos)
127 if root_group.hash
in self.checked_elements:
128 root_group.set_as_checked()
133 self.roots_dict[root_name] = root_group
143 self, structure_list: List[Element], x_pos: int
146 algs = GroupingElement(
"Algorithms and Sequences", x_pos)
147 tools = GroupingElement(
"Public tools", x_pos)
148 settings = GroupingElement(
"Application Settings and Services", x_pos)
150 new_structure_list: List[Element] = [algs, tools, settings]
154 def get_ath_sequences(el) -> None:
155 if isinstance(el, GroupingElement):
156 members_obj = el.get_child_by_name(
"Members")
158 assert isinstance(members_obj, GroupingElement)
159 for member
in members_obj.children:
160 ref_name = member.get_reference_name()
161 seq_names.append(ref_name)
162 get_ath_sequences(self.roots_dict.
get(ref_name))
164 seq_names.append(
"AthMasterSeq")
165 get_ath_sequences(self.roots_dict[
"AthMasterSeq"])
167 for el
in structure_list:
168 if el.name ==
"AthMasterSeq":
174 elif el.name
not in seq_names:
175 settings.add_child(el)
177 return new_structure_list
184 self.
filter.generate_by_text(filter_text)
186 assert isinstance(element, GroupingElement)
187 element.children =
list(
189 lambda child: child.get_reference_name()
190 in self.
filter.comps_to_save
191 or child
not in self.roots_dict,
199 element.sort_children()
203 def __init__(self, structure_dict: Dict[str, str]) ->
None:
204 self.structure_dict: Dict[str, str] = structure_dict
212 found_elements: List[str] = []
214 if not found_elements:
216 found_elements.extend(
218 "Algorithms and Sequences",
220 "Application Settings and Services",
224 logger.debug(f
"SEARCH - found elements: {found_elements}")
228 filter_text: str, structure_dict: Dict
229 ) -> List[Tuple[str, str]]:
231 filter(
lambda el: filter_text
in str(el), structure_dict.items())
235 self, filter_text: str, found_elements: List[str]
238 if el[0]
not in found_elements:
239 found_elements.append(el[0])
243 self, elements: Sequence[Element], filter_lambda: Callable
245 filtered =
list(
filter(filter_lambda, elements))
250 if len(filtered) > 0:
253 first.parent.children =
list(filtered)