205
211
self.__write__ ()
208
def append (self, minute, hour, day, month, weekday, command, nooutput, title, icon = None):
214
def append (self, job):
210
216
The method to add an entry to crontab (see man 5 crontab )
216
@param command: The command to execute
217
@param nooutput: True if we don't want any output (False for normal behaviour)
218
@param title: The title of the task (might be used for a search )
219
@param icon: an icon path to associate
217
@param job: The job to append
222
record = minute + " " + hour + " " + day + " " + month + " " + weekday + " " + command
225
if record[len(record)-1] == " ":
227
record = record + space + self.nooutputtag
228
if title != None and icon == None:
229
record = record + " # " + title
230
elif title != None and icon != None:
231
record = record + " # " + title + ", " + icon
232
elif title == None and icon != None:
233
record = record + " # " + _("Untitled") + ", " + icon
219
if not isinstance(job, Job):
220
raise Exception ("job should be of type Job")
225
if job.periodicity == Scheduler.YEAR :
226
record = "%d %d %d %d *" % (d.minute,d.hour,d.day, d.month )
227
elif job.periodicity == Scheduler.MONTH :
228
record = "%d %d %d * *" % (d.minute,d.hour,d.day )
229
elif job.periodicity == Scheduler.WEEK :
230
record = "%d %d * * %d" % (d.minute,d.hour,calendar.weekday(d.year, d.month, d.day)+1 )
231
elif Job.periodicity == Scheduler.DAY :
232
record = "%d %d * * *" % (d.minute,d.hour )
233
elif Job.periodicity == Scheduler.HOUR :
234
record = "%d * * * *" % (d.minute )
236
record = "%d %d %d %d %d" % (d.minute,d.hour,d.day, d.month, calendar.weekday(d.year, d.month, d.day)+1 )
238
record = record + " %s " % job.command
240
record = record + " # %d;%s;%s" % (job.id, job.title, job.icon)
235
242
self.lines.append (record)
237
244
# TODO: let write trow an exception if failed
238
245
self.__write__ ()
241
247
def humanRead(self,title=None):
342
348
min hour day month wday command comment/title-icon or end
343
349
The regexp: ('([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^#\n$]*)(\s#\s([^\n$]*)|$)')
344
A record: * * * * * echo $a >/dev/null 2>&1 # Untitled, /usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application.png
350
A record: * * * * * echo $a >/dev/null 2>&1 # 1;Untitled;/usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application.png
346
352
Parse a crontab line and return it as a tuple
347
353
@param line: The crontab line
348
354
@return: (minute, hour, day, month, weekday, command, title, icon) or None for empty line and comment
349
355
@raise Exception: if the line is neither a comment nor empty and is invalid
357
def getPeriodicity(line):
358
yearlyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) ([0-9]{,2}) ([0-9]{,2}) (\*) .+$")
359
monthlyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) ([0-9]{,2}) (\*) (\*) .+$")
360
weeklyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) (\*) (\*) ([0-9]{,2}) .+$")
361
dailyRE = re.compile("([0-9]{,2}) ([0-9]{,2}) (\*) (\*) (\*) .+$")
362
hourlyRE = re.compile("([0-9]{,2}) (\*) (\*) (\*) (\*) .+$")
364
if yearlyRE.match(line) : return Scheduler.YEAR
365
elif monthlyRE.match(line) : return Scheduler.MONTH
366
elif weeklyRE.match(line) : return Scheduler.WEEK
367
elif dailyRE.match(line) : return Scheduler.DAY
368
elif hourlyRE.match(line) : return Scheduler.HOUR
370
def convertToDatetime(srematch):
371
now = datetime.today()
374
if srematch.group(1) == '*' :
376
else : minute = int(srematch.group (1))
378
if srematch.group(2) == '*' :
380
else : hour = int(srematch.group (2))
382
if srematch.group (3) == '*' :
384
else : day = int(srematch.group(3))
386
if srematch.group (4) == '*' :
388
else : month = int(srematch.group(4))
391
if srematch.group (5) == '*' :
392
weekday = calendar.weekday(year, month, day)
393
else : weekday = int(srematch.group(5))
395
return datetime(year,month,day,hour,minute)
351
397
if len (line) > 1 and line[0] != '#':
353
399
m = self.crontabRecordRegex.match(line)
356
minute = m.groups ()[0]
357
hour = m.groups ()[1]
359
month = m.groups ()[3]
360
weekday = m.groups ()[4]
361
command = m.groups ()[5]
363
#icon path is in comment of the task, this is the default
366
#title is in comment of the task
368
if m.groups ()[7] != None:
369
# TODO: check if works
370
lastpiece = m.groups ()[7]
371
keep = lastpiece.split (", ")
377
title = _("Untitled")
379
return minute, hour, day, month, weekday, command, title, icon
403
result.date = convertToDatetime(m)
406
result.periodicity = self.__getPeriodicity(line)
409
result.user = m.group(6)
412
result.command = m.group(7)
415
result.id = m.group(9)
418
result.title = m.group(10)
420
#icon path is in comment of the task, this is the default
421
result.icon = m.group(11)
382
427
raise Exception( _("ERROR: Failed to parse crontab record : [ %s ]") % line)
388
432
def __easy__ (self, minute, hour, day, month, weekday):