Skip to content

Commit

Permalink
Sort by GA and IA (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio authored Oct 21, 2023
1 parent 7da615b commit 3c3ae60
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 74 deletions.
24 changes: 12 additions & 12 deletions test/resources/stubs/ets6_free.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
},
"devices": {},
"group_addresses": {
"3": {
"name": "whatever",
"identifier": "GA-3",
"raw_address": 3,
"address": "3",
"project_uid": 16,
"1": {
"name": "foo",
"identifier": "GA-1",
"raw_address": 1,
"address": "1",
"project_uid": 12,
"dpt": null,
"data_secure": false,
"communication_object_ids": [],
Expand All @@ -70,12 +70,12 @@
"description": "",
"comment": ""
},
"1": {
"name": "foo",
"identifier": "GA-1",
"raw_address": 1,
"address": "1",
"project_uid": 12,
"3": {
"name": "whatever",
"identifier": "GA-3",
"raw_address": 3,
"address": "3",
"project_uid": 16,
"dpt": null,
"data_secure": false,
"communication_object_ids": [],
Expand Down
28 changes: 14 additions & 14 deletions test/resources/stubs/module-definition-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,36 +287,36 @@
"description": "",
"comment": ""
},
"0/1/2": {
"name": "Temperatur Soll",
"identifier": "GA-4",
"raw_address": 258,
"address": "0/1/2",
"project_uid": 16,
"0/1/1": {
"name": "Temperatur Ist",
"identifier": "GA-7",
"raw_address": 257,
"address": "0/1/1",
"project_uid": 20,
"dpt": {
"main": 9,
"sub": 1
},
"data_secure": false,
"communication_object_ids": [
"1.1.1/MD-2_M-2_MI-1_O-2-2_R-3"
"1.1.1/MD-2_M-2_MI-1_O-2-1_R-1"
],
"description": "",
"comment": ""
},
"0/1/1": {
"name": "Temperatur Ist",
"identifier": "GA-7",
"raw_address": 257,
"address": "0/1/1",
"project_uid": 20,
"0/1/2": {
"name": "Temperatur Soll",
"identifier": "GA-4",
"raw_address": 258,
"address": "0/1/2",
"project_uid": 16,
"dpt": {
"main": 9,
"sub": 1
},
"data_secure": false,
"communication_object_ids": [
"1.1.1/MD-2_M-2_MI-1_O-2-1_R-1"
"1.1.1/MD-2_M-2_MI-1_O-2-2_R-3"
],
"description": "",
"comment": ""
Expand Down
90 changes: 45 additions & 45 deletions test/resources/stubs/xknx_test_project.json
Original file line number Diff line number Diff line change
Expand Up @@ -718,42 +718,6 @@
"description": "no dpt",
"comment": ""
},
"2/1/22": {
"name": "No DPT in GA 2",
"identifier": "GA-9",
"raw_address": 4374,
"address": "2/1/22",
"project_uid": 44,
"dpt": {
"main": 9,
"sub": null
},
"data_secure": false,
"communication_object_ids": [
"1.1.6/O-0_R-665",
"1.1.6/O-9_R-9073"
],
"description": "multiple DPT 9 and not defined in COs",
"comment": ""
},
"2/1/23": {
"name": "No DPT in GA 3",
"identifier": "GA-10",
"raw_address": 4375,
"address": "2/1/23",
"project_uid": 45,
"dpt": {
"main": 9,
"sub": null
},
"data_secure": false,
"communication_object_ids": [
"1.1.6/O-9_R-9073",
"1.1.6/O-18_R-7994"
],
"description": "DPT 9 generic and multiple in COs",
"comment": ""
},
"2/1/2": {
"name": "Not linked to a comm object 2",
"identifier": "GA-11",
Expand All @@ -769,6 +733,23 @@
"description": "temperature",
"comment": ""
},
"2/1/10": {
"name": "generic 1-bit",
"identifier": "GA-13",
"raw_address": 4362,
"address": "2/1/10",
"project_uid": 50,
"dpt": {
"main": 1,
"sub": null
},
"data_secure": false,
"communication_object_ids": [
"1.1.6/O-20_R-8108"
],
"description": "",
"comment": ""
},
"2/1/21": {
"name": "No DPT in GA",
"identifier": "GA-12",
Expand All @@ -786,21 +767,40 @@
"description": "multiple DPT 9 in 1 CO",
"comment": ""
},
"2/1/10": {
"name": "generic 1-bit",
"identifier": "GA-13",
"raw_address": 4362,
"address": "2/1/10",
"project_uid": 50,
"2/1/22": {
"name": "No DPT in GA 2",
"identifier": "GA-9",
"raw_address": 4374,
"address": "2/1/22",
"project_uid": 44,
"dpt": {
"main": 1,
"main": 9,
"sub": null
},
"data_secure": false,
"communication_object_ids": [
"1.1.6/O-20_R-8108"
"1.1.6/O-0_R-665",
"1.1.6/O-9_R-9073"
],
"description": "",
"description": "multiple DPT 9 and not defined in COs",
"comment": ""
},
"2/1/23": {
"name": "No DPT in GA 3",
"identifier": "GA-10",
"raw_address": 4375,
"address": "2/1/23",
"project_uid": 45,
"dpt": {
"main": 9,
"sub": null
},
"data_secure": false,
"communication_object_ids": [
"1.1.6/O-9_R-9073",
"1.1.6/O-18_R-7994"
],
"description": "DPT 9 generic and multiple in COs",
"comment": ""
},
"7/0/0": {
Expand Down
2 changes: 1 addition & 1 deletion xknxproject/loader/project_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _create_device(
product_ref = device_element.get("ProductRefId", "")
device: DeviceInstance = DeviceInstance(
identifier=device_element.get("Id", ""),
address=address,
address=int(address),
project_uid=int(project_uid) if project_uid else None,
name=device_element.get("Name", ""),
description=device_element.get("Description", ""),
Expand Down
6 changes: 4 additions & 2 deletions xknxproject/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __init__(
self,
*,
identifier: str,
address: str,
address: int,
project_uid: int | None,
name: str,
description: str,
Expand All @@ -142,14 +142,16 @@ def __init__(
self.product_ref = product_ref
self.hardware_program_ref = hardware_program_ref
self.line = line
self.area_address = line.area.address # used for sorting
self.line_address = line.address # used for sorting
self.manufacturer = manufacturer
self.additional_addresses = additional_addresses or []
self.com_object_instance_refs = com_object_instance_refs or []
self.com_objects = com_objects or []
self.application_program_ref: str | None = None

self.individual_address = (
f"{self.line.area.address}.{self.line.address}.{self.address}"
f"{self.area_address}.{self.line_address}.{self.address}"
)
self.product_name: str = ""
self.hardware_name: str = ""
Expand Down
31 changes: 31 additions & 0 deletions xknxproject/xml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import html
import logging
from operator import attrgetter

from striprtf.striprtf import rtf_to_text

Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self, knx_proj_contents: KNXProjContents) -> None:
def parse(self, language: str | None = None) -> KNXProject:
"""Parse ETS files."""
self.load(language=language)
self.sort()
_ga_id_to_address = {ga.identifier: ga.address for ga in self.group_addresses}

communication_objects: dict[str, CommunicationObject] = {}
Expand Down Expand Up @@ -344,3 +346,32 @@ def load(self, language: str | None) -> None:
devices=devices,
language_code=self.language_code,
)

def sort(self) -> None:
"""Sort loaded structures as XML content is sorted by creation time."""

def recursive_sort_spaces(spaces: list[XMLSpace]) -> None:
for _space in spaces:
_space.devices.sort(
key=lambda ia: tuple(ia.split(".")) # area > line > device
)
recursive_sort_spaces(_space.spaces)

recursive_sort_spaces(self.spaces)

self.group_addresses.sort(key=attrgetter("raw_address"))

def recursive_sort_group_ranges(group_ranges: list[XMLGroupRange]) -> None:
for _grs in group_ranges:
recursive_sort_group_ranges(_grs.group_ranges)
group_ranges.sort(key=attrgetter("range_start"))

recursive_sort_group_ranges(self.group_ranges)

for area in self.areas:
for line in area.lines:
line.devices.sort(key=attrgetter("address"))
area.lines.sort(key=attrgetter("address"))
self.areas.sort(key=attrgetter("address"))

self.devices.sort(key=attrgetter("area_address", "line_address", "address"))

0 comments on commit 3c3ae60

Please sign in to comment.