Skip to content

Instantly share code, notes, and snippets.

@entrity
Created March 9, 2015 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entrity/0f0cada24efff8b0729c to your computer and use it in GitHub Desktop.
Save entrity/0f0cada24efff8b0729c to your computer and use it in GitHub Desktop.
Python timer app. When timer expires, it jiggles the icon in the unity launcher until a 'KILL' signal is sent to its pipe.
#!/usr/bin/env python
from gi.repository import Unity, GObject, Dbusmenu
import time, sys, re, os, pipes, StringIO
DESKTOP_FILE_NAME = 'my-timer.desktop'
PIPEFILE_NAME = '/tmp/my-timer-pipefile'
# Assumes your desktop file is 'my-timer.desktop'
launcher = Unity.LauncherEntry.get_for_desktop_id(DESKTOP_FILE_NAME)
launcher.set_property('progress_visible', True)
loop = GObject.MainLoop()
progress = 0
if not os.path.exists(PIPEFILE_NAME):
os.mkfifo(PIPEFILE_NAME)
pipefilefd = os.open(PIPEFILE_NAME, os.O_RDONLY|os.O_NONBLOCK)
pipefile = os.fdopen(pipefilefd, 'r')
kill_flag = False
def init():
# Calc timeout and start time
timearr = re.split('\D+', sys.argv[1])
print 'Parsed time:', timearr
n = len(timearr)
hr = int(timearr[-3]) if n >= 3 else 0
mn = int(timearr[-2] or 0) if n >= 2 else int(timearr[0])
sc = int(timearr[-1]) if n >= 2 else 0
global seconds, starttime
seconds = sc + 60*mn + 360*hr
starttime = GObject.get_current_time()
print 'Timer for '+str(seconds)+' seconds'
# Quicklist
global timedisp
ql = Dbusmenu.Menuitem.new()
timedisp = Dbusmenu.Menuitem.new()
timedisp.property_set(Dbusmenu.MENUITEM_PROP_LABEL, "Remaining: *")
timedisp.property_set_bool(Dbusmenu.MENUITEM_PROP_VISIBLE, True)
ql.child_append(timedisp)
launcher.set_property('quicklist', ql)
def cleanup():
pipefile.close()
os.remove(PIPEFILE_NAME);
loop.quit()
def do_urgency():
if kill_flag:
cleanup()
return False
else:
urgent = launcher.get_property("urgent")
launcher.set_property("urgent", not urgent)
return True
def update_progress():
tdelta = GObject.get_current_time() - starttime
progress = tdelta/seconds
if progress >= 1.0:
launcher.set_property('progress_visible', False)
GObject.timeout_add_seconds(1, do_urgency)
return False
else:
launcher.set_property('progress', progress)
remaining = seconds - tdelta
mn = remaining / 60
hr = mn / 60
stamp = StringIO.StringIO()
stamp.write("Remaining: %d:%02d:%02d" % (hr, mn%60, remaining%60))
timedisp.property_set(Dbusmenu.MENUITEM_PROP_LABEL, stamp.getvalue())
return True
def pipefile_in_cb(src, condition):
data = src.read()
print 'DATA READ:', data
if re.match('.*KILL.*', data):
print 'setting kill flag'
global kill_flag
kill_flag = True
else:
return True
init()
GObject.timeout_add_seconds(1, update_progress)
GObject.io_add_watch(pipefile, GObject.IO_IN, pipefile_in_cb)
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment