aboutsummaryrefslogtreecommitdiff
path: root/include/py/hikvision/util.py
blob: 581c6ea31acdb298bd617c59e6875390fd60012f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import requests
import hashlib
import xml.etree.ElementTree as ET


def xml_to_dict(xml_data: str) -> dict:
    # Parse the XML data
    root = ET.fromstring(xml_data)

    # Function to remove namespace from the tag name
    def remove_namespace(tag):
        return tag.split('}')[-1]  # Splits on '}' and returns the last part, the actual tag name without namespace

    # Function to recursively convert XML elements to a dictionary
    def elem_to_dict(elem):
        tag = remove_namespace(elem.tag)
        elem_dict = {tag: {}}

        # If the element has attributes, add them to the dictionary
        elem_dict[tag].update({'@' + remove_namespace(k): v for k, v in elem.attrib.items()})

        # Handle the element's text content, if present and not just whitespace
        text = elem.text.strip() if elem.text and elem.text.strip() else None
        if text:
            elem_dict[tag]['#text'] = text

        # Process child elements
        for child in elem:
            child_dict = elem_to_dict(child)
            child_tag = remove_namespace(child.tag)
            if child_tag not in elem_dict[tag]:
                elem_dict[tag][child_tag] = []
            elem_dict[tag][child_tag].append(child_dict[child_tag])

        # Simplify structure if there's only text or no children and no attributes
        if len(elem_dict[tag]) == 1 and '#text' in elem_dict[tag]:
            return {tag: elem_dict[tag]['#text']}
        elif not elem_dict[tag]:
            return {tag: ''}

        return elem_dict

    # Convert the root element to dictionary
    return elem_to_dict(root)


def sha256_hex(input_string: str) -> str:
    return hashlib.sha256(input_string.encode()).hexdigest()