mozilla/crontabber — pull request #13 by twobraids (2014-04-25 14:46:07+00:00)
Title: added safe setup for defaults

_don't panic - the change here is not nearly as major this patch makes it look._

This PR is in response to the problem in Socorro of setting up the CronTabber app defaults. Recall that reaching in directly to the Configman required config options to set the ```jobs``` and ```database_class``` results in the changing the defaults for the ```CronTabber``` class itself. That is undesirable and results in really ugly looking code.

I propose that we add a method ```get_crontabber_class``` to the ```crontabber.app``` module that accepts the job and database strings. That method creates the ```Crontabber``` (& ```JobStateDatabase```) classes with the proper defaults and then returns that class ready to be instantiated.

>>> from crontabber.app import get_crontabber_class
>>> my_jobs = [ ... ]
>>> my_database_class = ...
>>> MyCronTabber = get_crontabber_class(my_jobs, my_database_class)
>>> ct = MyCronTabber(my_config)

This means the definitions of the ```CronTabber``` and ```JobStateDatabase``` classes has been moved from module level scope to within the ```get_crontabber_class``` method.

If you don't want to make a custom class, the original CronTabber and JobStateDatabase classes are still part of the module. They're defined automatically by running the ```get_crontabber_class``` using the original defaults.

Over in the Socorro world, the crontabber_app.py becomes the much prettier:

from crontabber.app import get_crontabber_class, main

DEFAULT_JOBS = '''
...
'''

if __name__ == '__main__': # pragma: no cover
import sys
sys.exit(main(get_crontabber_class(
DEFAULT_JOBS,
'socorro.external.postgresql.connection_context.ConnectionContext'
)))