comparison piecrust/app.py @ 758:6abb436fea5b

publish: Make publisher more powerful and better exposed on the command line. * Make the `chef publish` command have one sub-command per publish target. * Add custom argument parsing per publisher to have strong extra arguments available per publish target. * Make publish targets a first class citizen of the `PieCrust` app class.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 25 Jun 2016 17:03:29 -0700
parents ab5c6a8ae90a
children fd694f1297c7
comparison
equal deleted inserted replaced
757:7147b06670fd 758:6abb436fea5b
1 import time 1 import time
2 import os.path 2 import os.path
3 import hashlib 3 import hashlib
4 import logging 4 import logging
5 import urllib.parse
5 from werkzeug.utils import cached_property 6 from werkzeug.utils import cached_property
6 from piecrust import ( 7 from piecrust import (
7 RESOURCES_DIR, 8 RESOURCES_DIR,
8 CACHE_DIR, TEMPLATES_DIR, ASSETS_DIR, 9 CACHE_DIR, TEMPLATES_DIR, ASSETS_DIR,
9 THEME_DIR, 10 THEME_DIR,
10 CONFIG_PATH, THEME_CONFIG_PATH) 11 CONFIG_PATH, THEME_CONFIG_PATH)
11 from piecrust.appconfig import PieCrustConfiguration 12 from piecrust.appconfig import PieCrustConfiguration
12 from piecrust.cache import ExtensibleCache, NullExtensibleCache 13 from piecrust.cache import ExtensibleCache, NullExtensibleCache
14 from piecrust.configuration import ConfigurationError, merge_dicts
15 from piecrust.environment import StandardEnvironment
13 from piecrust.plugins.base import PluginLoader 16 from piecrust.plugins.base import PluginLoader
14 from piecrust.environment import StandardEnvironment
15 from piecrust.configuration import ConfigurationError, merge_dicts
16 from piecrust.routing import Route 17 from piecrust.routing import Route
17 from piecrust.sources.base import REALM_THEME 18 from piecrust.sources.base import REALM_THEME
18 19
19 20
20 logger = logging.getLogger(__name__) 21 logger = logging.getLogger(__name__)
163 g['type']) 164 g['type'])
164 gen = cls(self, n, g) 165 gen = cls(self, n, g)
165 gens.append(gen) 166 gens.append(gen)
166 return gens 167 return gens
167 168
169 @cached_property
170 def publishers(self):
171 defs_by_name = {}
172 defs_by_scheme = {}
173 for cls in self.plugin_loader.getPublishers():
174 defs_by_name[cls.PUBLISHER_NAME] = cls
175 if cls.PUBLISHER_SCHEME:
176 defs_by_scheme[cls.PUBLISHER_SCHEME] = cls
177
178 tgts = []
179 publish_config = self.config.get('publish')
180 if publish_config is None:
181 return tgts
182 for n, t in publish_config.items():
183 pub_type = None
184 is_scheme = False
185 if isinstance(t, dict):
186 pub_type = t.get('type')
187 elif isinstance(t, str):
188 comps = urllib.parse.urlparse(t)
189 pub_type = comps.scheme
190 is_scheme = True
191 cls = (defs_by_scheme.get(pub_type) if is_scheme
192 else defs_by_name.get(pub_type))
193 if cls is None:
194 raise ConfigurationError("No such publisher: %s" % pub_type)
195 tgt = cls(self, n, t)
196 tgts.append(tgt)
197 return tgts
198
168 def getSource(self, source_name): 199 def getSource(self, source_name):
169 for source in self.sources: 200 for source in self.sources:
170 if source.name == source_name: 201 if source.name == source_name:
171 return source 202 return source
172 return None 203 return None
191 222
192 def getGeneratorRoute(self, generator_name): 223 def getGeneratorRoute(self, generator_name):
193 for route in self.routes: 224 for route in self.routes:
194 if route.generator_name == generator_name: 225 if route.generator_name == generator_name:
195 return route 226 return route
227 return None
228
229 def getPublisher(self, target_name):
230 for pub in self.publishers:
231 if pub.target == target_name:
232 return pub
196 return None 233 return None
197 234
198 def _get_dir(self, default_rel_dir): 235 def _get_dir(self, default_rel_dir):
199 abs_dir = os.path.join(self.root_dir, default_rel_dir) 236 abs_dir = os.path.join(self.root_dir, default_rel_dir)
200 if os.path.isdir(abs_dir): 237 if os.path.isdir(abs_dir):