Mercurial > piecrust2
annotate piecrust/commands/builtin/themes.py @ 488:a00750896316
themes: Improve CLI, add `deactivate` command.
* `link` command renamed to `activate`.
* `deactivate` command removes the active theme.
* `info` command shows information about the active theme. Runs by default
when doing `chef themes`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 22 Jul 2015 21:32:54 -0700 |
parents | d5885c6d64bd |
children | 5336e146ac8d |
rev | line source |
---|---|
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import shutil |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import logging |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import yaml |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 from piecrust import ( |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 RESOURCES_DIR, THEME_DIR, THEME_CONFIG_PATH, THEME_INFO_PATH) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 from piecrust.commands.base import ChefCommand |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 logger = logging.getLogger(__name__) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 class ThemesCommand(ChefCommand): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 def __init__(self): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 super(ThemesCommand, self).__init__() |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 self.name = 'themes' |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 self.description = "Manage the themes for the current website." |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 def setupParser(self, parser, app): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 if app.root_dir is None: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 return |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 subparsers = parser.add_subparsers() |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 p = subparsers.add_parser( |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
26 'info', |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
27 help="Provides information about the current theme.") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
28 p.set_defaults(sub_func=self._info) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
29 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
30 p = subparsers.add_parser( |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 'create', |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 help="Create a new theme for the current website.") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 p.add_argument( |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 '--from-default', |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 action='store_true', |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 help=("Create a new theme by copying the default PieCrust " |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 "theme into the theme directory")) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 p.add_argument( |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 'theme_name', |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 help=("The name of the theme")) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 p.set_defaults(sub_func=self._createTheme) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 p = subparsers.add_parser( |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 'override', |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 help="Copies a theme to the website for customization.") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 p.set_defaults(sub_func=self._overrideTheme) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 |
480
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
48 p = subparsers.add_parser( |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
49 'activate', |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
50 help="Makes a given theme the active one for the current " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
51 "website by creating a symbolic link to it from the " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
52 "'theme' directory. If a symbolic link can't be created " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
53 "the theme will be copied to the 'theme' directory.") |
480
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
54 p.add_argument( |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
55 'theme_dir', |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
56 help="The directory of the theme to link.") |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
57 p.add_argument( |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
58 '--link-only', |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
59 action='store_true', |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
60 help="Abort the activation if a symbolic link can't be " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
61 "created") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
62 p.set_defaults(sub_func=self._activateTheme) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
63 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
64 p = subparsers.add_parser( |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
65 'deactivate', |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
66 help="Removes the currently active theme for the website. " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
67 "This removes the symbolic link to the theme, if any, or " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
68 "deletes the theme folder if it was copied locally.") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
69 p.set_defaults(sub_func=self._deactivateTheme) |
480
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
70 |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 def checkedRun(self, ctx): |
475
c5df200354e8
themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents:
273
diff
changeset
|
72 if not hasattr(ctx.args, 'sub_func'): |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
73 ctx.args = ctx.parser.parse_args(['themes', 'info']) |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 ctx.args.sub_func(ctx) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
76 def _info(self, ctx): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
77 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
78 if not os.path.exists(theme_dir): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
79 logger.info("Using default theme, from: %s" % ctx.app.theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
80 elif os.path.islink(theme_dir): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
81 target = os.readlink(theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
82 target = os.path.join(os.path.dirname(theme_dir), target) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
83 logger.info("Using theme, from: %s" % target) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
84 else: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
85 logger.info("Using local theme.") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
86 |
273
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 def _createTheme(self, ctx): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 if os.path.exists(theme_dir): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 logger.warning("A theme already exists, and will be overwritten. " |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 "Are you sure? [Y/n]") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 ans = input() |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 if len(ans) > 0 and ans.lower() not in ['y', 'yes']: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 return 1 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 shutil.rmtree(theme_dir) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 try: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 if ctx.args.from_default: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 def reporting_copy2(src, dst): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 rel_dst = os.path.relpath(dst, ctx.app.root_dir) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 logger.info(rel_dst) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 shutil.copy2(src, dst) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 default_theme_dir = os.path.join(RESOURCES_DIR, 'theme') |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 shutil.copytree(default_theme_dir, theme_dir, |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 copy_function=reporting_copy2) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 return 0 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 logger.info("Creating theme directory.") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 os.makedirs(theme_dir) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 logger.info("Creating theme_config.yml") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 config_path = os.path.join(theme_dir, THEME_CONFIG_PATH) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 with open(config_path, 'w', encoding='utf8') as fp: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 fp.write('') |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 logger.info("Creating theme_info.yml") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 info_path = os.path.join(theme_dir, THEME_INFO_PATH) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 with open(info_path, 'w', encoding='utf8') as fp: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 yaml.dump( |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 { |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 'name': ctx.args.theme_name or 'My New Theme', |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 'description': "A new PieCrust theme.", |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 'authors': ['Your Name Here <email or twitter>'], |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 'url': 'http://www.example.org'}, |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 fp, |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 default_flow_style=False) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 return 0 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 except: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 logger.error("Error occured, deleting theme directory.") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 shutil.rmtree(theme_dir) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 raise |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 def _overrideTheme(self, ctx): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 app_dir = ctx.app.root_dir |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 theme_dir = ctx.app.theme_dir |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 if not theme_dir: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 logger.error("There is not theme currently applied to override.") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 return 1 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 copies = [] |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 for dirpath, dirnames, filenames in os.walk(theme_dir): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 rel_dirpath = os.path.relpath(dirpath, theme_dir) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 for name in filenames: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 if (dirpath == theme_dir and |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 name in [THEME_CONFIG_PATH, THEME_INFO_PATH]): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 continue |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 src_path = os.path.join(dirpath, name) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 dst_path = os.path.join(app_dir, rel_dirpath, name) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 copies.append((src_path, dst_path)) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
152 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
153 conflicts = [] |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
154 for c in copies: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
155 if os.path.exists(c[1]): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
156 conflicts.append(c[1]) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 if conflicts: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 logger.warning("Some website files will be overwritten:") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
159 for c in conflicts: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
160 logger.warning(os.path.relpath(c, app_dir)) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
161 logger.warning("Are you sure? [Y/n]") |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
162 ans = input() |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
163 if len(ans) > 0 and ans.lower() not in ['y', 'yes']: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
164 return 1 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 for c in copies: |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 logger.info(os.path.relpath(c[1], app_dir)) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 if not os.path.exists(os.path.dirname(c[1])): |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 os.makedirs(os.path.dirname(c[1])) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 shutil.copy2(c[0], c[1]) |
d70a4adb61dd
themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
171 |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
172 def _activateTheme(self, ctx): |
480
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
173 if not os.path.isdir(ctx.args.theme_dir): |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
174 logger.error("Invalid theme directory: %s" % ctx.args.theme_dir) |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
175 return 1 |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
176 |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
177 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
178 |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
179 if os.path.islink(theme_dir): |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
180 logger.debug("Unlinking: %s" % theme_dir) |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
181 os.unlink(theme_dir) |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
182 elif os.path.isdir(theme_dir): |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
183 logger.warning("A theme already exists, and will be overwritten. " |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
184 "Are you sure? [Y/n]") |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
185 ans = input() |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
186 if len(ans) > 0 and ans.lower() not in ['y', 'yes']: |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
187 return 1 |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
188 |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
189 shutil.rmtree(theme_dir) |
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
190 |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
191 try: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
192 os.symlink(ctx.args.theme_dir, theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
193 except (NotImplementedError, OSError) as ex: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
194 if ctx.args.link_only: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
195 logger.error("Couldn't symlink the theme: %s" % ex) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
196 return 1 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
197 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
198 # pre-Vista Windows, or unprivileged user... gotta copy the |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
199 # theme the old fashioned way. |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
200 logging.warning("Can't create a symbolic link... copying the " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
201 "theme directory instead.") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
202 ignore = shutil.ignore_patterns('.git*', '.hg*', '.svn', '.bzr') |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
203 shutil.copytree(ctx.args.theme_dir, theme_dir, ignore=ignore) |
480
d5885c6d64bd
themes: Add a `link` sub-command to install a theme via a symbolic link.
Ludovic Chabant <ludovic@chabant.com>
parents:
475
diff
changeset
|
204 |
488
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
205 def _deactivateTheme(self, ctx): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
206 theme_dir = os.path.join(ctx.app.root_dir, THEME_DIR) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
207 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
208 if os.path.islink(theme_dir): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
209 logger.debug("Unlinking: %s" % theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
210 os.unlink(theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
211 elif os.path.isdir(theme_dir): |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
212 logger.warning("The active theme is local. Are you sure you want " |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
213 "to delete the theme directory? [Y/n]") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
214 ans = input() |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
215 if len(ans) > 0 and ans.lower() not in ['y', 'yes']: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
216 return 1 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
217 |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
218 shutil.rmtree(theme_dir) |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
219 else: |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
220 logger.info("No currently active theme.") |
a00750896316
themes: Improve CLI, add `deactivate` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
480
diff
changeset
|
221 |