Mercurial > piecrust2
annotate piecrust/themes/base.py @ 893:14cca285f73b
clean: PEP8.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 02 Jul 2017 22:20:48 -0700 |
parents | 0b4eb0e37363 |
children | 8adc27285d93 |
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 | 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: |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 config = yaml.load(fp.read()) |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 site_config = config.get('site', {}) |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 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
|
31 if theme is None: |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 return None |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 # Get the list of directories in which themes are installed. |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 dirs = [] |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 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
|
37 if isinstance(themes_dirs, str): |
745
0b4eb0e37363
themes: Expand `~` paths, fix error message.
Ludovic Chabant <ludovic@chabant.com>
parents:
709
diff
changeset
|
38 dirs.append(os.path.join(self.root_dir, |
0b4eb0e37363
themes: Expand `~` paths, fix error message.
Ludovic Chabant <ludovic@chabant.com>
parents:
709
diff
changeset
|
39 os.path.expanduser(themes_dirs))) |
709
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 else: |
745
0b4eb0e37363
themes: Expand `~` paths, fix error message.
Ludovic Chabant <ludovic@chabant.com>
parents:
709
diff
changeset
|
41 dirs += [os.path.join(self.root_dir, os.path.expanduser(p)) |
0b4eb0e37363
themes: Expand `~` paths, fix error message.
Ludovic Chabant <ludovic@chabant.com>
parents:
709
diff
changeset
|
42 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
|
43 |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 # Add the default `themes` directory. |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 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
|
46 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
|
47 dirs.append(default_themes_dir) |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 # 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
|
50 for d in dirs: |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 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
|
52 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
|
53 return theme_dir |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 |
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 raise ThemeNotFoundError( |
893 | 56 "Can't find theme '%s'. Looked in: %s" % |
57 (theme, ', '.join(dirs))) | |
709
4285b2c9b872
themes: Add support for loading from a library of themes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 |