forked from Heckie75/voltcraft-sem-6000
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
94 lines (70 loc) · 2.22 KB
/
util.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
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
import datetime
import enum
class Weekday(enum.Enum):
SUNDAY = 0
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
def _format_year_and_month(year, month):
return "{:04}-{:02}".format(year, month)
def _list_values_to_enum(enum_class, list_of_values):
list_of_enums = []
for v in list_of_values:
list_of_enums.append(enum_class(v))
return list_of_enums
def _format_list_of_objects(format_object_lambda, list_of_objects):
is_first = True
formatted_string = "["
for o in list_of_objects:
if not is_first:
formatted_string += ", "
formatted_string += format_object_lambda(o)
is_first = False
formatted_string += "]"
return formatted_string
def _parse_boolean(boolean_string):
boolean_value = False
if str(boolean_string).lower() == "true":
boolean_value = True
if str(boolean_string).lower() == "on":
boolean_value = True
if str(boolean_string).lower() == "1":
boolean_value = True
return boolean_value
def _parse_list(list_input):
if type(list_input) == list:
list_value = list_input
if type(list_input) == str:
list_value = list_input.split(",")
for i in range(len(list_value)):
if type(list_value[i]) == str:
list_value[i] = list_value[i].strip()
return list_value
def _parse_time_from_minutes(minutes):
hour = minutes // 60
minute = minutes - hour*60
return datetime.time(hour, minute)
def _parse_weekday(weekday):
if isinstance(weekday, Weekday):
return weekday
if type(weekday) == int:
return Weekday(weekday)
weekday = weekday.lower()
weekday_num = 0
for weekday_str in ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]:
if weekday_str in weekday or weekday == str(weekday_num):
return Weekday(weekday_num)
weekday_num += 1
return None
def _parse_weekdays_list(weekdays_list):
weekdays=[]
weekday_values_list = _parse_list(weekdays_list)
for weekday_value in weekday_values_list:
weekday = _parse_weekday(weekday_value)
if weekday is None:
continue
weekdays.append(weekday)
return weekdays