Extracts all the keys and key defaults from the xml.
(self, graph_element)
| 1021 | return data |
| 1022 | |
| 1023 | def find_graphml_keys(self, graph_element): |
| 1024 | """Extracts all the keys and key defaults from the xml.""" |
| 1025 | graphml_keys = {} |
| 1026 | graphml_key_defaults = {} |
| 1027 | for k in graph_element.findall(f"{{{self.NS_GRAPHML}}}key"): |
| 1028 | attr_id = k.get("id") |
| 1029 | attr_type = k.get("attr.type") |
| 1030 | attr_name = k.get("attr.name") |
| 1031 | yfiles_type = k.get("yfiles.type") |
| 1032 | if yfiles_type is not None: |
| 1033 | attr_name = yfiles_type |
| 1034 | attr_type = "yfiles" |
| 1035 | if attr_type is None: |
| 1036 | attr_type = "string" |
| 1037 | warnings.warn(f"No key type for id {attr_id}. Using string") |
| 1038 | if attr_name is None: |
| 1039 | raise eg.EasyGraphError(f"Unknown key for id {attr_id}.") |
| 1040 | graphml_keys[attr_id] = { |
| 1041 | "name": attr_name, |
| 1042 | "type": self.python_type[attr_type], |
| 1043 | "for": k.get("for"), |
| 1044 | } |
| 1045 | # check for "default" sub-element of key element |
| 1046 | default = k.find(f"{{{self.NS_GRAPHML}}}default") |
| 1047 | if default is not None: |
| 1048 | # Handle default values identically to data element values |
| 1049 | python_type = graphml_keys[attr_id]["type"] |
| 1050 | if python_type == bool: |
| 1051 | graphml_key_defaults[attr_id] = self.convert_bool[ |
| 1052 | default.text.lower() |
| 1053 | ] |
| 1054 | else: |
| 1055 | graphml_key_defaults[attr_id] = python_type(default.text) |
| 1056 | return graphml_keys, graphml_key_defaults |