[b5d87e0] | 1 | from pyramid.paster import bootstrap |
---|
| 2 | from pyramid.paster import setup_logging |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | class TasksManager(object): |
---|
| 6 | |
---|
| 7 | def __init__(self): |
---|
| 8 | """ |
---|
| 9 | managers is a dict containing all the available tasks. The keys are |
---|
| 10 | the name used to reference this tasks (the action parameter accepted |
---|
| 11 | on runtime) while the values are the functions/methods responsible to |
---|
| 12 | run the task. |
---|
| 13 | |
---|
| 14 | Those functions/methods have to expect one parameter (the environment |
---|
| 15 | we get from bootstraping pyramid, see the run() method to learn more) |
---|
| 16 | """ |
---|
| 17 | self.managers = {} |
---|
| 18 | |
---|
| 19 | def usage(self, script_name): |
---|
| 20 | """ |
---|
| 21 | Prints command line usage. |
---|
| 22 | |
---|
| 23 | The description of the different tasks is gathered from the docstring |
---|
| 24 | of the methods that will be called to perform the task. |
---|
| 25 | """ |
---|
| 26 | docs = [t + ': ' + self.managers[t].__doc__ for t in self.managers] |
---|
| 27 | msg = """ |
---|
| 28 | Usage: %s /path/to/settings.ini [%s] |
---|
| 29 | |
---|
| 30 | settings.ini has to be a PasteDeploy .ini file representing your |
---|
| 31 | application configuration. |
---|
| 32 | |
---|
| 33 | %s |
---|
| 34 | |
---|
| 35 | """ % (script_name, '|'.join(self.managers), '\n\t '.join(docs)) |
---|
| 36 | print(msg) |
---|
| 37 | |
---|
| 38 | def add_task(self, name, method): |
---|
| 39 | self.managers[name] = method |
---|
| 40 | |
---|
| 41 | def remove_task(self, name): |
---|
| 42 | if name in self.managers: |
---|
| 43 | del self.managers[name] |
---|
| 44 | |
---|
| 45 | def run(self, script_name, ini_file, action): |
---|
| 46 | env = bootstrap(ini_file) |
---|
| 47 | setup_logging(ini_file) |
---|
| 48 | if action not in self.managers.keys(): |
---|
| 49 | self.usage(script_name) |
---|
| 50 | else: |
---|
| 51 | self.managers[action](env) |
---|
| 52 | env['closer']() |
---|