comparison chef.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
comparison
equal deleted inserted replaced
2:40fa08b261b9 3:f485ba500df3
1 import sys 1 from piecrust.main import main
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
8 from piecrust.plugins.base import PluginLoader
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.plugin_loader = PluginLoader(self)
29 self.env = StandardEnvironment()
30 self.env.initialize(self)
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)
93 p.set_defaults(func=c._runFromChef)
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!
110 result.func(app, result)
111 2
112 3
113 if __name__ == '__main__': 4 if __name__ == '__main__':
114 main() 5 main()
115 6