diff --git a/pelican/__init__.py b/pelican/__init__.py index 915d92c7..f0684d65 100755 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -10,71 +10,79 @@ from pelican.generators import (ArticlesGenerator, PagesGenerator, VERSION = "2.5.3" -def init_params(settings=None, path=None, theme=None, output_path=None, - markup=None, keep=False): - """Read the settings, and performs some checks on the environment - before doing anything else. - """ - if settings is None: - settings = {} - settings = read_settings(settings) - path = path or settings['PATH'] - if path.endswith('/'): - path = path[:-1] +class Pelican(object): + def __init__(self, settings=None, path=None, theme=None, output_path=None, + markup=None, keep=False): + """Read the settings, and performs some checks on the environment + before doing anything else. + """ + self.path = path or settings['PATH'] + if self.path.endswith('/'): + self.path = path[:-1] - # define the default settings - theme = theme or settings['THEME'] - output_path = output_path or settings['OUTPUT_PATH'] - output_path = os.path.realpath(output_path) - markup = markup or settings['MARKUP'] - keep = keep or settings['KEEP_OUTPUT_DIRECTORY'] + # define the default settings + self.settings = settings + self.theme = theme or settings['THEME'] + self.path = path + output_path = output_path or settings['OUTPUT_PATH'] + self.output_path = os.path.realpath(output_path) + self.markup = markup or settings['MARKUP'] + self.keep = keep or settings['KEEP_OUTPUT_DIRECTORY'] - # find the theme in pelican.theme if the given one does not exists - if not os.path.exists(theme): - theme_path = os.sep.join([os.path.dirname( - os.path.abspath(__file__)), "themes/%s" % theme]) - if os.path.exists(theme_path): - theme = theme_path - else: - raise Exception("Impossible to find the theme %s" % theme) + # find the theme in pelican.theme if the given one does not exists + if not os.path.exists(self.theme): + theme_path = os.sep.join([os.path.dirname( + os.path.abspath(__file__)), "themes/%s" % self.theme]) + if os.path.exists(theme_path): + self.theme = theme_path + else: + raise Exception("Impossible to find the theme %s" % theme) - # get the list of files to parse - if not path: - raise Exception('you need to specify a path to search the docs on !') - - return settings, path, theme, output_path, markup, keep + # get the list of files to parse + if not self.path: + raise Exception('you need to specify a path to search the docs on !') -def run_generators(generators, settings, path, theme, output_path, markup, keep): - """Run the generators and return""" + def run(self): + """Run the generators and return""" - context = settings.copy() - generators = [p(context, settings, path, theme, output_path, markup, keep) - for p in generators] + context = self.settings.copy() + generators = [ + cls( + context, + self.settings, + self.path, + self.theme, + self.output_path, + self.markup, + self.keep + ) for cls in self.get_generator_classes() + ] - for p in generators: - if hasattr(p, 'generate_context'): - p.generate_context() + for p in generators: + if hasattr(p, 'generate_context'): + p.generate_context() - # erase the directory if it is not the source - if output_path not in os.path.realpath(path) and not keep: - clean_output_dir(output_path) + # erase the directory if it is not the source + if os.path.realpath(self.path).startswith(self.output_path) and not keep: + clean_output_dir(self.output_path) - writer = Writer(output_path) + writer = self.get_writer() - for p in generators: - if hasattr(p, 'generate_output'): - p.generate_output(writer) + for p in generators: + if hasattr(p, 'generate_output'): + p.generate_output(writer) -def run_pelican(settings, path, theme, output_path, markup, delete): - """Run pelican with the given parameters""" + def get_generator_classes(self): + generators = [ArticlesGenerator, PagesGenerator, StaticGenerator] + if self.settings['PDF_GENERATOR']: + generators.append(PdfGenerator) + return generators + + def get_writer(self): + return Writer(self.output_path) - params = init_params(settings, path, theme, output_path, markup, delete) - generators = [ArticlesGenerator, PagesGenerator, StaticGenerator] - if params[0]['PDF_GENERATOR']: # param[0] is settings - generators.append(PdfGenerator) - run_generators(generators, *params) def main(): @@ -106,7 +114,18 @@ def main(): # the variable with None. markup = [a.strip().lower() for a in args.markup.split(',')] if args.markup else None - run_pelican(args.settings, args.path, args.theme, args.output, markup, args.keep) + if args.settings is None: + settings = {} + settings = read_settings(args.settings) + + cls = settings.get('PELICAN_CLASS') + if isinstance(cls, basestring): + module, cls_name = cls.rsplit('.', 1) + module = __import__(module) + cls = getattr(module, cls_name) + + pelican = cls(settings, args.path, args.theme, args.output, markup, args.keep) + pelican.run() if __name__ == '__main__': diff --git a/pelican/settings.py b/pelican/settings.py index 8f0dcdbd..0fef4fec 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -22,6 +22,7 @@ _DEFAULT_CONFIG = {'PATH': None, 'CLEAN_URLS': False, # use /blah/ instead /blah.html in urls 'RELATIVE_URLS': True, 'DEFAULT_LANG': 'en', + 'PELICAN_CLASS': 'pelican.Pelican', } def read_settings(filename):