|
| 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}") |
0 commit comments