-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
153 lines (118 loc) · 4.13 KB
/
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from datetime import datetime
import json
import time
def bits2int(bitlist):
out = 0
for bit in bitlist:
out = (out << 1) | bit
return out
def int2bits(n):
weekdaysCount = 7
if not isinstance(n, int):
return [0] * weekdaysCount
bits = [1 if digit == '1' else 0 for digit in bin(n)[2:]]
# make sure to return always list with 7 items:
bits = [0] * (weekdaysCount - len(bits)) + bits
return bits
def weekdays2bits(weekdaysDict):
bitlist = []
weekdays = getWeekdays()
for day in weekdays:
if day in weekdaysDict:
bitlist.append(1)
else:
bitlist.append(0)
return bitlist
def getWeekdays():
return ['monday', 'tuesday', 'wednesday',
'thursday', 'friday', 'saturday', 'sunday']
def sqlrow2dict(row):
return dict(zip(row.keys(), row))
def config2dict(filename):
with open(filename, 'r') as configFile:
data = configFile.read()
obj = json.loads(data)
return obj
def utc2local_time(utc_hour, utc_minute):
''' (UTC hour, UTC minute) -> (local hour, local minute)
we need to create a datetime object since timezone offset
depends on actual date because of daylight saving times
'''
now = datetime.now()
utc_datetime = now.replace(hour=utc_hour, minute=utc_minute)
local_datetime = utc2local_datetime(utc_datetime)
return (local_datetime.hour, local_datetime.minute)
def utc2local_datetime(utc):
''' UTC datetime -> local utc_datetime
'''
epoch = time.mktime(utc.timetuple())
offset = datetime.fromtimestamp(epoch) - datetime.utcfromtimestamp(epoch)
return utc + offset
def getSunriseTime(longitude, latitude):
from sun import Sun
coords = {'longitude': longitude, 'latitude': latitude}
sun = Sun()
sunset = sun.getSunriseTime(coords)
return utc2local_time(sunset['hr'], sunset['min'])
def getSunsetTime(longitude, latitude):
from sun import Sun
coords = {'longitude': longitude, 'latitude': latitude}
sun = Sun()
sunset = sun.getSunsetTime(coords)
return utc2local_time(sunset['hr'], sunset['min'])
def timetuple2str(time_tuple):
hourStr = str(time_tuple[0]).zfill(2)
minuteStr = str(time_tuple[1]).zfill(2)
return '{0}:{1}'.format(hourStr, minuteStr)
def addOffsetToTimeTuple(timeTuple, offsetInMin):
offHours = int(offsetInMin / 60)
offMinutes = offsetInMin - (offHours * 60)
return (timeTuple[0] + offHours, timeTuple[1] + offMinutes)
def event2str(event, html=False):
res = ''
if event['mode'] == 0:
res = timetuple2str((event['hour'], event['minute']))
elif event['mode'] == 1:
if html is True:
res = '<span title="sunrise">🌅</span>'
else:
res = '🌅'
if event['sunriseOffset'] != 0:
if html is True:
tmpStr = '<span class="small"> {0} {1} min.</span>'
else:
tmpStr = ' {0} {1} min.'
if event['sunriseOffset'] > 0:
res += tmpStr.format('+', str(event['sunriseOffset']))
else:
res += tmpStr.format('-', str(event['sunriseOffset']*-1))
elif event['mode'] == 2:
if html is True:
res = '<span title="sunset">🌇</span>'
else:
res = '🌇'
if event['sunsetOffset'] != 0:
if html is True:
tmpStr = '<span class="small"> {0} {1} min.</span>'
else:
tmpStr = ' {0} {1} min.'
if event['sunsetOffset'] > 0:
res += tmpStr.format('+', str(event['sunsetOffset']))
else:
res += tmpStr.format('-', str(event['sunsetOffset']*-1))
if event['randomOffset'] != 0:
rStr = '(rand. +/- {0} min.)'
if html is True:
rStr = '<span title="random offset {0} min." class="small">' + rStr + '</span>'
res += ' ' + rStr.format(event['randomOffset'])
if event['switchOn'] == 0:
switchOnStr = 'off'
else:
switchOnStr = 'on'
res += ' → ' + switchOnStr
return res
TIMED_EVENT_MODES = {
'fixed': 0,
'sunrise': 1,
'sunset': 2,
}