make human time intervals available in timegens

Note that the WebUI is still not aware of this functionality
refs #20
This commit is contained in:
boyska 2016-09-09 10:24:28 +02:00
parent 2895b2fe41
commit 6977608c7e
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
3 changed files with 34 additions and 15 deletions

View file

@ -1,4 +1,4 @@
from datetime import timedelta
from datetime import timedelta, datetime
from pprint import pprint
import pytest
@ -17,10 +17,30 @@ def eq_(a, b, reason=None):
@pytest.fixture
def now():
from datetime import datetime
return datetime.now()
@pytest.fixture(params=['seconds', 'human', 'humanlong', 'coloned'])
def onehour(now, request):
'''a FrequencyAlarm: every hour for one day'''
intervals = dict(seconds=3600, human='1h', humanlong='30m 1800s',
coloned='01:00:00')
return FrequencyAlarm({
'start': now - timedelta(days=1),
'interval': intervals[request.param],
'end': now + days(1)})
@pytest.fixture(params=['seconds', 'human', 'coloned'])
def tenseconds(now, request):
'''a FrequencyAlarm: every 10 seconds for one day'''
intervals = dict(seconds=10, human='10s', coloned='00:10')
return FrequencyAlarm({
'start': now - timedelta(days=1),
'interval': intervals[request.param],
'end': now + days(1)})
def days(n):
return timedelta(days=n)
@ -60,12 +80,8 @@ def test_single_all(now):
eq_(list(s.all_rings(now + days(2))), [])
def test_freq_short(now):
f = FrequencyAlarm({
'start': now - days(1),
'interval': 10,
'end': now + days(1)
})
def test_freq_short(now, tenseconds):
f = tenseconds
assert now in f.all_rings(now - days(3))
assert f.next_ring(now) is not None
assert f.next_ring(now) != now
@ -76,12 +92,8 @@ def test_freq_short(now):
@pytest.mark.timeout(1)
def test_freq_ring(now):
f = FrequencyAlarm({
'start': now - days(1),
'interval': 3600,
'end': now + days(1)
})
def test_freq_ring(now, onehour):
f = onehour
assert now in f.all_rings(now - days(3))
assert f.next_ring(now) is not None
assert f.next_ring(now) != now

View file

@ -3,6 +3,8 @@ import logging
log = logging.getLogger('time-every')
from datetime import datetime, timedelta
from pytimeparse.timeparse import timeparse
def getdate(val):
if type(val) is int:
@ -64,7 +66,11 @@ class FrequencyAlarm(Alarm):
def __init__(self, obj):
self.start = getdate(obj['start'])
self.interval = obj['interval']
try:
self.interval = int(obj['interval'])
except ValueError:
self.interval = timeparse(obj['interval'])
assert type(self.interval) is int
self.end = getdate(obj['end']) if 'end' in obj else None
def next_ring(self, current_time=None):

View file

@ -44,6 +44,7 @@ setup(name='larigira',
'python-mpd2',
'wtforms',
'Flask-WTF',
'pytimeparse',
'tinydb'
],
tests_require=['pytest', 'pytest-timeout'],