Mercurial > piecrust2
diff piecrust/commands/builtin/info.py @ 3:f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
- Serving works, with debug window.
- Baking works, multi-threading, with dependency handling.
- Various things not implemented yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 Aug 2014 23:43:16 -0700 |
parents | aaa8fb7c8918 |
children | 474c9882decf |
line wrap: on
line diff
--- a/piecrust/commands/builtin/info.py Wed Dec 25 22:16:46 2013 -0800 +++ b/piecrust/commands/builtin/info.py Sun Aug 10 23:43:16 2014 -0700 @@ -1,4 +1,6 @@ +import os.path import logging +import fnmatch from piecrust.commands.base import ChefCommand @@ -11,9 +13,112 @@ self.name = 'root' self.description = "Gets the root directory of the current website." - def setupParser(self, parser): + def setupParser(self, parser, app): + pass + + def run(self, ctx): + logger.info(ctx.app.root_dir) + + +class ShowConfigCommand(ChefCommand): + def __init__(self): + super(ShowConfigCommand, self).__init__() + self.name = 'showconfig' + self.description = "Prints part of, or the entirety of, the website's configuration." + + def setupParser(self, parser, app): + parser.add_argument('path', + help="The path to a config section or value", + nargs='?') + + def run(self, ctx): + show = ctx.app.config.get(ctx.args.path) + if show is not None: + if isinstance(show, (dict, list)): + import yaml + out = yaml.safe_dump(show, default_flow_style=False) + logger.info(out) + else: + logger.info(show) + elif ctx.args.path: + logger.error("No such configuration path: %s" % ctx.args.path) + ctx.result = 1 + + +class ShowRoutesCommand(ChefCommand): + def __init__(self): + super(ShowRoutesCommand, self).__init__() + self.name = 'routes' + self.description = "Shows the routes defined for this website." + + def setupParser(self, parser, app): pass def run(self, ctx): - logger.info(ctx.app.root) + for route in ctx.app.routes: + logger.info("%s:" % route.uri_pattern) + logger.info(" source: %s" % route.source_name) + logger.info(" taxonomy: %s" % (route.taxonomy or '')) + + +class ShowPathsCommand(ChefCommand): + def __init__(self): + super(ShowPathsCommand, self).__init__() + self.name = 'paths' + self.description = "Shows the paths that this website is using." + + def setupParser(self, parser, app): + pass + + def run(self, ctx): + app = ctx.app + paths = ['theme_dir', 'templates_dirs', 'plugins_dirs', 'cache_dir'] + for p in paths: + value = getattr(app, p) + if value is list: + logging.info("%s: %s" % (p, ', '.join(value))) + else: + logging.info("%s: %s" % (p, value)) + + +class FindCommand(ChefCommand): + def __init__(self): + super(FindCommand, self).__init__() + self.name = 'find' + self.description = "Find pages in the website." + def setupParser(self, parser, app): + parser.add_argument('pattern', + help="The pattern to match with page slugs", + nargs='?') + parser.add_argument('--endpoint', + help="The endpoint(s) to look into", + nargs='+') + parser.add_argument('--full-path', + help="Return full paths instead of root-relative paths", + action='store_true') + parser.add_argument('--metadata', + help="Return metadata about the page instead of just the path", + action='store_true') + + def run(self, ctx): + pattern = ctx.args.pattern + sources = list(ctx.app.sources) + if ctx.args.endpoint: + endpoints = ctx.args.endpoint + sources = filter(lambda s: s.endpoint in endpoints, sources) + for src in sources: + page_facs = src.getPageFactories() + for pf in page_facs: + name = os.path.relpath(pf.path, ctx.app.root_dir) + if pattern is None or fnmatch.fnmatch(name, pattern): + if ctx.args.full_path: + name = pf.path + if ctx.args.metadata: + logger.info("path:%s" % pf.path) + for key, val in pf.metadata.iteritems(): + logger.info("%s:%s" % (key, val)) + logger.info("---") + else: + logger.info(name) +