annotate piecrust/themes/base.py @ 1158:5762a0b821ac

core: Remove yaml deprecation warning.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 11 Jun 2019 16:40:11 -0700
parents 40a40305c4e1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import sys
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import yaml
893
14cca285f73b clean: PEP8.
Ludovic Chabant <ludovic@chabant.com>
parents: 745
diff changeset
4 from piecrust import CONFIG_PATH, THEMES_DIR
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 class Theme(object):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 def getPath(self):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 mod_name = type(self).__module__
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 mod_file = sys.modules[mod_name].__file__
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 return os.path.dirname(mod_file)
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 class ThemeNotFoundError(Exception):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 pass
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 class ThemeLoader(object):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 def __init__(self, root_dir):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 self.root_dir = root_dir
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 def getThemeDir(self):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 # Pre-load the config quickly to see if we're loading a specific
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 # theme from somehwere.
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 # TODO: make configs and themes load together to speed this up.
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 config_path = os.path.join(self.root_dir, CONFIG_PATH)
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 with open(config_path, 'r', encoding='utf8') as fp:
1158
5762a0b821ac core: Remove yaml deprecation warning.
Ludovic Chabant <ludovic@chabant.com>
parents: 1157
diff changeset
28 config = yaml.load(fp.read(), Loader=yaml.SafeLoader)
989
8adc27285d93 bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents: 893
diff changeset
29 if not config:
8adc27285d93 bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents: 893
diff changeset
30 return None
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 site_config = config.get('site', {})
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 theme = site_config.get('theme', None)
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 if theme is None:
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 return None
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 # Get the list of directories in which themes are installed.
1157
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 989
diff changeset
37 from piecrust.pathutil import expandall
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 dirs = []
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 themes_dirs = site_config.get('themes_dirs', [])
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if isinstance(themes_dirs, str):
1157
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 989
diff changeset
41 dirs.append(os.path.join(self.root_dir, expandall(themes_dirs)))
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 else:
1157
40a40305c4e1 config: Support environment variables in theme directories.
Ludovic Chabant <ludovic@chabant.com>
parents: 989
diff changeset
43 dirs += [os.path.join(self.root_dir, expandall(p))
745
0b4eb0e37363 themes: Expand `~` paths, fix error message.
Ludovic Chabant <ludovic@chabant.com>
parents: 709
diff changeset
44 for p in themes_dirs]
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 # Add the default `themes` directory.
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 default_themes_dir = os.path.join(self.root_dir, THEMES_DIR)
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 if os.path.isdir(default_themes_dir):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 dirs.append(default_themes_dir)
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 # Try to find the theme the user wants.
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 for d in dirs:
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 theme_dir = os.path.join(d, theme)
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 if os.path.isdir(theme_dir):
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 return theme_dir
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 raise ThemeNotFoundError(
893
14cca285f73b clean: PEP8.
Ludovic Chabant <ludovic@chabant.com>
parents: 745
diff changeset
58 "Can't find theme '%s'. Looked in: %s" %
14cca285f73b clean: PEP8.
Ludovic Chabant <ludovic@chabant.com>
parents: 745
diff changeset
59 (theme, ', '.join(dirs)))
709
4285b2c9b872 themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60