Mercurial > piecrust2
comparison chef.py @ 0:a212a3f2e3ee
Initial commit.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 21 Dec 2013 14:44:02 -0800 |
parents | |
children | aaa8fb7c8918 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a212a3f2e3ee |
---|---|
1 import sys | |
2 import os.path | |
3 import logging | |
4 import argparse | |
5 from piecrust.app import PieCrust, PieCrustConfiguration, APP_VERSION | |
6 from piecrust.commands.base import CommandContext | |
7 from piecrust.environment import StandardEnvironment | |
8 from piecrust.pathutil import SiteNotFoundError, find_app_root | |
9 | |
10 | |
11 logger = logging.getLogger(__name__) | |
12 logging.basicConfig(level=logging.INFO, | |
13 format="%(message)s") | |
14 | |
15 | |
16 class NullPieCrust: | |
17 def __init__(self): | |
18 self.root = None | |
19 self.cache = False | |
20 self.debug = False | |
21 self.templates_dirs = [] | |
22 self.pages_dir = [] | |
23 self.posts_dir = [] | |
24 self.plugins_dirs = [] | |
25 self.theme_dir = None | |
26 self.cache_dir = None | |
27 self.config = PieCrustConfiguration() | |
28 self.env = StandardEnvironment(self) | |
29 | |
30 | |
31 def main(): | |
32 root = None | |
33 cache = True | |
34 debug = False | |
35 config_variant = None | |
36 i = 0 | |
37 while i < len(sys.argv): | |
38 arg = sys.argv[i] | |
39 if arg.startswith('--root='): | |
40 root = os.path.expanduser(arg[len('--root='):]) | |
41 elif arg == '--root': | |
42 root = sys.argv[i + 1] | |
43 ++i | |
44 elif arg.startswith('--config='): | |
45 config_variant = arg[len('--config='):] | |
46 elif arg == '--config': | |
47 config_variant = sys.argv[i + 1] | |
48 ++i | |
49 elif arg == '--no-cache': | |
50 cache = False | |
51 elif arg == '--debug': | |
52 debug = True | |
53 | |
54 if arg[0] != '-': | |
55 break | |
56 | |
57 if debug: | |
58 logger.setLevel(logging.DEBUG) | |
59 | |
60 if root is None: | |
61 root = find_app_root() | |
62 | |
63 if not root: | |
64 app = NullPieCrust() | |
65 else: | |
66 app = PieCrust(root, cache=cache) | |
67 | |
68 # Handle a configuration variant. | |
69 if config_variant is not None: | |
70 if not root: | |
71 raise SiteNotFoundError() | |
72 app.config.applyVariant('variants/' + config_variant) | |
73 | |
74 # Setup the arg parser. | |
75 parser = argparse.ArgumentParser( | |
76 description="The PieCrust chef manages your website.") | |
77 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) | |
78 parser.add_argument('--root', help="The root directory of the website.") | |
79 parser.add_argument('--config', help="The configuration variant to use for this command.") | |
80 parser.add_argument('--debug', help="Show debug information.", action='store_true') | |
81 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') | |
82 parser.add_argument('--quiet', help="Print only important information.", action='store_true') | |
83 parser.add_argument('--log', help="Send log messages to the specified file.") | |
84 | |
85 commands = sorted(app.plugin_loader.getCommands(), | |
86 lambda a, b: cmp(a.name, b.name)) | |
87 subparsers = parser.add_subparsers() | |
88 for c in commands: | |
89 def command_runner(r): | |
90 if root is None and c.requires_website: | |
91 raise SiteNotFoundError() | |
92 c.run(CommandContext(r, app)) | |
93 | |
94 p = subparsers.add_parser(c.name, help=c.description) | |
95 c.setupParser(p) | |
96 p.set_defaults(func=command_runner) | |
97 | |
98 # Parse the command line. | |
99 result = parser.parse_args() | |
100 | |
101 # Setup the logger. | |
102 if result.debug and result.quiet: | |
103 raise Exception("You can't specify both --debug and --quiet.") | |
104 if result.debug: | |
105 logger.setLevel(logging.DEBUG) | |
106 elif result.quiet: | |
107 logger.setLevel(logging.WARNING) | |
108 if result.log: | |
109 from logging.handlers import FileHandler | |
110 logger.addHandler(FileHandler(result.log)) | |
111 | |
112 # Run the command! | |
113 result.func(result) | |
114 | |
115 | |
116 if __name__ == '__main__': | |
117 main() | |
118 |