Skip to content

Commit

Permalink
Make test settings for BedtimeEnforcer accessible via --test
Browse files Browse the repository at this point in the history
(More automation for test.sh use)
  • Loading branch information
Stephan Sokolow authored and Stephan Sokolow committed Aug 3, 2015
1 parent bf3a305 commit 7e3f159
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
14 changes: 14 additions & 0 deletions timeclock.py
Expand Up @@ -84,6 +84,7 @@
]

import logging, os, signal, sys
from datetime import datetime, time, timedelta
from importlib import import_module

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -163,6 +164,9 @@ def main():
help="Use separate data store and single instance lock"
"so a development copy can be launched without "
"interfering with normal use")
parser.add_option('--test',
action="store_true", dest="test_mode", default=False,
help="Configure Bedtime Enforcer for testing")
parser.add_option('-v', '--verbose', action="count", dest="verbose",
default=3, help="Increase the verbosity.")
parser.add_option('-q', '--quiet', action="count", dest="quiet",
Expand Down Expand Up @@ -194,6 +198,16 @@ def main():
# Model
model = TimerModel(savefile, default_timers, opts.mode)

if opts.test_mode:
now = datetime.utcnow()
model.bedtime_enforcer = {
'bedtime': time(hour=now.hour, minute=now.minute,
second=now.second),
'sleep_duration': timedelta(seconds=10),
'snooze_duration': timedelta(seconds=5),
'update_interval': timedelta(seconds=1)
}

if opts.mode == 'help':
print "Valid mode names are: %s" % ', '.join(model.timers)
parser.exit(0)
Expand Down
40 changes: 18 additions & 22 deletions timeclock/controllers/bedtime_enforcer.py
Expand Up @@ -16,20 +16,14 @@

MIN_NOTIFICATION_SIZE = (750, 550)

if True:
# Test code
now = datetime.utcnow()
BEDTIME = time(hour=now.hour, minute=now.minute, second=now.second)
SLEEP_DURATION = timedelta(seconds=10) # Minimum allowed
SNOOZE_DURATION = timedelta(seconds=5)
UPD_INTERVAL = timedelta(seconds=1)
else:
# TODO: Make these configurable
# NOTE: Times are in UTC (EST = UTC-5, EDT = UTC-4)
BEDTIME = time(hour=7)
SLEEP_DURATION = timedelta(hours=8) # Minimum allowed
SNOOZE_DURATION = timedelta(minutes=20)
UPD_INTERVAL = timedelta(minutes=1)
# NOTE: Times are in UTC (EST = UTC-5, EDT = UTC-4)
DEFAULTS = {
# TODO: Make these persistent, configurable settings
'bedtime': time(hour=8),
'sleep_duration': timedelta(hours=7), # Minimum allowed
'snooze_duration': timedelta(minutes=20),
'update_interval': timedelta(minutes=1)
}

log = logging.getLogger(__name__)

Expand All @@ -55,17 +49,19 @@ class BedtimeEnforcer(gobject.GObject): # pylint: disable=R0903,E1101
tunings to produce the desired sleep cycle.
"""
epoch = datetime.utcfromtimestamp(0)
upd_interval = UPD_INTERVAL

def __init__(self, model): # pylint: disable=E1002
super(BedtimeEnforcer, self).__init__()
self.config = DEFAULTS.copy()
self.config.update(getattr(model, 'bedtime_enforcer', {}))

self.model = model
self.orig_proctitle = getproctitle()
self.last_tick = self.epoch
self.bedtime = rrule(DAILY,
byhour=BEDTIME.hour,
byminute=BEDTIME.minute,
bysecond=BEDTIME.second,
byhour=self.config['bedtime'].hour,
byminute=self.config['bedtime'].minute,
bysecond=self.config['bedtime'].second,
dtstart=self.epoch)
self.alert_start = self.epoch
self.alert_end = self.epoch
Expand All @@ -85,7 +81,7 @@ def __init__(self, model): # pylint: disable=E1002

def cb_snooze(self, _):
now = datetime.utcnow()
self.alert_start = now + SNOOZE_DURATION
self.alert_start = now + self.config['snooze_duration']
self._upd_alert_time(now)
self.model.emit("action-set-enabled", "Snooze", False)
self.has_snoozed = True
Expand Down Expand Up @@ -115,19 +111,19 @@ def _update_alerting(self, now):
self.model.emit("action-set-enabled", "Snooze", False)

def _upd_alert_time(self, now, force=False):
self.alert_end = self.alert_start + SLEEP_DURATION
self.alert_end = self.alert_start + self.config['sleep_duration']
if self.alert_start < now and self.alert_end < now:
self.has_snoozed = False
self.alert_start = self.bedtime.before(now)
self.alert_end = self.alert_start + SLEEP_DURATION
self.alert_end = self.alert_start + self.config['sleep_duration']

def cb_updated(self, model):
"""Callback to check the time duration once per minute."""
now = datetime.utcnow()

# TODO: Deduplicate this logic without tripping over the GObject
# event loop bug.
if self.last_tick + self.upd_interval < now:
if self.last_tick + self.config['update_interval'] < now:
self.last_tick = now
self._upd_alert_time(now)
self._update_alerting(now)

0 comments on commit 7e3f159

Please sign in to comment.