Mercurial > piecrust2
annotate chef.py @ 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 | f485ba500df3 |
rev | line source |
---|---|
0 | 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.environment import StandardEnvironment | |
7 from piecrust.pathutil import SiteNotFoundError, find_app_root | |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
8 from piecrust.plugins.base import PluginLoader |
0 | 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() | |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
28 self.plugin_loader = PluginLoader(self) |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
29 self.env = StandardEnvironment() |
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
30 self.env.initialize(self) |
0 | 31 |
32 | |
33 def main(): | |
34 root = None | |
35 cache = True | |
36 debug = False | |
37 config_variant = None | |
38 i = 0 | |
39 while i < len(sys.argv): | |
40 arg = sys.argv[i] | |
41 if arg.startswith('--root='): | |
42 root = os.path.expanduser(arg[len('--root='):]) | |
43 elif arg == '--root': | |
44 root = sys.argv[i + 1] | |
45 ++i | |
46 elif arg.startswith('--config='): | |
47 config_variant = arg[len('--config='):] | |
48 elif arg == '--config': | |
49 config_variant = sys.argv[i + 1] | |
50 ++i | |
51 elif arg == '--no-cache': | |
52 cache = False | |
53 elif arg == '--debug': | |
54 debug = True | |
55 | |
56 if arg[0] != '-': | |
57 break | |
58 | |
59 if debug: | |
60 logger.setLevel(logging.DEBUG) | |
61 | |
62 if root is None: | |
63 root = find_app_root() | |
64 | |
65 if not root: | |
66 app = NullPieCrust() | |
67 else: | |
68 app = PieCrust(root, cache=cache) | |
69 | |
70 # Handle a configuration variant. | |
71 if config_variant is not None: | |
72 if not root: | |
73 raise SiteNotFoundError() | |
74 app.config.applyVariant('variants/' + config_variant) | |
75 | |
76 # Setup the arg parser. | |
77 parser = argparse.ArgumentParser( | |
78 description="The PieCrust chef manages your website.") | |
79 parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) | |
80 parser.add_argument('--root', help="The root directory of the website.") | |
81 parser.add_argument('--config', help="The configuration variant to use for this command.") | |
82 parser.add_argument('--debug', help="Show debug information.", action='store_true') | |
83 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') | |
84 parser.add_argument('--quiet', help="Print only important information.", action='store_true') | |
85 parser.add_argument('--log', help="Send log messages to the specified file.") | |
86 | |
87 commands = sorted(app.plugin_loader.getCommands(), | |
88 lambda a, b: cmp(a.name, b.name)) | |
89 subparsers = parser.add_subparsers() | |
90 for c in commands: | |
91 p = subparsers.add_parser(c.name, help=c.description) | |
92 c.setupParser(p) | |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
93 p.set_defaults(func=c._runFromChef) |
0 | 94 |
95 # Parse the command line. | |
96 result = parser.parse_args() | |
97 | |
98 # Setup the logger. | |
99 if result.debug and result.quiet: | |
100 raise Exception("You can't specify both --debug and --quiet.") | |
101 if result.debug: | |
102 logger.setLevel(logging.DEBUG) | |
103 elif result.quiet: | |
104 logger.setLevel(logging.WARNING) | |
105 if result.log: | |
106 from logging.handlers import FileHandler | |
107 logger.addHandler(FileHandler(result.log)) | |
108 | |
109 # Run the command! | |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
110 result.func(app, result) |
0 | 111 |
112 | |
113 if __name__ == '__main__': | |
114 main() | |
115 |