-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
218 lines (157 loc) · 5.18 KB
/
utils.py
File metadata and controls
218 lines (157 loc) · 5.18 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import uuid
from time import time, sleep
import unittest
from datetime import datetime, timedelta
import boto3
from pytz import timezone
from exceptions import BuzzerException, NotANumberException
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table('apartment-buzzer-auto-buzz')
times_table = dynamodb.Table('apartment-buzzer-auto-buzz-times')
def get_now():
return datetime.now(timezone('US/Eastern'))
def parse_minutes(text):
try:
minutes = int(text)
except ValueError:
raise NotANumberException()
if not 5 <= minutes <= 60:
raise BuzzerException('Please type a number between 5 and 60')
return minutes
def parse_start_end_times(text, now):
start, end = text.split('-')
try:
start = int(start)
end = int(end)
except ValueError:
start = 0
end = 0
if not 1 <= start <= 12 or not 1 <= end <= 12:
raise BuzzerException('Hours need to be between 1 and 12')
else:
start_dt = now.replace(hour=start, minute=0, second=0, microsecond=0)
end_dt = now.replace(hour=end, minute=0, second=0, microsecond=0)
while now >= start_dt:
start_dt += timedelta(hours=12)
while start_dt >= end_dt:
end_dt += timedelta(hours=12)
return start_dt, end_dt
def add_auto_buzz_time(start, end):
start = start.isoformat()
end = end.isoformat()
times_table.put_item(
Item={
'uuid': str(uuid.uuid4()),
'start': start,
'end': end,
}
)
def get_auto_buzz_times():
response = times_table.scan()
return response['Items']
def set_auto_buzz_config(minutes):
response = table.update_item(
Key={
'key': 'auto-buzz'
},
UpdateExpression='SET #until = :until',
ExpressionAttributeNames={
'#until': 'until'
},
ExpressionAttributeValues={
':until': int(time() + 60 * minutes)
}
)
def get_auto_buzz_config():
response = table.get_item(
Key={
'key': 'auto-buzz'
}
)
try:
rv = response['Item']
except KeyError:
rv = None
return rv
def _should_auto_buzz(config):
auto_buzz = False
try:
value = config['value']
until = config['until']
if value == 'true' and until > time():
auto_buzz = True
except KeyError:
pass
return auto_buzz
def _should_auto_buzz_times(times):
now = get_now()
for time_entry in times:
if datetime.fromisoformat(time_entry['start']) < now < datetime.fromisoformat(time_entry['end']):
return True
return False
def should_auto_buzz():
config = get_auto_buzz_config()
times = get_auto_buzz_times()
return _should_auto_buzz(config) or _should_auto_buzz_times(times)
class StartEndTimesTestCase(unittest.TestCase):
def test_parse_start_end_times(self):
now = datetime(2019, 1, 1, 0)
text = '1-2'
start, end = parse_start_end_times(text, now)
self.assertEqual(start, datetime(2019, 1, 1, 1))
self.assertEqual(end, datetime(2019, 1, 1, 2))
def test_parse_start_end_times_pm_to_am(self):
now = datetime(2019, 1, 1, 20)
text = '6-8'
start, end = parse_start_end_times(text, now)
self.assertEqual(start, datetime(2019, 1, 2, 6))
self.assertEqual(end, datetime(2019, 1, 2, 8))
def test_parse_start_end_times_am_to_pm(self):
now = datetime(2019, 1, 1, 8)
text = '6-7'
start, end = parse_start_end_times(text, now)
self.assertEqual(start, datetime(2019, 1, 1, 18))
self.assertEqual(end, datetime(2019, 1, 1, 19))
def test_parse_start_end_times_start_now(self):
now = datetime(2019, 1, 1, 1)
text = '1-2'
start, end = parse_start_end_times(text, now)
self.assertEqual(start, datetime(2019, 1, 1, 13))
self.assertEqual(end, datetime(2019, 1, 1, 14))
def test_parse_start_end_times_invalid(self):
now = datetime(2019, 1, 1, 0)
text = '1-50'
with self.assertRaises(BuzzerException) as context:
parse_start_end_times(text, now)
class AutoBuzzTestCase(unittest.TestCase):
def test_auto_buzz_true(self):
config = {
'value': 'true',
'until': time() + 60
}
auto_buzz = _should_auto_buzz(config)
self.assertTrue(auto_buzz)
def test_auto_buzz_false(self):
config = {
'value': 'true',
'until': time() - 60
}
auto_buzz = _should_auto_buzz(config)
self.assertFalse(auto_buzz)
def test_in_range(self):
times = [
{'start': time() - 60, 'end': time() + 60}
]
auto_buzz = _should_auto_buzz_times(times)
self.assertTrue(auto_buzz)
def test_out_of_range(self):
times = [
{'start': time() - 60, 'end': time() - 1}
]
auto_buzz = _should_auto_buzz_times(times)
self.assertFalse(auto_buzz)
if __name__ == '__main__':
add_auto_buzz_time(get_now(), get_now() + timedelta(seconds=1))
print(should_auto_buzz())
sleep(2)
print(should_auto_buzz())