Mercurial > piecrust2
annotate piecrust/main.py @ 369:4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
* We need a distinction between source metadata and route metadata. In most
cases they're the same, but in cases like taxonomy pages, route metadata
contains more things that can't be in source metadata if we want to re-use
cached pages.
* Create a new `QualifiedPage` type which is a page with a specific route
and route metadata. Pass this around in many places.
* Instead of passing an URL around, use the route in the `QualifiedPage` to
generate URLs. This is better since it removes the guess-work from trying
to generate URLs for sub-pages.
* Deep-copy app and page configurations before passing them around to things
that could modify them, like data builders and such.
* Exclude taxonomy pages from iterator data providers.
* Properly nest iterator data providers for when the theme and user page
sources are merged inside `site.pages`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 03 May 2015 18:47:10 -0700 |
parents | d8677ad748f0 |
children | c2ca72fb7f0b |
rev | line source |
---|---|
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
1 import io |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import sys |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import time |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import os.path |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import logging |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 import argparse |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 import colorama |
69
cb1ed436642c
Always use version generated by `setup.py`. Better version generation.
Ludovic Chabant <ludovic@chabant.com>
parents:
61
diff
changeset
|
8 from piecrust import APP_VERSION |
cb1ed436642c
Always use version generated by `setup.py`. Better version generation.
Ludovic Chabant <ludovic@chabant.com>
parents:
61
diff
changeset
|
9 from piecrust.app import PieCrust, PieCrustConfiguration |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
10 from piecrust.chefutil import ( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
11 format_timed, log_friendly_exception, print_help_item) |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
12 from piecrust.commands.base import CommandContext |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 from piecrust.pathutil import SiteNotFoundError, find_app_root |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 from piecrust.plugins.base import PluginLoader |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 class ColoredFormatter(logging.Formatter): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 COLORS = { |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 'DEBUG': colorama.Fore.BLACK + colorama.Style.BRIGHT, |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 'INFO': '', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 'WARNING': colorama.Fore.YELLOW, |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 'ERROR': colorama.Fore.RED, |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 'CRITICAL': colorama.Back.RED + colorama.Fore.WHITE |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 } |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 def __init__(self, fmt=None, datefmt=None): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 super(ColoredFormatter, self).__init__(fmt, datefmt) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 def format(self, record): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 color = self.COLORS.get(record.levelname) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 res = super(ColoredFormatter, self).format(record) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 if color: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 res = color + res + colorama.Style.RESET_ALL |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 return res |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 class NullPieCrust: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 self.root_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 self.debug = False |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 self.templates_dirs = [] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 self.theme_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 self.cache_dir = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 self.config = PieCrustConfiguration() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 self.plugin_loader = PluginLoader(self) |
103
028df35a690e
Fix using `chef` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents:
99
diff
changeset
|
49 self.env = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 def main(): |
167
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
53 if sys.platform == 'darwin': |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
54 # There's a bug on MacOSX that can cause Python to be confused |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
55 # about the locale. Let's try to fix that. |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
56 # See: http://bugs.python.org/issue18378 |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
57 import locale |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
58 try: |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
59 locale.getdefaultlocale() |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
60 except ValueError: |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
61 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') |
0a1736ef740d
chef: Work around a bug in MacOSX where the default locale doesn't work.
Ludovic Chabant <ludovic@chabant.com>
parents:
103
diff
changeset
|
62 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
63 argv = sys.argv |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
64 pre_args = _pre_parse_chef_args(argv) |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
65 try: |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
66 exit_code = _run_chef(pre_args) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
67 except Exception as ex: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
68 if pre_args.debug: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
69 logger.exception(ex) |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
70 else: |
39
2f717f961996
Better error reporting and cache validation.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
71 log_friendly_exception(logger, ex) |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
72 exit_code = 1 |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
73 sys.exit(exit_code) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
74 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
76 class PreParsedChefArgs(object): |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
77 def __init__(self, root=None, cache=True, debug=False, quiet=False, |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
78 log_file=None, log_debug=False, config_variant=None): |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
79 self.root = root |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
80 self.cache = cache |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
81 self.debug = debug |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
82 self.quiet = quiet |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
83 self.log_file = log_file |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
84 self.log_debug = log_debug |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
85 self.config_variant = config_variant |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
86 self.config_values = [] |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
87 |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
88 |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
89 def _parse_config_value(arg): |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
90 try: |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
91 name, value = arg.split('=') |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
92 except Exception: |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
93 raise Exception("Invalid configuration name and value: %s" % arg) |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
94 return (name, value) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
95 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
96 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
97 def _pre_parse_chef_args(argv): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 # We need to parse some arguments before we can build the actual argument |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 # parser, because it can affect which plugins will be loaded. Also, log- |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 # related arguments must be parsed first because we want to log everything |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 # from the beginning. |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
102 res = PreParsedChefArgs() |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 i = 1 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
104 while i < len(argv): |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
105 arg = argv[i] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 if arg.startswith('--root='): |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
107 res.root = os.path.expanduser(arg[len('--root='):]) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 elif arg == '--root': |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
109 res.root = os.path.expanduser(argv[i + 1]) |
342
d8677ad748f0
chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents:
341
diff
changeset
|
110 i += 1 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 elif arg.startswith('--config='): |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
112 res.config_variant = arg[len('--config='):] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 elif arg == '--config': |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
114 res.config_variant = argv[i + 1] |
342
d8677ad748f0
chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents:
341
diff
changeset
|
115 i += 1 |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
116 elif arg.startswith('--config-set='): |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
117 res.config_values.append( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
118 _parse_config_value(arg[len('--config-set='):])) |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
119 elif arg == '--config-set': |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
120 res.config_values.append(_parse_config_value(argv[i + 1])) |
342
d8677ad748f0
chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents:
341
diff
changeset
|
121 i += 1 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 elif arg == '--log': |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
123 res.log_file = argv[i + 1] |
342
d8677ad748f0
chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents:
341
diff
changeset
|
124 i += 1 |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
125 elif arg == '--log-debug': |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
126 res.log_debug = True |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 elif arg == '--no-cache': |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
128 res.cache = False |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 elif arg == '--debug': |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
130 res.debug = True |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 elif arg == '--quiet': |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
132 res.quiet = True |
342
d8677ad748f0
chef: Fix pre-parsing.
Ludovic Chabant <ludovic@chabant.com>
parents:
341
diff
changeset
|
133 else: |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 break |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 i = i + 1 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 # Setup the logger. |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
139 if res.debug and res.quiet: |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 raise Exception("You can't specify both --debug and --quiet.") |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 colorama.init() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 root_logger = logging.getLogger() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 root_logger.setLevel(logging.INFO) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
145 if res.debug or res.log_debug: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
146 root_logger.setLevel(logging.DEBUG) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
147 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 log_handler = logging.StreamHandler(sys.stdout) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
149 if res.debug: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
150 log_handler.setLevel(logging.DEBUG) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 log_handler.setFormatter(ColoredFormatter("[%(name)s] %(message)s")) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
152 else: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
153 if res.quiet: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
154 log_handler.setLevel(logging.WARNING) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
155 else: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
156 log_handler.setLevel(logging.INFO) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 log_handler.setFormatter(ColoredFormatter("%(message)s")) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 root_logger.addHandler(log_handler) |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
159 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
160 if res.log_file: |
97
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
161 file_handler = logging.FileHandler(res.log_file, mode='w') |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
162 root_logger.addHandler(file_handler) |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
163 if res.log_debug: |
00a9b24ca944
Add `--log-debug` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
69
diff
changeset
|
164 file_handler.setLevel(logging.DEBUG) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
165 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
166 return res |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
167 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
169 def _run_chef(pre_args): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 # Setup the app. |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
171 start_time = time.clock() |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
172 root = pre_args.root |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 if root is None: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
174 try: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
175 root = find_app_root() |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
176 except SiteNotFoundError: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
177 root = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
179 if not root: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 app = NullPieCrust() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 else: |
44
a62452ab5080
Correctly set the `debug` flag on the app.
Ludovic Chabant <ludovic@chabant.com>
parents:
39
diff
changeset
|
182 app = PieCrust(root, cache=pre_args.cache, debug=pre_args.debug) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
183 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
184 # Handle a configuration variant. |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
185 if pre_args.config_variant is not None: |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
186 if not root: |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
187 raise SiteNotFoundError("Can't apply any variant.") |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
188 app.config.applyVariant('variants/' + pre_args.config_variant) |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
189 for name, value in pre_args.config_values: |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
190 logger.debug("Setting configuration '%s' to: %s" % (name, value)) |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
191 app.config.set(name, value) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
192 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
193 # Setup the arg parser. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
194 parser = argparse.ArgumentParser( |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
195 prog='chef', |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
196 description="The PieCrust chef manages your website.", |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
197 formatter_class=argparse.RawDescriptionHelpFormatter) |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
198 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
199 '--version', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
200 action='version', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
201 version=('%(prog)s ' + APP_VERSION)) |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
202 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
203 '--root', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
204 help="The root directory of the website.") |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
205 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
206 '--config', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
207 help="The configuration variant to use for this command.") |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
208 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
209 '--config-set', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
210 help="Sets a specific site configuration setting.") |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
211 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
212 '--debug', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
213 help="Show debug information.", action='store_true') |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
214 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
215 '--no-cache', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
216 help="When applicable, disable caching.", |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
217 action='store_true') |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
218 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
219 '--quiet', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
220 help="Print only important information.", |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
221 action='store_true') |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
222 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
223 '--log', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
224 help="Send log messages to the specified file.") |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
225 parser.add_argument( |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
226 '--log-debug', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
227 help="Log debug messages to the log file.", |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
228 action='store_true') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
229 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
230 commands = sorted(app.plugin_loader.getCommands(), |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
231 key=lambda c: c.name) |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
232 subparsers = parser.add_subparsers(title='list of commands') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
233 for c in commands: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 p = subparsers.add_parser(c.name, help=c.description) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
235 c.setupParser(p, app) |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
236 p.set_defaults(func=c.checkedRun) |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
237 |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
238 help_cmd = next(filter(lambda c: c.name == 'help', commands), None) |
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
239 if help_cmd and help_cmd.has_topics: |
99
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
240 with io.StringIO() as epilog: |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
241 epilog.write("additional help topics:\n") |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
242 for name, desc in help_cmd.getTopics(): |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
243 print_help_item(epilog, name, desc) |
8703be118430
Changes to `help` command and extendable commands:
Ludovic Chabant <ludovic@chabant.com>
parents:
98
diff
changeset
|
244 parser.epilog = epilog.getvalue() |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
245 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
246 # Parse the command line. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
247 result = parser.parse_args() |
341
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
248 logger.debug(format_timed(start_time, 'initialized PieCrust', |
bd726306d4dc
chef: Add a `--config-set` option to set ad-hoc site configuration settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
307
diff
changeset
|
249 colored=False)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
250 |
61
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
251 # Print the help if no command was specified. |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
252 if not hasattr(result, 'func'): |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
253 parser.print_help() |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
254 return 0 |
64f37c4cce68
Print the help by default when running `chef` with no command.
Ludovic Chabant <ludovic@chabant.com>
parents:
57
diff
changeset
|
255 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
256 # Run the command! |
57
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
257 ctx = CommandContext(app, parser, result) |
c8c522dacfea
Add `help` function, cleanup argument handling.
Ludovic Chabant <ludovic@chabant.com>
parents:
44
diff
changeset
|
258 exit_code = result.func(ctx) |
98
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
259 if exit_code is None: |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
260 return 0 |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
261 if not isinstance(exit_code, int): |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
262 logger.error("Got non-integer exit code: %s" % exit_code) |
5959a117a943
Exit with the proper code.
Ludovic Chabant <ludovic@chabant.com>
parents:
97
diff
changeset
|
263 return -1 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
264 return exit_code |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
265 |