annotate piecrust/commands/builtin/themes.py @ 475:c5df200354e8

themes: Fix crash when invoking command with no sub-command.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 19 Jul 2015 17:00:57 -0700
parents d70a4adb61dd
children d5885c6d64bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 'create',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 help="Create a new theme for the current website.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 p.add_argument(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 '--from-default',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 action='store_true',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 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
32 "theme into the theme directory"))
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 'theme_name',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 help=("The name of the theme"))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 p.set_defaults(sub_func=self._createTheme)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 p = subparsers.add_parser(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 'override',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 help="Copies a theme to the website for customization.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 p.set_defaults(sub_func=self._overrideTheme)
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 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
44 if not hasattr(ctx.args, 'sub_func'):
c5df200354e8 themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents: 273
diff changeset
45 ctx.parser.parse_args(['themes', '--help'])
c5df200354e8 themes: Fix crash when invoking command with no sub-command.
Ludovic Chabant <ludovic@chabant.com>
parents: 273
diff changeset
46 return
273
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 ctx.args.sub_func(ctx)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 def _createTheme(self, ctx):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 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
51 if os.path.exists(theme_dir):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 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
53 "Are you sure? [Y/n]")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 ans = input()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 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
56 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 shutil.rmtree(theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 try:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 if ctx.args.from_default:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 def reporting_copy2(src, dst):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 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
64 logger.info(rel_dst)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 shutil.copy2(src, dst)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 default_theme_dir = os.path.join(RESOURCES_DIR, 'theme')
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 shutil.copytree(default_theme_dir, theme_dir,
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 copy_function=reporting_copy2)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 return 0
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 logger.info("Creating theme directory.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 os.makedirs(theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 logger.info("Creating theme_config.yml")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 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
77 with open(config_path, 'w', encoding='utf8') as fp:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 fp.write('')
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 logger.info("Creating theme_info.yml")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 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
82 with open(info_path, 'w', encoding='utf8') as fp:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 yaml.dump(
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 {
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 'name': ctx.args.theme_name or 'My New Theme',
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 'description': "A new PieCrust theme.",
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 'authors': ['Your Name Here <email or twitter>'],
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 'url': 'http://www.example.org'},
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 fp,
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 default_flow_style=False)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 return 0
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 except:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 logger.error("Error occured, deleting theme directory.")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 shutil.rmtree(theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 raise
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 def _overrideTheme(self, ctx):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 app_dir = ctx.app.root_dir
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 theme_dir = ctx.app.theme_dir
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 if not theme_dir:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 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
102 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 copies = []
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 for dirpath, dirnames, filenames in os.walk(theme_dir):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 rel_dirpath = os.path.relpath(dirpath, theme_dir)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 for name in filenames:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 if (dirpath == theme_dir and
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 name in [THEME_CONFIG_PATH, THEME_INFO_PATH]):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 continue
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 src_path = os.path.join(dirpath, name)
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 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
113 copies.append((src_path, dst_path))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 conflicts = []
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 for c in copies:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 if os.path.exists(c[1]):
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 conflicts.append(c[1])
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 if conflicts:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 logger.warning("Some website files will be overwritten:")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 for c in conflicts:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 logger.warning(os.path.relpath(c, app_dir))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 logger.warning("Are you sure? [Y/n]")
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 ans = input()
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 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
126 return 1
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 for c in copies:
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 logger.info(os.path.relpath(c[1], app_dir))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 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
131 os.makedirs(os.path.dirname(c[1]))
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 shutil.copy2(c[0], c[1])
d70a4adb61dd themes: Add the `chef themes` command
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133