-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
43 lines (31 loc) · 941 Bytes
/
utils.py
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
import re
import os
import logging
from lxml import etree
from config import LOG_FORMAT
mac_regex = re.compile("^(?:[0-9A-Fa-f]{2}[:-]?){5}(?:[0-9A-Fa-f]{2})$")
def configure_logging():
logging.basicConfig(
level=logging.INFO,
format=LOG_FORMAT,
datefmt="%d.%m.%Y, %H:%M:%S",
filename="log.txt",
filemode="a",
)
def ensure_ztp_mac(mac):
mac = mac.replace(":", "")
mac = ":".join(re.findall("..", mac))
# The code contains the management interface's MAC which ends on a zero
# The ZTP MAC ends on a one. Set that here:
mac = mac[:-1] + "3"
mac = mac.lower()
if not mac_regex.match(mac):
raise ValueError("Invalid MAC address")
return mac
def xml_to_keyvalue(xml):
result = ""
root = etree.fromstring(xml)
result = ", ".join(
[f"{node.tag}={node.text}" for node in root.iter("*") if len(node) == 0]
)
return result