opensourcejason.info
deskbar applet timer
#
#  timer.py : A timer module
#
#  Copyright (C) 2006 by Jason LeBrun
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  Authors: Jason LeBrun <jason@jasonlebrun.info>
#


# Enable this to see what goes wrong when evaluating the string.
# Since half-written strings get evaluated, this produces a lot
# of crap as well as the answer you want.
#_debug = False
_debug = True

from gettext import gettext as _
import deskbar.Handler, deskbar.Match
import deskbar.Categories
import gtk,gobject
import re
import cgi
from threading import Thread
import time
if _debug:
    import traceback

HANDLERS = {
    "TimerHandler" : {
    "name" : _("Timer"),
    "description" : _("Set a timer"),
    "version" : "0.1",
    }
}

debug=open("/home/jblebrun/timer_debug",'w')
TIMERS={}

class TimerMatch (deskbar.Match.Match):

    def __init__(self, backend, name=None, t=None, **args):
        for i in args.keys():
            print i
        deskbar.Match.Match.__init__ (self, backend, **args)
        self.name=name
        self.t=t

    def fire(self,*args):
        print "Timer fired:",a,b,c
        debug.write("Timer fired: %s\n"%self.name)
        debug.flush()
        dialog.show()
        debug.write("Window shown\n")
        debug.flush()


    def get_name (self, text=None):
        if self.name in TIMERS.keys():
            remaining=TIMERS[self.name]-time.time()
            h=remaining/3600
            m=remaining%3600/60
            s=remaining%3600%60
            escapedtext=cgi.escape("%s (%02d:%02d:%02d remaining)"%(self.name,h,m,s))
            return { "name" : "Cancel Timer", "escapedtext" : escapedtext }
        elif self.t:
            escapedtext=cgi.escape("%s (%02d:%02d:%02d)"%((self.name,)+self.t))
            return { "name" : "Set Timer", "escapedtext" : escapedtext }
        else:
            return { "name" : "Huh?", "escapedtext" : text }

    def get_verb (self):
        return "%(name)s <b>%(escapedtext)s</b>"

    def get_category (self):
        return "actions"

    def action (self, text=None):
        print self.name,text
        if self.name in TIMERS.keys():
            print "Cancelled timer"
            del TIMERS[self.name]

        elif text:
            print "Setting timer"
            now=time.time()
            TIMERS[self.name]=now+self.t[0]*3600+self.t[1]*60+self.t[2]
        else:
            print "Doing nothing"

class TimerHandler (deskbar.Handler.Handler):


    def __init__ (self):
        deskbar.Handler.Handler.__init__ (self, "gnome-set-time")
        self.timerre=re.compile("(([0-9]+)h){0,1}(([0-9]+)m){0,1}(([0-9]+)s){0,1}([ \t]+|$)(.+)*")
        self.timers={}

    def destroy(self, dialog, *args):
        dialog.destroy()

    def alert(self, t):
        dialog = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT,  gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Timer %s is finished!"%(t))
        dialog.set_title("Timer")
        dialog.connect("response", self.destroy, dialog);
        dialog.show()

    def check_timers(self, **args):
        now=time.time()
        for t in TIMERS.keys():
            if TIMERS[t]-now < 0:
                self.alert(t)
                del TIMERS[t]
        return True

    def initialize(self):
        gobject.timeout_add(1000,self.check_timers)


    def query (self, query):
        try:
            results=[]
            for key in TIMERS.keys():
                results.append(TimerMatch(self, name=key))
            match=self.timerre.match(query.lower())
            if match:
                hours,mins,secs,name=match.groups()[1:8:2]
                if not name:
                    name="Timer %s"%(len(TIMERS.keys())+1)
                print "made name:",name
                if not hours: hours=0
                if not mins: mins=0
                if not secs: secs=0
                hours,mins,secs=map(int,[hours,mins,secs])
                mins += secs/60
                secs %= 60
                hours += mins/60
                mins %= 60
                if(hours or mins or secs):
                    t=(hours,mins,secs)
                    results.append(TimerMatch(self, name=name, t=t))
            return results
        except:
            if _debug:
                traceback.print_exc ()
            return []
if __name__ == '__main__':

    import sys

    print __file__,': Running unit test'
    ah = TimerHandler()
    ah.initialize()

    hits = ah.query('1s')

    for h in hits:
        print '%s: action %s ? (y/n)' % (h.get_category(), h.get_verb()),
        if sys.stdin.read(1) == 'y':
            print "doing it"
            h.action('1s')
    print ''
    time.sleep(3)

Add Comment 
Sign as Author 
Enter code 741