Mercurial > piecrust2
comparison piecrust/commands/builtin/themes.py @ 879:58ae026b4c31
chef: Optimize startup time.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 15 Jun 2017 22:38:05 -0700 |
parents | 549e21789ad9 |
children | f13d618cfec6 |
comparison
equal
deleted
inserted
replaced
878:313db67cfc35 | 879:58ae026b4c31 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import shutil | |
4 import logging | 3 import logging |
5 import yaml | 4 from piecrust import THEME_DIR, THEME_CONFIG_PATH, THEME_INFO_PATH |
6 from piecrust import ( | |
7 RESOURCES_DIR, THEME_DIR, THEME_CONFIG_PATH, THEME_INFO_PATH) | |
8 from piecrust.commands.base import ChefCommand | 5 from piecrust.commands.base import ChefCommand |
9 from piecrust.pathutil import SiteNotFoundError | |
10 | 6 |
11 | 7 |
12 logger = logging.getLogger(__name__) | 8 logger = logging.getLogger(__name__) |
13 | 9 |
14 | 10 |
19 self.description = "Manage the themes for the current website." | 15 self.description = "Manage the themes for the current website." |
20 | 16 |
21 def setupParser(self, parser, app): | 17 def setupParser(self, parser, app): |
22 subparsers = parser.add_subparsers() | 18 subparsers = parser.add_subparsers() |
23 p = subparsers.add_parser( | 19 p = subparsers.add_parser( |
24 'info', | 20 'info', |
25 help="Provides information about the current theme.") | 21 help="Provides information about the current theme.") |
26 p.set_defaults(sub_func=self._info) | 22 p.set_defaults(sub_func=self._info) |
27 | 23 |
28 p = subparsers.add_parser( | 24 p = subparsers.add_parser( |
29 'override', | 25 'override', |
30 help="Copies the current theme to the website for " | 26 help="Copies the current theme to the website for " |
31 "customization.") | 27 "customization.") |
32 p.set_defaults(sub_func=self._overrideTheme) | 28 p.set_defaults(sub_func=self._overrideTheme) |
33 | 29 |
34 p = subparsers.add_parser( | 30 p = subparsers.add_parser( |
35 'link', | 31 'link', |
36 help="Makes a given theme the active one for the current " | 32 help="Makes a given theme the active one for the current " |
37 "website by creating a symbolic link to it from the " | 33 "website by creating a symbolic link to it from the " |
38 "'theme' directory.") | 34 "'theme' directory.") |
39 p.add_argument( | 35 p.add_argument( |
40 'theme_dir', | 36 'theme_dir', |
41 help="The directory of the theme to link.") | 37 help="The directory of the theme to link.") |
42 p.set_defaults(sub_func=self._linkTheme) | 38 p.set_defaults(sub_func=self._linkTheme) |
43 | 39 |
44 p = subparsers.add_parser( | 40 p = subparsers.add_parser( |
45 'unlink', | 41 'unlink', |
46 help="Removes the currently active theme for the website. " | 42 help="Removes the currently active theme for the website. " |
47 "This removes the symbolic link to the theme, if any, or " | 43 "This removes the symbolic link to the theme, if any, or " |
48 "deletes the theme folder if it was copied locally.") | 44 "deletes the theme folder if it was copied locally.") |
49 p.set_defaults(sub_func=self._unlinkTheme) | 45 p.set_defaults(sub_func=self._unlinkTheme) |
50 | 46 |
51 def checkedRun(self, ctx): | 47 def checkedRun(self, ctx): |
48 from piecrust.pathutil import SiteNotFoundError | |
49 | |
52 if ctx.app.root_dir is None: | 50 if ctx.app.root_dir is None: |
53 raise SiteNotFoundError(theme=ctx.app.theme_site) | 51 raise SiteNotFoundError(theme=ctx.app.theme_site) |
54 | 52 |
55 if not hasattr(ctx.args, 'sub_func'): | 53 if not hasattr(ctx.args, 'sub_func'): |
56 ctx.parser.parse_args(['themes', '--help']) | 54 ctx.parser.parse_args(['themes', '--help']) |
57 return | 55 return |
58 ctx.args.sub_func(ctx) | 56 ctx.args.sub_func(ctx) |
59 | 57 |
60 def _info(self, ctx): | 58 def _info(self, ctx): |
59 import yaml | |
60 | |
61 theme_dir = ctx.app.theme_dir | 61 theme_dir = ctx.app.theme_dir |
62 if not os.path.exists(theme_dir): | 62 if not os.path.exists(theme_dir): |
63 logger.info("Using default theme, from: %s" % ctx.app.theme_dir) | 63 logger.info("Using default theme, from: %s" % ctx.app.theme_dir) |
64 elif theme_dir.startswith(ctx.app.root_dir): | 64 elif theme_dir.startswith(ctx.app.root_dir): |
65 if os.path.islink(theme_dir): | 65 if os.path.islink(theme_dir): |
82 logger.info("Theme info:") | 82 logger.info("Theme info:") |
83 for k, v in info.items(): | 83 for k, v in info.items(): |
84 logger.info(" - %s: %s" % (str(k), str(v))) | 84 logger.info(" - %s: %s" % (str(k), str(v))) |
85 | 85 |
86 def _overrideTheme(self, ctx): | 86 def _overrideTheme(self, ctx): |
87 import shutil | |
88 | |
87 theme_dir = ctx.app.theme_dir | 89 theme_dir = ctx.app.theme_dir |
88 if not theme_dir: | 90 if not theme_dir: |
89 logger.error("There is no theme currently applied.") | 91 logger.error("There is no theme currently applied.") |
90 return 1 | 92 return 1 |
91 | 93 |
124 if not os.path.isdir(ctx.args.theme_dir): | 126 if not os.path.isdir(ctx.args.theme_dir): |
125 logger.error("Invalid theme directory: %s" % ctx.args.theme_dir) | 127 logger.error("Invalid theme directory: %s" % ctx.args.theme_dir) |
126 return 1 | 128 return 1 |
127 | 129 |
128 msg = ("A theme already exists, and will be deleted. " | 130 msg = ("A theme already exists, and will be deleted. " |
129 "Are you sure? [Y/n]") | 131 "Are you sure? [Y/n]") |
130 self._doUnlinkTheme(ctx.app.root_dir, msg) | 132 self._doUnlinkTheme(ctx.app.root_dir, msg) |
131 | 133 |
132 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) | 134 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) |
133 try: | 135 try: |
134 os.symlink(ctx.args.theme_dir, theme_dir) | 136 os.symlink(ctx.args.theme_dir, theme_dir) |
137 logger.error("Couldn't symlink the theme: %s" % ex) | 139 logger.error("Couldn't symlink the theme: %s" % ex) |
138 return 1 | 140 return 1 |
139 | 141 |
140 def _unlinkTheme(self, ctx): | 142 def _unlinkTheme(self, ctx): |
141 msg = ("The active theme is local. Are you sure you want " | 143 msg = ("The active theme is local. Are you sure you want " |
142 "to delete the theme directory? [Y/n]") | 144 "to delete the theme directory? [Y/n]") |
143 self._doUnlinkTheme(ctx.app.root_dir, msg) | 145 self._doUnlinkTheme(ctx.app.root_dir, msg) |
144 | 146 |
145 def _doUnlinkTheme(self, root_dir, delete_message): | 147 def _doUnlinkTheme(self, root_dir, delete_message): |
148 import shutil | |
149 | |
146 theme_dir = os.path.join(root_dir, THEME_DIR) | 150 theme_dir = os.path.join(root_dir, THEME_DIR) |
147 | 151 |
148 if os.path.islink(theme_dir): | 152 if os.path.islink(theme_dir): |
149 logger.debug("Unlinking: %s" % theme_dir) | 153 logger.debug("Unlinking: %s" % theme_dir) |
150 os.unlink(theme_dir) | 154 os.unlink(theme_dir) |