Skip to content

Commit

Permalink
maxsplit
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Oct 5, 2024
1 parent 1ed508e commit 223435d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion xknxproject/loader/application_program_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def load(
tree_iterator = ElementTree.iterparse(application_xml, events=("start",))
# get namespace from root element
_, elem = next(tree_iterator)
namespace = elem.tag.split("KNX")[0]
namespace = elem.tag.split("KNX", maxsplit=1)[0]
# define namespaced tag strings for faster comparison - ~15% faster
# than elem.tag.endswith("tagname") or elem.tag == f"{namespace}tagname"
ns_com_object = f"{namespace}ComObject"
Expand Down
8 changes: 5 additions & 3 deletions xknxproject/loader/project_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ def __get_links_from_ets4(com_object: ElementTree.Element) -> list[str]:
ga_list = connectors.findall("{*}Send") + connectors.findall("{*}Receive")

# Remove the project ID from GA
return [ga.get("GroupAddressRefId", "").split("_")[1] for ga in ga_list]
return [
ga.get("GroupAddressRefId", "").split("_", maxsplit=1)[1] for ga in ga_list
]

@staticmethod
def __get_links_from_ets5(com_object: ElementTree.Element) -> list[str]:
Expand Down Expand Up @@ -451,7 +453,7 @@ def parse_space(

def parse_functions(self, node: ElementTree.Element) -> XMLFunction:
"""Parse a functions from the document."""
identifier = node.get("Id", "").split("_")[1]
identifier = node.get("Id", "").split("_", 1)[1]
project_uid = node.get("Puid")
function_type = node.get("Type", "")

Expand All @@ -468,7 +470,7 @@ def parse_functions(self, node: ElementTree.Element) -> XMLFunction:
for sub_node in node:
if sub_node.tag.endswith("GroupAddressRef"):
project_uid = sub_node.get("Puid")
ref_id = sub_node.get("RefId", "").split("_")[1]
ref_id = sub_node.get("RefId", "").split("_", 1)[1]

group_address_ref: XMLGroupAddressRef = XMLGroupAddressRef(
ref_id=ref_id,
Expand Down
12 changes: 7 additions & 5 deletions xknxproject/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
):
"""Initialize a group address."""
self.name = name
self.identifier = identifier.split("_")[1]
self.identifier = identifier.split("_", 1)[1]
self.raw_address = int(address)
self.project_uid = project_uid
self.description = description
Expand Down Expand Up @@ -284,7 +284,7 @@ def resolve_channel_module_placeholders(
):
return

module_instance_ref = self.ref_id.split("_CH")[0]
module_instance_ref = self.ref_id.split("_CH", maxsplit=1)[0]
try:
module_instance = next(
mi
Expand Down Expand Up @@ -316,10 +316,10 @@ class ModuleInstance:

def __post_init__(self) -> None:
"""Set is_submodule based on the identifier."""
self.module_def_id = self.ref_id.split("_")[0]
self.module_def_id = self.ref_id.split("_", maxsplit=1)[0]
_submodule_match = re.search(r"(_SM-[^_]+)", self.identifier)
if _submodule_match is not None:
self.base_module = f"{self.identifier.split('_SM-')[0]}"
self.base_module = f"{self.identifier.split('_SM-', maxsplit=1)[0]}"
self.definition_id = f"{self.module_def_id}{_submodule_match.group()}"
else:
self.base_module = None
Expand Down Expand Up @@ -580,7 +580,9 @@ def _base_number_from_allocator(
base_number_argument,
)
return 0
module_instance_index = int(self.ref_id.split("_MI-")[1].split("_")[0])
module_instance_index = int(
self.ref_id.split("_MI-", maxsplit=1)[1].split("_", maxsplit=1)[0]
)
return allocator_object_base.start + (
allocator_size * (module_instance_index - 1)
)
Expand Down
4 changes: 2 additions & 2 deletions xknxproject/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def text_parameter_insert_module_instance(
if "_MD-" in text_parameter_ref_id and (
_module_ref := get_module_instance_part(instance_ref, next_id=instance_next_id)
):
_application_ref = text_parameter_ref_id.split("_MD-")[0]
_parameter_ref = text_parameter_ref_id.split("_P-")[1]
_application_ref = text_parameter_ref_id.split("_MD-", maxsplit=1)[0]
_parameter_ref = text_parameter_ref_id.rsplit("_P-", maxsplit=1)[-1]
return f"{_application_ref}_{_module_ref}_P-{_parameter_ref}"

return text_parameter_ref_id
2 changes: 1 addition & 1 deletion xknxproject/zip/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _get_xml_namespace(project_zip: ZipFile) -> str:
def _get_schema_version(namespace: str) -> int:
"""Get the schema version of the project."""
try:
schema_version = int(namespace.split("/")[-1])
schema_version = int(namespace.rsplit("/", 1)[-1])
except ValueError:
_LOGGER.error("Could not parse schema version from %s", namespace)
raise UnexpectedFileContent("Could not parse schema version.") from None
Expand Down

0 comments on commit 223435d

Please sign in to comment.