Skip to content

Commit

Permalink
let user specify absolute time
Browse files Browse the repository at this point in the history
using format -t HH:MM[:ss]
  • Loading branch information
eiennohito committed Jan 16, 2015
1 parent 7acaa8f commit 7b8da83
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a wrapper around the caffeinate utility.
It allows you to force your computer not to sleep (or use other options of `caffeinate`).
However, it doesn't support running utilites, use original `caffeinate` for that.
47 changes: 38 additions & 9 deletions caff.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
#!/usr/bin/env python

from optparse import OptionParser
from optparse import OptionParser, Option, OptionValueError
import re
from os import execvp
from datetime import datetime, timedelta
from copy import copy

parser = OptionParser()
def check_int(s):
try:
return int(s)
except TypeError:
return 0

def check_time(option, opt, value):
m = re.match(r"(\d\d):(\d\d)(?::(\d\d))?", value)
if m is None:
raise OptionValueError("")
else:
return map(check_int, m.groups())

class TimeOption(Option):
TYPES = Option.TYPES + ("time",)
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
TYPE_CHECKER["time"] = check_time

parser = OptionParser(option_class=TimeOption)
parser.add_option('-i', action="store_const", dest='mode', const='i')
parser.add_option('-m', action="store_const", dest='mode', const='m')
parser.add_option('-s', action="store_const", dest='mode', const='s')
parser.add_option('-u', action="store_const", dest='mode', const='u')
parser.add_option('-t', type="time", dest="till")

parser.set_default('mode', 'd')

Expand All @@ -25,15 +45,24 @@
}

total = 0 # 0 seconds by default
d = datetime.now()
if opts.till is None:
for arg in args:
for pattern in re.findall(r"(?:(-?\d+(?:\.\d+)?)([smhd])?)", arg):
(dur, measure) = pattern
total += float(dur) * multipliers[measure]
else:
time = opts.till
d2 = d.replace(hour=time[0], minute=time[1], second=time[2])
if d2 < d:
d2 = d2 + timedelta(days=1)
total = (d2 - d).total_seconds()

for arg in args:
for pattern in re.findall(r"(?:(-?\d+(?:\.\d+)?)([smhd])?)", arg):
(dur, measure) = pattern
total += float(dur) * multipliers[measure]

if (total == 0):
print "Specify duration in format 3.1d 1h5m 2s"
print "Specify duration in format 3.1d 1h5m 2s.\nOtherwise you can use -t 12:30[:15] format to sleep until 12:30.\nUtil will use 24h format and nearest time in the future."
else:
print "Not sleeping for %d seconds" % total
end = datetime.now() + timedelta(seconds = total)
print "Not sleeping for %d seconds, until %s" % (total, end.strftime("%c"))

execvp("/usr/bin/caffeinate", ["caffeinate", "-%s" % mode, "-t", str(int(total))])
execvp("/usr/bin/caffeinate", ["caffeinate", "-%s" % mode, "-t", str(int(total))])

0 comments on commit 7b8da83

Please sign in to comment.