Base routines were transformed into the class Pelican. This class could be overridden using PELICAN_CLASS option.

This commit is contained in:
Alexander Artemenko 2010-12-25 17:26:24 +03:00
commit a937424faa
2 changed files with 85 additions and 65 deletions

View file

@ -10,71 +10,79 @@ from pelican.generators import (ArticlesGenerator, PagesGenerator,
VERSION = "2.5.3" VERSION = "2.5.3"
def init_params(settings=None, path=None, theme=None, output_path=None, class Pelican(object):
def __init__(self, settings=None, path=None, theme=None, output_path=None,
markup=None, keep=False): markup=None, keep=False):
"""Read the settings, and performs some checks on the environment """Read the settings, and performs some checks on the environment
before doing anything else. before doing anything else.
""" """
if settings is None: self.path = path or settings['PATH']
settings = {} if self.path.endswith('/'):
settings = read_settings(settings) self.path = path[:-1]
path = path or settings['PATH']
if path.endswith('/'):
path = path[:-1]
# define the default settings # define the default settings
theme = theme or settings['THEME'] self.settings = settings
self.theme = theme or settings['THEME']
self.path = path
output_path = output_path or settings['OUTPUT_PATH'] output_path = output_path or settings['OUTPUT_PATH']
output_path = os.path.realpath(output_path) self.output_path = os.path.realpath(output_path)
markup = markup or settings['MARKUP'] self.markup = markup or settings['MARKUP']
keep = keep or settings['KEEP_OUTPUT_DIRECTORY'] self.keep = keep or settings['KEEP_OUTPUT_DIRECTORY']
# find the theme in pelican.theme if the given one does not exists # find the theme in pelican.theme if the given one does not exists
if not os.path.exists(theme): if not os.path.exists(self.theme):
theme_path = os.sep.join([os.path.dirname( theme_path = os.sep.join([os.path.dirname(
os.path.abspath(__file__)), "themes/%s" % theme]) os.path.abspath(__file__)), "themes/%s" % self.theme])
if os.path.exists(theme_path): if os.path.exists(theme_path):
theme = theme_path self.theme = theme_path
else: else:
raise Exception("Impossible to find the theme %s" % theme) raise Exception("Impossible to find the theme %s" % theme)
# get the list of files to parse # get the list of files to parse
if not path: if not self.path:
raise Exception('you need to specify a path to search the docs on !') raise Exception('you need to specify a path to search the docs on !')
return settings, path, theme, output_path, markup, keep
def run(self):
def run_generators(generators, settings, path, theme, output_path, markup, keep):
"""Run the generators and return""" """Run the generators and return"""
context = settings.copy() context = self.settings.copy()
generators = [p(context, settings, path, theme, output_path, markup, keep) generators = [
for p in 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: for p in generators:
if hasattr(p, 'generate_context'): if hasattr(p, 'generate_context'):
p.generate_context() p.generate_context()
# erase the directory if it is not the source # erase the directory if it is not the source
if output_path not in os.path.realpath(path) and not keep: if os.path.realpath(self.path).startswith(self.output_path) and not keep:
clean_output_dir(output_path) clean_output_dir(self.output_path)
writer = Writer(output_path) writer = self.get_writer()
for p in generators: for p in generators:
if hasattr(p, 'generate_output'): if hasattr(p, 'generate_output'):
p.generate_output(writer) p.generate_output(writer)
def run_pelican(settings, path, theme, output_path, markup, delete): def get_generator_classes(self):
"""Run pelican with the given parameters"""
params = init_params(settings, path, theme, output_path, markup, delete)
generators = [ArticlesGenerator, PagesGenerator, StaticGenerator] generators = [ArticlesGenerator, PagesGenerator, StaticGenerator]
if params[0]['PDF_GENERATOR']: # param[0] is settings if self.settings['PDF_GENERATOR']:
generators.append(PdfGenerator) generators.append(PdfGenerator)
run_generators(generators, *params) return generators
def get_writer(self):
return Writer(self.output_path)
def main(): def main():
@ -106,7 +114,18 @@ def main():
# the variable with None. # the variable with None.
markup = [a.strip().lower() for a in args.markup.split(',')] if args.markup else 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__': if __name__ == '__main__':

View file

@ -22,6 +22,7 @@ _DEFAULT_CONFIG = {'PATH': None,
'CLEAN_URLS': False, # use /blah/ instead /blah.html in urls 'CLEAN_URLS': False, # use /blah/ instead /blah.html in urls
'RELATIVE_URLS': True, 'RELATIVE_URLS': True,
'DEFAULT_LANG': 'en', 'DEFAULT_LANG': 'en',
'PELICAN_CLASS': 'pelican.Pelican',
} }
def read_settings(filename): def read_settings(filename):