PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/hyde/publisher.py

http://github.com/hyde/hyde
Python | 63 lines | 42 code | 13 blank | 8 comment | 6 complexity | f23ae9a5bace64d4f37a93f7aacbe661 MD5 | raw file
 1import abc
 2from operator import attrgetter
 3
 4from commando.util import getLoggerWithNullHandler, load_python_object
 5
 6from hyde._compat import with_metaclass
 7
 8"""
 9Contains abstract classes and utilities that help publishing a website to a
10server.
11"""
12
13
14class Publisher(with_metaclass(abc.ABCMeta)):
15
16    """
17    The abstract base class for publishers.
18    """
19
20    def __init__(self, site, settings, message):
21        super(Publisher, self).__init__()
22        self.logger = getLoggerWithNullHandler(
23            'hyde.engine.%s' % self.__class__.__name__)
24        self.site = site
25        self.message = message
26        self.initialize(settings)
27
28    @abc.abstractmethod
29    def initialize(self, settings):
30        pass
31
32    @abc.abstractmethod
33    def publish(self):
34        if not self.site.config.deploy_root_path.exists:
35            raise Exception("Please generate the site first")
36
37    @staticmethod
38    def load_publisher(site, publisher, message):
39        logger = getLoggerWithNullHandler('hyde.engine.publisher')
40        try:
41            settings = attrgetter("publisher.%s" % publisher)(site.config)
42        except AttributeError:
43            settings = False
44
45        if not settings:
46            # Find the first configured publisher
47            try:
48                publisher = site.config.publisher.__dict__.iterkeys().next()
49                logger.warning(
50                    "No default publisher configured. Using: %s" % publisher)
51                settings = attrgetter("publisher.%s" % publisher)(site.config)
52            except (AttributeError, StopIteration):
53                logger.error(
54                    "Cannot find the publisher configuration: %s" % publisher)
55                raise
56
57        if not hasattr(settings, 'type'):
58            logger.error(
59                "Publisher type not specified: %s" % publisher)
60            raise Exception("Please specify the publisher type in config.")
61
62        pub_class = load_python_object(settings.type)
63        return pub_class(site, settings, message)