Skip to content

Commit e73ef4a

Browse files
committedSep 13, 2024·
Add Cluster XML parser
1 parent 514b2f6 commit e73ef4a

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
 

‎ClusterReader.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
import sys
4+
5+
def is_attribute_non_volatile(source, attribute_id):
6+
"""
7+
Checks if the attribute with the given hexadecimal ID has 'nonVolatile' persistence in the XML.
8+
Also prints the attribute name if found.
9+
10+
Args:
11+
source: Either a URL (string) or a file path (string) to the XML file.
12+
attribute_id: The hexadecimal ID of the attribute to check (e.g., 0x0000).
13+
14+
Returns:
15+
True if the attribute has 'nonVolatile' persistence, False otherwise.
16+
"""
17+
18+
if source.startswith("http://") or source.startswith("https://"):
19+
response = requests.get(source)
20+
response.raise_for_status()
21+
content = response.content
22+
23+
# Debug: Check the response status code
24+
print("Response status code:", response.status_code)
25+
26+
else:
27+
with open(source, 'r') as file:
28+
content = file.read()
29+
30+
soup = BeautifulSoup(content, 'lxml-xml')
31+
32+
# Debug: Print the parsed XML structure (for a quick visual check)
33+
# print("Parsed XML:", soup.prettify())
34+
35+
# Find the attribute with the given ID (convert hex ID from XML to integer for comparison)
36+
attribute = soup.find('attribute', {'id': lambda x: int(x, 16) == attribute_id})
37+
38+
# Debug: Check if the attribute was found
39+
print("Attribute found:", attribute is not None)
40+
41+
if attribute:
42+
quality_node = attribute.find('quality')
43+
44+
# Debug: Check if the quality node was found
45+
print("Quality node found:", quality_node is not None)
46+
47+
if quality_node and quality_node.get('persistence') == 'nonVolatile':
48+
print(f"Attribute name: {attribute['name']}")
49+
return True
50+
51+
return False
52+
53+
if __name__ == "__main__":
54+
if len(sys.argv) != 3:
55+
print(f"Usage: python {sys.argv[0]} <source> <attribute_id>")
56+
print(f"e.g. python {sys.argv[0]} data_model/1.3/clusters/OnOff.xml 0x0000")
57+
sys.exit(1)
58+
59+
source = sys.argv[1]
60+
attribute_id_to_check = int(sys.argv[2], 16)
61+
62+
result = is_attribute_non_volatile(source, attribute_id_to_check)
63+
print(f"Attribute with ID '0x{attribute_id_to_check:04X}' is non-volatile: {result}")

‎ParseXmlFolder.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import sys
3+
import xml.etree.ElementTree as ET
4+
5+
id2XmlMap = []
6+
7+
8+
def parse_xml_files_in_folder(folder_path):
9+
print(f"DEBUG: Starting to parse XML files in folder: {folder_path}")
10+
11+
for filename in os.listdir(folder_path):
12+
if filename.endswith('.xml'):
13+
full_path = os.path.join(folder_path, filename)
14+
print(f"DEBUG: Processing file: {filename}")
15+
16+
try:
17+
tree = ET.parse(full_path)
18+
root = tree.getroot()
19+
print(f"DEBUG: Successfully parsed {filename}")
20+
21+
# Now you can work with the 'root' element to extract data
22+
# For example, to print all child elements:
23+
for child in root:
24+
print(child.tag, child.attrib)
25+
26+
if child.tag != 'clusterIds':
27+
continue
28+
29+
clusterIdSet = child.findall('clusterId')
30+
print(clusterIdSet)
31+
32+
for clusterId in clusterIdSet:
33+
clusterIdMap = {'id':clusterId.get('id'), 'name':clusterId.get('name'), 'file':full_path}
34+
35+
id2XmlMap.append(clusterIdMap)
36+
37+
print(f"DEBUG: Successfully parsed {filename}")
38+
39+
except ET.ParseError as e:
40+
print(f"ERROR: Error parsing {filename}: {e}")
41+
42+
print(clusterIdMap)
43+
44+
45+
if __name__ == "__main__":
46+
if len(sys.argv) < 2:
47+
print(f"Usage: python {sys.argv[0]} folder_path")
48+
print(f"e.g. python {sys.argv[0]} data_model/1.3/clusters")
49+
sys.exit(1)
50+
51+
folder_path = sys.argv[1]
52+
parse_xml_files_in_folder(folder_path)

0 commit comments

Comments
 (0)
Please sign in to comment.