comparison piecrust/app.py @ 999:46025a1b5434

plugins: Support multiple customizable plugins directories. Also add support for specifying the theme directory in some customizable paths.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 21 Nov 2017 09:54:56 -0800
parents 7d83b9484b98
children 2e5c5d33d62c
comparison
equal deleted inserted replaced
998:a49e6846e0da 999:46025a1b5434
14 from piecrust.configuration import ConfigurationError, merge_dicts 14 from piecrust.configuration import ConfigurationError, merge_dicts
15 from piecrust.environment import StandardEnvironment 15 from piecrust.environment import StandardEnvironment
16 from piecrust.plugins.base import PluginLoader 16 from piecrust.plugins.base import PluginLoader
17 from piecrust.routing import Route 17 from piecrust.routing import Route
18 from piecrust.sources.base import REALM_THEME 18 from piecrust.sources.base import REALM_THEME
19 from piecrust.uriutil import multi_replace
19 20
20 21
21 logger = logging.getLogger(__name__) 22 logger = logging.getLogger(__name__)
22 23
23 24
110 # No theme if the curent site is already a theme. 111 # No theme if the curent site is already a theme.
111 if self.theme_site: 112 if self.theme_site:
112 return None 113 return None
113 114
114 # See if there's a theme we absolutely want. 115 # See if there's a theme we absolutely want.
115 td = self._get_dir(THEME_DIR) 116 td = os.path.join(self.root_dir, THEME_DIR)
116 if td is not None: 117 if os.path.isdir(td):
117 return td 118 return td
118 119
119 # Try to load a theme specified in the configuration. 120 # Try to load a theme specified in the configuration.
120 from piecrust.themes.base import ThemeLoader 121 from piecrust.themes.base import ThemeLoader
121 loader = ThemeLoader(self.root_dir) 122 loader = ThemeLoader(self.root_dir)
125 126
126 # Nothing... use the default theme. 127 # Nothing... use the default theme.
127 return os.path.join(RESOURCES_DIR, 'theme') 128 return os.path.join(RESOURCES_DIR, 'theme')
128 129
129 @cached_property 130 @cached_property
130 def plugins_dir(self): 131 def plugins_dirs(self):
131 return self._get_dir(PLUGINS_DIR) 132 return self._get_configurable_dirs(PLUGINS_DIR, 'site/plugins_dirs')
132 133
133 @cached_property 134 @cached_property
134 def cache_dir(self): 135 def cache_dir(self):
135 return os.path.join(self.root_dir, CACHE_DIR, self.cache_key) 136 return os.path.join(self.root_dir, CACHE_DIR, self.cache_key)
136 137
238 for pub in self.publishers: 239 for pub in self.publishers:
239 if pub.target == target_name: 240 if pub.target == target_name:
240 return pub 241 return pub
241 return None 242 return None
242 243
243 def _get_dir(self, default_rel_dir): 244 def resolvePath(self, path):
244 abs_dir = os.path.join(self.root_dir, default_rel_dir) 245 path = multi_replace(path, {'%theme_dir%': self.theme_dir})
245 if os.path.isdir(abs_dir): 246 return os.path.join(self.root_dir, path)
246 return abs_dir
247 return None
248 247
249 def _get_configurable_dirs(self, default_rel_dir, conf_name): 248 def _get_configurable_dirs(self, default_rel_dir, conf_name):
250 dirs = [] 249 dirs = []
251 250
252 # Add custom directories from the configuration. 251 # Add custom directories from the configuration.
253 conf_dirs = self.config.get(conf_name) 252 conf_dirs = self.config.get(conf_name)
254 if conf_dirs is not None: 253 if conf_dirs is not None:
255 if isinstance(conf_dirs, str): 254 if isinstance(conf_dirs, str):
256 dirs.append(os.path.join(self.root_dir, conf_dirs)) 255 dirs.append(self.resolvePath(conf_dirs))
257 else: 256 else:
258 dirs += [os.path.join(self.root_dir, p) for p in conf_dirs] 257 dirs += [self.resolvePath(p) for p in conf_dirs]
259 258
260 # Add the default directory if it exists. 259 # Add the default directory if it exists.
261 default_dir = os.path.join(self.root_dir, default_rel_dir) 260 default_dir = os.path.join(self.root_dir, default_rel_dir)
262 if os.path.isdir(default_dir): 261 if os.path.isdir(default_dir):
263 dirs.append(default_dir) 262 dirs.append(default_dir)