예제 #1
0
            print an_event.title.text ,"Scheduled:",i,"For:",time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.start_time))),"Current Time:",time.strftime('%d-%m-%Y %H:%M')
            if time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.start_time))) == time.strftime('%d-%m-%Y %H:%M'):
                print "Waking you up!"
                print "---" 
                songfile = random.choice(os.listdir(mp3_path)) #  choosing by random an .mp3 file from direcotry
                print "Now Playing:", songfile
                                                               #  plays the MP3 in it's entierty. As long as the file is longer 
                                                               #  than a minute it will only be played once:
                command ="mpg321" + " " + mp3_path + "'"+songfile+"'"+ " -g 100" 
                print command
                os.system(command)                             #  plays the song
            else:
                print "Wait for it..."                         #  the event's start time is not the system's current time
 
#************************************************************************************# 
#****           Function to be run by Scheduler                                  ****#
#****           The prints are more for debug than actual necessity              ****#
#************************************************************************************# 
def callable_func():
    os.system("clear")
    print "----------------------------"
    FullTextQuery(calendar_service)
    print "----------------------------"

#************************************************************************************# 
#****           Run scheduler service                                            ****#
#************************************************************************************# 
sched = BackgroundScheduler(standalone=True)
sched.add_interval_job(callable_func,seconds=10)  #  define refresh rate. Set to every 10 seconds by default
sched.start()                                     #  runs the program indefinatly on an interval of x seconds
예제 #2
0
class Scheduler(Plugin):

    crons = {}
    intervals = {}
    started = False

    def __init__(self):

        add_event('schedule.cron', self.cron)
        add_event('schedule.interval', self.interval)
        add_event('schedule.remove', self.remove)
        add_event('schedule.queue', self.queue)

        self.sched = BackgroundScheduler(misfire_grace_time=60)
        self.sched.start()
        self.started = True

    def remove(self, identifier):
        for cron_type in ['intervals', 'crons']:
            try:
                self.sched.unschedule_job(
                    getattr(self, cron_type)[identifier]['job'])
                log.debug('%s unscheduled %s',
                          (cron_type.capitalize(), identifier))
            except:
                pass

    def do_shutdown(self, *args, **kwargs):
        self.stop()
        return super(Scheduler, self).do_shutdown(*args, **kwargs)

    def stop(self):
        if self.started:
            log.debug('Stopping scheduler')
            self.sched.shutdown(wait=False)
            log.debug('Scheduler stopped')
        self.started = False

    def cron(self, identifier='', handle=None, day='*', hour='*', minute='*'):
        log.info('Scheduling "%s", cron: day = %s, hour = %s, minute = %s',
                 (identifier, day, hour, minute))

        self.remove(identifier)
        self.crons[identifier] = {
            'handle':
            handle,
            'day':
            day,
            'hour':
            hour,
            'minute':
            minute,
            'job':
            self.sched.add_cron_job(handle, day=day, hour=hour, minute=minute)
        }

    def interval(self,
                 identifier='',
                 handle=None,
                 hours=0,
                 minutes=0,
                 seconds=0):
        log.info(
            'Scheduling %s, interval: hours = %s, minutes = %s, seconds = %s',
            (identifier, hours, minutes, seconds))

        self.remove(identifier)
        self.intervals[identifier] = {
            'handle':
            handle,
            'hours':
            hours,
            'minutes':
            minutes,
            'seconds':
            seconds,
            'job':
            self.sched.add_interval_job(handle,
                                        hours=hours,
                                        minutes=minutes,
                                        seconds=seconds)
        }

        return True

    def queue(self, handlers=None):
        if not handlers: handlers = []

        for h in handlers:
            h()

            if self.shuttingDown():
                break

        return True