root / PyCon07 / material / pycommunity_documented / generator.py

Revision 57:efd3c5bd5313, 1.5 kB (checked in by Tarek Ziad?? <tarek@…>, 23 months ago)

more on recipes

Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# Copyright (c) 2006 Tarek Ziadé <tarek@ziade.org>
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19# $Id: $
20""" Generator
21"""
22import logging
23
24class BaseTask(object):
25    """base class for task"""
26    def _getName(self):
27        raise NotImplementedError
28
29    def _run(self, configuration):
30        raise NotImplementedError
31
32    def _writeFile(self, path, content):
33        """helper to write a file"""
34        f = open(path, 'w')
35        logging.info('writing %s' % path)
36        try:
37            f.write(content)
38        finally:
39            f.close()
40
41tasks = None
42
43def registerTask(task):
44    """registers a task"""
45    global tasks
46    if tasks is None:
47        tasks = {}
48    instance = task()
49    tasks[instance._getName()] = instance
50
51def run(steps, configuration):
52    """runs a sequence"""
53    for step in steps:
54        tasks[step]._run(configuration)
Note: See TracBrowser for help on using the browser.