Mercurial > piecrust2
changeset 1:aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Added `init` command.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 22 Dec 2013 08:00:24 -0800 |
parents | a212a3f2e3ee |
children | 40fa08b261b9 |
files | chef.py piecrust/app.py piecrust/commands/base.py piecrust/commands/builtin/__init__.py piecrust/commands/builtin/info.py piecrust/commands/builtin/util.py piecrust/environment.py piecrust/pathutil.py piecrust/plugins.py piecrust/plugins/__init__.py piecrust/plugins/base.py piecrust/plugins/builtin.py |
diffstat | 10 files changed, 213 insertions(+), 149 deletions(-) [+] |
line wrap: on
line diff
--- a/chef.py Sat Dec 21 14:44:02 2013 -0800 +++ b/chef.py Sun Dec 22 08:00:24 2013 -0800 @@ -3,9 +3,9 @@ import logging import argparse from piecrust.app import PieCrust, PieCrustConfiguration, APP_VERSION -from piecrust.commands.base import CommandContext from piecrust.environment import StandardEnvironment from piecrust.pathutil import SiteNotFoundError, find_app_root +from piecrust.plugins.base import PluginLoader logger = logging.getLogger(__name__) @@ -25,7 +25,9 @@ self.theme_dir = None self.cache_dir = None self.config = PieCrustConfiguration() - self.env = StandardEnvironment(self) + self.plugin_loader = PluginLoader(self) + self.env = StandardEnvironment() + self.env.initialize(self) def main(): @@ -86,14 +88,9 @@ lambda a, b: cmp(a.name, b.name)) subparsers = parser.add_subparsers() for c in commands: - def command_runner(r): - if root is None and c.requires_website: - raise SiteNotFoundError() - c.run(CommandContext(r, app)) - p = subparsers.add_parser(c.name, help=c.description) c.setupParser(p) - p.set_defaults(func=command_runner) + p.set_defaults(func=c._runFromChef) # Parse the command line. result = parser.parse_args() @@ -110,7 +107,7 @@ logger.addHandler(FileHandler(result.log)) # Run the command! - result.func(result) + result.func(app, result) if __name__ == '__main__':
--- a/piecrust/app.py Sat Dec 21 14:44:02 2013 -0800 +++ b/piecrust/app.py Sun Dec 22 08:00:24 2013 -0800 @@ -7,7 +7,7 @@ import yaml from cache import SimpleCache from decorators import lazy_property -from plugins import PluginLoader +from plugins.base import PluginLoader from environment import StandardEnvironment from configuration import Configuration, merge_dicts
--- a/piecrust/commands/base.py Sat Dec 21 14:44:02 2013 -0800 +++ b/piecrust/commands/base.py Sun Dec 22 08:00:24 2013 -0800 @@ -1,13 +1,14 @@ import logging +from piecrust.pathutil import SiteNotFoundError logger = logging.getLogger(__name__) class CommandContext(object): - def __init__(self, args, app): + def __init__(self, app, args): + self.app = app self.args = args - self.app = app class ChefCommand(object): @@ -22,16 +23,8 @@ def run(self, ctx): raise NotImplementedError() - -class RootCommand(ChefCommand): - def __init__(self): - super(RootCommand, self).__init__() - self.name = 'root' - self.description = "Gets the root directory of the current website." + def _runFromChef(self, app, res): + if app.root is None and self.requires_website: + raise SiteNotFoundError() + self.run(CommandContext(app, res)) - def setupParser(self, parser): - pass - - def run(self, ctx): - logger.info(ctx.app.root) -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/commands/builtin/info.py Sun Dec 22 08:00:24 2013 -0800 @@ -0,0 +1,19 @@ +import logging +from piecrust.commands.base import ChefCommand + + +logger = logging.getLogger(__name__) + + +class RootCommand(ChefCommand): + def __init__(self): + super(RootCommand, self).__init__() + self.name = 'root' + self.description = "Gets the root directory of the current website." + + def setupParser(self, parser): + pass + + def run(self, ctx): + logger.info(ctx.app.root) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/commands/builtin/util.py Sun Dec 22 08:00:24 2013 -0800 @@ -0,0 +1,49 @@ +import os +import os.path +import codecs +import logging +import yaml +from piecrust.app import CONFIG_PATH +from piecrust.commands.base import ChefCommand + + +logger = logging.getLogger(__name__) + + +class InitCommand(ChefCommand): + def __init__(self): + super(InitCommand, self).__init__() + self.name = 'init' + self.description = "Creates a new empty PieCrust website." + self.requires_website = False + + def setupParser(self, parser): + parser.add_argument('destination', + help="The destination directory in which to create the website.") + + def run(self, ctx): + destination = ctx.args.destination + if destination is None: + destination = os.getcwd() + + if not os.path.isdir(destination): + os.makedirs(destination, 0755) + + config_path = os.path.join(destination, CONFIG_PATH) + if not os.path.isdir(os.path.dirname(config_path)): + os.makedirs(os.path.dirname(config_path)) + + config_text = yaml.dump({ + 'site': { + 'title': "My New Website", + 'description': "A website recently generated with PieCrust", + 'pretty_urls': True + }, + 'smartypants': { + 'enable': True + } + }, + default_flow_style=False) + with codecs.open(config_path, 'w', 'utf-8') as fp: + fp.write(config_text) +
--- a/piecrust/environment.py Sat Dec 21 14:44:02 2013 -0800 +++ b/piecrust/environment.py Sun Dec 22 08:00:24 2013 -0800 @@ -48,5 +48,6 @@ class StandardEnvironment(Environment): - pass + def __init__(self): + super(StandardEnvironment, self).__init__()
--- a/piecrust/pathutil.py Sat Dec 21 14:44:02 2013 -0800 +++ b/piecrust/pathutil.py Sun Dec 22 08:00:24 2013 -0800 @@ -17,7 +17,7 @@ while not os.path.isfile(os.path.join(cwd, '_content', 'config.yml')): cwd = os.path.dirname(cwd) - if not cwd: + if not cwd or cwd == '/': return None return cwd
--- a/piecrust/plugins.py Sat Dec 21 14:44:02 2013 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,123 +0,0 @@ -import os -from piecrust.commands.base import (RootCommand) - - -class PieCrustPlugin(object): - def getFormatters(self): - return [] - - def getTemplateEngines(self): - return [] - - def getDataProviders(self): - return [] - - def getFileSystems(self): - return [] - - def getProcessors(self): - return [] - - def getImporters(self): - return [] - - def getCommands(self): - return [] - - def getRepositories(self): - return [] - - def getBakerAssistants(self): - return [] - - def initialize(self, app): - pass - - -class BuiltInPlugin(PieCrustPlugin): - def __init__(self): - super(BuiltInPlugin, self).__init__() - self.name = '__builtin__' - - def getCommands(self): - return [ - RootCommand()] - - -class PluginLoader(object): - def __init__(self, app): - self.app = app - self._plugins = None - self._pluginsMeta = None - self._componentCache = {} - - @property - def plugins(self): - self._ensureLoaded() - return self._plugins - - def getFormatters(self): - return self._getPluginComponents('getFormatters', True, - order_key=lambda f: f.priority) - - def getTemplateEngines(self): - return self._getPluginComponents('getTemplateEngines', True) - - def getDataProviders(self): - return self._getPluginComponents('getDataProviders') - - def getFileSystems(self): - return self._getPluginComponents('getFileSystems') - - def getProcessors(self): - return self._getPluginComponents('getProcessors', True, - order_key=lambda p: p.priority) - - def getImporters(self): - return self._getPluginComponents('getImporters') - - def getCommands(self): - return self._getPluginComponents('getCommands') - - def getRepositories(self): - return self._getPluginComponents('getRepositories', True) - - def getBakerAssistants(self): - return self._getPluginComponents('getBakerAssistants') - - def _ensureLoaded(self): - if self._plugins is not None: - return - - self._plugins = [BuiltInPlugin()] - self._pluginsMeta = {self._plugins[0].name: False} - - for d in self.app.plugins_dirs: - _, dirs, __ = next(os.walk(d)) - for dd in dirs: - self._loadPlugin(os.path.join(d, dd)) - - for plugin in self._plugins: - plugin.initialize(self.app) - - def _loadPlugin(self, plugin_dir): - pass - - def _getPluginComponents(self, name, initialize=False, order_cmp=None, order_key=None): - if name in self._componentCache: - return self._componentCache[name] - - all_components = [] - for plugin in self.plugins: - plugin_components = getattr(plugin, name)() - all_components += plugin_components - if initialize: - for comp in plugin_components: - comp.initialize(self.app) - - if order_cmp is not None or order_key is not None: - all_components.sort(cmp=order_cmp, key=order_key) - - self._componentCache[name] = all_components - return all_components -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/plugins/base.py Sun Dec 22 08:00:24 2013 -0800 @@ -0,0 +1,113 @@ +import os + + +class PieCrustPlugin(object): + def getFormatters(self): + return [] + + def getTemplateEngines(self): + return [] + + def getDataProviders(self): + return [] + + def getFileSystems(self): + return [] + + def getProcessors(self): + return [] + + def getImporters(self): + return [] + + def getCommands(self): + return [] + + def getRepositories(self): + return [] + + def getBakerAssistants(self): + return [] + + def initialize(self, app): + pass + + +class PluginLoader(object): + def __init__(self, app): + self.app = app + self._plugins = None + self._pluginsMeta = None + self._componentCache = {} + + @property + def plugins(self): + self._ensureLoaded() + return self._plugins + + def getFormatters(self): + return self._getPluginComponents('getFormatters', True, + order_key=lambda f: f.priority) + + def getTemplateEngines(self): + return self._getPluginComponents('getTemplateEngines', True) + + def getDataProviders(self): + return self._getPluginComponents('getDataProviders') + + def getFileSystems(self): + return self._getPluginComponents('getFileSystems') + + def getProcessors(self): + return self._getPluginComponents('getProcessors', True, + order_key=lambda p: p.priority) + + def getImporters(self): + return self._getPluginComponents('getImporters') + + def getCommands(self): + return self._getPluginComponents('getCommands') + + def getRepositories(self): + return self._getPluginComponents('getRepositories', True) + + def getBakerAssistants(self): + return self._getPluginComponents('getBakerAssistants') + + def _ensureLoaded(self): + if self._plugins is not None: + return + + from piecrust.plugins.builtin import BuiltInPlugin + self._plugins = [BuiltInPlugin()] + self._pluginsMeta = {self._plugins[0].name: False} + + for d in self.app.plugins_dirs: + _, dirs, __ = next(os.walk(d)) + for dd in dirs: + self._loadPlugin(os.path.join(d, dd)) + + for plugin in self._plugins: + plugin.initialize(self.app) + + def _loadPlugin(self, plugin_dir): + pass + + def _getPluginComponents(self, name, initialize=False, order_cmp=None, order_key=None): + if name in self._componentCache: + return self._componentCache[name] + + all_components = [] + for plugin in self.plugins: + plugin_components = getattr(plugin, name)() + all_components += plugin_components + if initialize: + for comp in plugin_components: + comp.initialize(self.app) + + if order_cmp is not None or order_key is not None: + all_components.sort(cmp=order_cmp, key=order_key) + + self._componentCache[name] = all_components + return all_components +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/plugins/builtin.py Sun Dec 22 08:00:24 2013 -0800 @@ -0,0 +1,15 @@ +from piecrust.commands.builtin.info import RootCommand +from piecrust.commands.builtin.util import InitCommand +from piecrust.plugins.base import PieCrustPlugin + + +class BuiltInPlugin(PieCrustPlugin): + def __init__(self): + super(BuiltInPlugin, self).__init__() + self.name = '__builtin__' + + def getCommands(self): + return [ + InitCommand(), + RootCommand()] +