-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
99 lines (82 loc) · 3.13 KB
/
utils.py
File metadata and controls
99 lines (82 loc) · 3.13 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Telenet utils."""
from __future__ import annotations
import logging
import re
from jsonpath import jsonpath
_LOGGER = logging.getLogger(__name__)
def str_to_float(input) -> float:
"""Transform float to string."""
if isinstance(input, str):
return float(input.replace(",", "."))
return input
def float_to_timestring(float_time, unit_type) -> str:
"""Transform float to timestring."""
float_time = str_to_float(float_time)
if unit_type.lower() == "seconds":
float_time = float_time * 60 * 60
elif unit_type.lower() == "minutes":
float_time = float_time * 60
# _LOGGER.debug(f"[float_to_timestring] Float Time {float_time}")
hours, seconds = divmod(float_time, 3600) # split to hours and seconds
minutes, seconds = divmod(seconds, 60) # split the seconds to minutes and seconds
result = ""
if hours:
result += f" {hours:02.0f}" + "u"
if minutes:
result += f" {minutes:02.0f}" + " min"
if seconds:
result += f" {seconds:02.0f}" + " sec"
if len(result) == 0:
result = "0 sec"
return result.strip()
def format_entity_name(string: str) -> str:
"""Format entity name."""
string = string.strip()
string = re.sub(r"\s+", "_", string)
string = re.sub(r"\W+", "", string).lower()
return string
def sizeof_fmt(num, suffix="b"):
"""Convert unit to human readable."""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
def get_json_dict_path(dictionary, path):
"""Fetch info based on jsonpath from dict."""
# _LOGGER.debug(f"[get_json_dict_path] Path: {path}, Dict: {dictionary}")
json_dict = jsonpath(dictionary, path)
if isinstance(json_dict, list):
json_dict = json_dict[0]
return json_dict
def get_localized(language, localizedcontent):
"""Fetch localized content."""
# _LOGGER.debug(f"[get_localized] {language} {localizedcontent}")
for lang in localizedcontent:
if language == lang.get("locale"):
return lang
return localizedcontent[0]
def clean_ipv6(data):
"""Clean ipv6 addresses from the list."""
# _LOGGER.debug("[clean_ipv6] " + str(data))
if isinstance(data, list):
for idx, item in enumerate(data):
if "ipType" in item and "ipAddress" in item:
if item["ipType"] == "IPv6":
_LOGGER.debug(f"[utils|clean_ipv6] IPv6 address removed: {item}")
del data[idx]
else:
data[idx] = clean_ipv6(data[idx])
else:
for property in data:
if isinstance(data.get(property), bool | str):
data[property] = data.get(property)
else:
if isinstance(data.get(property), list):
if len(data[property]) == 0:
data[property] = []
else:
data[property] = clean_ipv6(data.get(property))
else:
data[property] = clean_ipv6(data.get(property))
return data