Mercurial > piecrust2
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 2:40fa08b261b9 | 3:f485ba500df3 |
|---|---|
| 1 import os.path | |
| 1 import logging | 2 import logging |
| 3 import fnmatch | |
| 2 from piecrust.commands.base import ChefCommand | 4 from piecrust.commands.base import ChefCommand |
| 3 | 5 |
| 4 | 6 |
| 5 logger = logging.getLogger(__name__) | 7 logger = logging.getLogger(__name__) |
| 6 | 8 |
| 9 def __init__(self): | 11 def __init__(self): |
| 10 super(RootCommand, self).__init__() | 12 super(RootCommand, self).__init__() |
| 11 self.name = 'root' | 13 self.name = 'root' |
| 12 self.description = "Gets the root directory of the current website." | 14 self.description = "Gets the root directory of the current website." |
| 13 | 15 |
| 14 def setupParser(self, parser): | 16 def setupParser(self, parser, app): |
| 15 pass | 17 pass |
| 16 | 18 |
| 17 def run(self, ctx): | 19 def run(self, ctx): |
| 18 logger.info(ctx.app.root) | 20 logger.info(ctx.app.root_dir) |
| 19 | 21 |
| 22 | |
| 23 class ShowConfigCommand(ChefCommand): | |
| 24 def __init__(self): | |
| 25 super(ShowConfigCommand, self).__init__() | |
| 26 self.name = 'showconfig' | |
| 27 self.description = "Prints part of, or the entirety of, the website's configuration." | |
| 28 | |
| 29 def setupParser(self, parser, app): | |
| 30 parser.add_argument('path', | |
| 31 help="The path to a config section or value", | |
| 32 nargs='?') | |
| 33 | |
| 34 def run(self, ctx): | |
| 35 show = ctx.app.config.get(ctx.args.path) | |
| 36 if show is not None: | |
| 37 if isinstance(show, (dict, list)): | |
| 38 import yaml | |
| 39 out = yaml.safe_dump(show, default_flow_style=False) | |
| 40 logger.info(out) | |
| 41 else: | |
| 42 logger.info(show) | |
| 43 elif ctx.args.path: | |
| 44 logger.error("No such configuration path: %s" % ctx.args.path) | |
| 45 ctx.result = 1 | |
| 46 | |
| 47 | |
| 48 class ShowRoutesCommand(ChefCommand): | |
| 49 def __init__(self): | |
| 50 super(ShowRoutesCommand, self).__init__() | |
| 51 self.name = 'routes' | |
| 52 self.description = "Shows the routes defined for this website." | |
| 53 | |
| 54 def setupParser(self, parser, app): | |
| 55 pass | |
| 56 | |
| 57 def run(self, ctx): | |
| 58 for route in ctx.app.routes: | |
| 59 logger.info("%s:" % route.uri_pattern) | |
| 60 logger.info(" source: %s" % route.source_name) | |
| 61 logger.info(" taxonomy: %s" % (route.taxonomy or '')) | |
| 62 | |
| 63 | |
| 64 class ShowPathsCommand(ChefCommand): | |
| 65 def __init__(self): | |
| 66 super(ShowPathsCommand, self).__init__() | |
| 67 self.name = 'paths' | |
| 68 self.description = "Shows the paths that this website is using." | |
| 69 | |
| 70 def setupParser(self, parser, app): | |
| 71 pass | |
| 72 | |
| 73 def run(self, ctx): | |
| 74 app = ctx.app | |
| 75 paths = ['theme_dir', 'templates_dirs', 'plugins_dirs', 'cache_dir'] | |
| 76 for p in paths: | |
| 77 value = getattr(app, p) | |
| 78 if value is list: | |
| 79 logging.info("%s: %s" % (p, ', '.join(value))) | |
| 80 else: | |
| 81 logging.info("%s: %s" % (p, value)) | |
| 82 | |
| 83 | |
| 84 class FindCommand(ChefCommand): | |
| 85 def __init__(self): | |
| 86 super(FindCommand, self).__init__() | |
| 87 self.name = 'find' | |
| 88 self.description = "Find pages in the website." | |
| 89 | |
| 90 def setupParser(self, parser, app): | |
| 91 parser.add_argument('pattern', | |
| 92 help="The pattern to match with page slugs", | |
| 93 nargs='?') | |
| 94 parser.add_argument('--endpoint', | |
| 95 help="The endpoint(s) to look into", | |
| 96 nargs='+') | |
| 97 parser.add_argument('--full-path', | |
| 98 help="Return full paths instead of root-relative paths", | |
| 99 action='store_true') | |
| 100 parser.add_argument('--metadata', | |
| 101 help="Return metadata about the page instead of just the path", | |
| 102 action='store_true') | |
| 103 | |
| 104 def run(self, ctx): | |
| 105 pattern = ctx.args.pattern | |
| 106 sources = list(ctx.app.sources) | |
| 107 if ctx.args.endpoint: | |
| 108 endpoints = ctx.args.endpoint | |
| 109 sources = filter(lambda s: s.endpoint in endpoints, sources) | |
| 110 for src in sources: | |
| 111 page_facs = src.getPageFactories() | |
| 112 for pf in page_facs: | |
| 113 name = os.path.relpath(pf.path, ctx.app.root_dir) | |
| 114 if pattern is None or fnmatch.fnmatch(name, pattern): | |
| 115 if ctx.args.full_path: | |
| 116 name = pf.path | |
| 117 if ctx.args.metadata: | |
| 118 logger.info("path:%s" % pf.path) | |
| 119 for key, val in pf.metadata.iteritems(): | |
| 120 logger.info("%s:%s" % (key, val)) | |
| 121 logger.info("---") | |
| 122 else: | |
| 123 logger.info(name) | |
| 124 |
