Source

celery-paste / celerypaste / __init__.py

Full commit
import os
import imp

from paste.script.command import Command, BadCommand
from paste.deploy import appconfig

from celery.loaders.default import Loader

KEY_ENV_LOADER = "CELERY_LOADER"


class PasteLoader(Loader):

    def __init__(self, config):
        self._config = config

    def read_configuration(self):
        config = dict(self._config)
        init_tasks = config.pop("tasks", None) # XXX What is this?
        # this trips up django, and paste puts this in by default
        config.pop("__file__", None)
        self.django_settings = self.setup_django_env(config)
        return self.django_settings


class PasteLoaderFactory(object):

    def __init__(self, **config):
        self._config = config

    def __call__(self):
        return PasteLoader(self._config)


def set_loader(config):
    """
    order of operations is important
    """
    os.environ[KEY_ENV_LOADER] = __name__
    os.environ["DJANGO_SETTINGS_MODULE"] = __name__ #why?
    _config={}

    from celery.loaders.default import DEFAULT_SETTINGS
    _config.update(DEFAULT_SETTINGS)
    _config.update(config)

    global Loader
    Loader = PasteLoaderFactory(**_config) #config from ini file


class BaseCommand(Command):
    """"""

    def __init__(self, command_name):
        Command.__init__(self, command_name)
        self.min_args = 1
        self.max_args = 1
        self.summary =self. __doc__.splitlines()[0]
        self.usage = '\n' +self. __doc__
        self.parser = Command.standard_parser()
        self.parser.add_option("--section",
                          action="store",
                          type="string",
                          dest="section",
                          help="section in config file to pickup settings")

    def init_loader(self):
        paste_config = {}
        paste_config.update(self.get_config(self.parse_section()))
        set_loader(paste_config)

    def parse_section(self):
        return self.options.section

    def get_config(self, name):
        if len(self.args)==1:
            config_file = self.args[0]
            if not os.path.isfile(config_file):
                raise BadCommand(
"""
%s
Error: CONFIG_FILE not found at %s%s.
Please specify a CONFIG_FILE"""%
                    (self.parser.get_usage(),
                    os.path.sep,
                    config_file))
            else:
                config = appconfig("config:%s" % config_file,
                                   name = name,
                                   relative_to = os.getcwd())
                return config
        else:
            raise BadCommand(self.parser.get_usage())


class CelerySyncDBCmd(BaseCommand):
    """
    run with settings from paste config file

    Example::
       $ paster celerysyncdb production.ini --section <section_name>
    """

    def command(self):
        import sys
        self.init_loader()
        from celery.loaders import current_loader
        loader = current_loader()
        conf = loader.read_configuration()
        from django.core.management import call_command, setup_environ
        sys.stderr.write("Creating database tables...\n")
        print conf
        setup_environ(conf)
        call_command("syncdb")


class CeleryDCmd(BaseCommand):
    """
    run with settings from paste config file

    Example::
       $ paster celeryd production.ini --section <section_name>
    """

    def _get_celeryd_opts(self, config):
        """
        any option in the config prefixed with 'celeryd.' will be
        treated like command line arguments to celeryd
        """
        celeryd_keys = []
        result = {}
        for k in config.keys():
            if k.lower().startswith("celeryd."):
                celeryd_keys.append(k)

        for k in celeryd_keys:
            result[k.replace("celeryd.", "")] = config.pop(k)

        return result

    def command(self):
        self.init_loader()
        options = {}
        options.update(
            self._get_celeryd_opts(
                self.get_config(self.parse_section())))
        #any celeryd. options should be in options at this point
        #be careful about when you import this, once you do, the
        #settings are initialized
        from celery.bin.celeryd import run_worker
        run_worker(**options)


class CeleryApp(object):
    """
    this does nothing except allow for a config section to be defined
    for on it's own
    """