2011-11-13 20:55:45 +00:00
from plugin import BasePlugin
import curses
import common
import timed_events
class Plugin ( BasePlugin ) :
def init ( self ) :
2012-05-06 20:09:16 +00:00
self . add_command ( ' remind ' , self . command_remind , " Usage: /remind <time in seconds> <todo> \n Reminder: remind you of <todo> every <time> seconds.. " , self . completion_remind )
2011-11-14 19:46:19 +00:00
self . add_command ( ' done ' , self . command_done , " Usage: /done <id> \n Done: Stop reminding you do the task identified by <id> " , self . completion_done )
2011-11-13 20:55:45 +00:00
self . add_command ( ' tasks ' , self . command_tasks , " Usage: /tasks \n Tasks: List all the current tasks and their ids. " , None )
self . tasks = { }
self . count = 0
2011-11-14 19:46:19 +00:00
for option in self . config . options ( self . __module__ ) :
id , secs = option . split ( ' , ' )
id = int ( id )
if id > self . count :
self . count = id
value = self . config . get ( option , ' ' )
self . tasks [ id ] = ( int ( secs ) , value )
self . config . remove_section ( self . __module__ )
self . config . add_section ( self . __module__ )
if self . tasks :
self . count + = 1
self . command_tasks ( ' ' , nocommand = True )
2011-11-13 20:55:45 +00:00
def command_remind ( self , arg ) :
args = common . shell_split ( arg )
if len ( args ) < 2 :
return
2011-11-15 20:42:10 +00:00
time = common . parse_str_to_secs ( args [ 0 ] )
if not time :
2011-11-13 20:55:45 +00:00
return
self . tasks [ self . count ] = ( time , args [ 1 ] )
timed_event = timed_events . DelayedEvent ( time , self . remind , self . count )
self . core . add_timed_event ( timed_event )
2011-11-15 20:42:10 +00:00
self . core . information ( ' Task %s added: %s every %s . ' % ( self . count , args [ 1 ] ,
common . parse_secs_to_str ( time ) ) , ' Info ' )
2011-11-13 20:55:45 +00:00
self . count + = 1
2011-11-14 19:46:19 +00:00
def completion_remind ( self , the_input ) :
txt = the_input . get_text ( )
args = common . shell_split ( txt )
n = len ( args )
if txt . endswith ( ' ' ) :
n + = 1
if n == 2 :
2011-11-14 19:57:54 +00:00
return the_input . auto_completion ( [ " 60 " , " 5m " , " 15m " , " 30m " , " 1h " , " 10h " , " 1 d " ], ' ' )
2011-11-14 19:46:19 +00:00
def completion_done ( self , the_input ) :
return the_input . auto_completion ( [ " %s " % key for key in self . tasks ] , ' ' )
2011-11-13 20:55:45 +00:00
def command_done ( self , arg = " 0 " ) :
try :
id = int ( arg )
except :
return
if not id in self . tasks :
return
2011-11-14 19:46:19 +00:00
self . core . information ( ' Task %s : %s [DONE] ' % ( id , self . tasks [ id ] [ 1 ] ) , ' Info ' )
2011-11-13 20:55:45 +00:00
del self . tasks [ id ]
2011-11-14 19:46:19 +00:00
def command_tasks ( self , arg , nocommand = None ) :
if nocommand :
s = ' The following tasks were loaded: \n '
else :
s = ' The following tasks are active: \n '
2011-11-13 20:55:45 +00:00
for key in self . tasks :
2011-11-15 20:42:10 +00:00
s + = ' Task %s : %s every %s . \n ' % ( key , repr ( self . tasks [ key ] [ 1 ] ) ,
common . parse_secs_to_str ( self . tasks [ key ] [ 0 ] ) )
2011-11-13 20:55:45 +00:00
if s :
self . core . information ( s , ' Info ' )
def remind ( self , id = 0 ) :
if not id in self . tasks :
return
self . core . information ( ' Task %s : %s ' % ( id , self . tasks [ id ] [ 1 ] ) , ' Info ' )
if self . config . get ( ' beep ' , ' ' ) == ' true ' :
curses . beep ( )
timed_event = timed_events . DelayedEvent ( self . tasks [ id ] [ 0 ] , self . remind , id )
self . core . add_timed_event ( timed_event )
2011-11-14 19:46:19 +00:00
def cleanup ( self ) :
if self . tasks :
self . config . remove_section ( self . __module__ )
self . config . add_section ( self . __module__ )
for task in self . tasks :
self . config . set ( ' %s , %s ' % ( task , self . tasks [ task ] [ 0 ] ) , self . tasks [ task ] [ 1 ] )
self . config . write ( )