HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Review request: a simple Cron wrapper in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
cronsimplerequestwrapperpythonreview

Problem

This is my first attempt to create a reusable and tested module. Any comments is highly appreciated.

```
#-- coding: utf-8 --
"""
Micron: a micro wrapper around Unix crontab.

Micron is a thin wrapper around Unix crontab command. It let you add and remove
jobs from the crontab file or remove the entire crontab file. "crontab" is the
program used to install, deinstall or list the tables used to drive the cron
daemon

How it works:

>>> #Obviously you need to import the module
>>> import micron
>>> #Then you create a crontab instance
>>> crontab = micron.CronTab()
>>> #Assuming your crontab does not exist trying to read it will rise an error
>>> crontab.read()
Traceback (most recent call last):
...
raise CronTabMissing()
CronTabMissing: Current crontab does not exist
>>> #Now you can add some jobs. Micron supports some common presets
>>> sorted(crontab.PRESETS.items())
[('daily', '0 0 '), ('hourly', '0 '), ('monthly', '0 0 1 '), ('weekly', '0 0 * 1 ')]
>>> crontab.add_job('weekly', 'echo "WOW"', 0)
>>> crontab.add_job('daily', 'echo "BOOM!"', 1)
>>> #You can omit the job id and micron will generate it for you
>>> crontab.add_job('daily', 'echo "BOOM!"')
>>> #Read again the crontab content
>>> crontab.read()
['0 0 1 echo "WOW" #MICRON_ID_0', '0 0 echo "BOOM!" #MICRON_ID_1', '0 0 echo "BOOM!" #MICRON_ID_2']
>>> #See how it look in the crontab file
>>> print crontab
0 0 1 echo "WOW" #MICRON_ID_0
0 0 * echo "BOOM!" #MICRON_ID_1
0 0 * echo "BOOM!" #MICRON_ID_2
>>> #Remove job with id 0
>>> crontab.remove_job(0)
>>> print crontab
0 0 * echo "BOOM!" #MICRON_ID_1
0 0 * echo "BOOM!" #MICRON_ID_2
>>> #Remove job with non existing id and you'll get and error
>>> crontab.remove_job(11)
Traceback (most recent call last):
...
raise ValueError('Id %s not in crontab' % job_id)
ValueError: Id 11 not in crontab
>>> #If the presets are not enough, you can add your own timing using the
>>> #crontab syntax
>>> cron

Solution

I think it is a pretty good start:

  • I would like to see a Job class.



  • The stderr output would be great in cases of an retcode != 0.



  • A validation and/or escaping of timing and program would be great.

Context

StackExchange Code Review Q#2846, answer score: 2

Revisions (0)

No revisions yet.