Mercurial > piecrust2
comparison piecrust/sources/base.py @ 186:e61fbae61402
sources: Pass any current mode to `_populateMetadata` when finding pages.
Page sources like the `prose` source may need to open an existing page's
file to read stuff from it. This won't work if the metadata is populated as
part of finding a path to create a page (like when running `chef prepare`).
We pass the mode to `_populateMetadata` so the underlying class now knows
the current context in which it is called.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sun, 04 Jan 2015 15:48:29 -0800 |
| parents | d5991525801d |
| children | 0b2d8f6df4ce |
comparison
equal
deleted
inserted
replaced
| 185:139179dc7abd | 186:e61fbae61402 |
|---|---|
| 307 def getPageFactories(self): | 307 def getPageFactories(self): |
| 308 for p in self.inner_source: | 308 for p in self.inner_source: |
| 309 yield CachedPageFactory(p) | 309 yield CachedPageFactory(p) |
| 310 | 310 |
| 311 | 311 |
| 312 class SimplePageSource(PageSource, IListableSource): | 312 class SimplePageSource(PageSource, IListableSource, IPreparingSource, |
| 313 SimplePaginationSourceMixin): | |
| 313 def __init__(self, app, name, config): | 314 def __init__(self, app, name, config): |
| 314 super(SimplePageSource, self).__init__(app, name, config) | 315 super(SimplePageSource, self).__init__(app, name, config) |
| 315 self.fs_endpoint = config.get('fs_endpoint', name) | 316 self.fs_endpoint = config.get('fs_endpoint', name) |
| 316 self.fs_endpoint_path = os.path.join(self.root_dir, self.fs_endpoint) | 317 self.fs_endpoint_path = os.path.join(self.root_dir, self.fs_endpoint) |
| 317 self.supported_extensions = list(app.config.get('site/auto_formats').keys()) | 318 self.supported_extensions = list(app.config.get('site/auto_formats').keys()) |
| 351 if mode == MODE_CREATING: | 352 if mode == MODE_CREATING: |
| 352 if ext == '': | 353 if ext == '': |
| 353 path = '%s.%s' % (path, self.default_auto_format) | 354 path = '%s.%s' % (path, self.default_auto_format) |
| 354 rel_path = os.path.relpath(path, self.fs_endpoint_path) | 355 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
| 355 rel_path = rel_path.replace('\\', '/') | 356 rel_path = rel_path.replace('\\', '/') |
| 356 self._populateMetadata(rel_path, metadata) | 357 self._populateMetadata(rel_path, metadata, mode) |
| 357 return rel_path, metadata | 358 return rel_path, metadata |
| 358 | 359 |
| 359 if ext == '': | 360 if ext == '': |
| 360 paths_to_check = [ | 361 paths_to_check = [ |
| 361 '%s.%s' % (path, e) | 362 '%s.%s' % (path, e) |
| 364 paths_to_check = [path] | 365 paths_to_check = [path] |
| 365 for path in paths_to_check: | 366 for path in paths_to_check: |
| 366 if os.path.isfile(path): | 367 if os.path.isfile(path): |
| 367 rel_path = os.path.relpath(path, self.fs_endpoint_path) | 368 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
| 368 rel_path = rel_path.replace('\\', '/') | 369 rel_path = rel_path.replace('\\', '/') |
| 369 self._populateMetadata(rel_path, metadata) | 370 self._populateMetadata(rel_path, metadata, mode) |
| 370 return rel_path, metadata | 371 return rel_path, metadata |
| 371 | 372 |
| 372 return None, None | 373 return None, None |
| 373 | 374 |
| 374 def listPath(self, rel_path): | 375 def listPath(self, rel_path): |
| 401 def getBasename(self, rel_path): | 402 def getBasename(self, rel_path): |
| 402 filename = os.path.basename(rel_path) | 403 filename = os.path.basename(rel_path) |
| 403 name, _ = os.path.splitext(filename) | 404 name, _ = os.path.splitext(filename) |
| 404 return name | 405 return name |
| 405 | 406 |
| 407 def setupPrepareParser(self, parser, app): | |
| 408 parser.add_argument('uri', help='The URI for the new page.') | |
| 409 | |
| 410 def buildMetadata(self, args): | |
| 411 return {'path': args.uri} | |
| 412 | |
| 406 def _makeSlug(self, rel_path): | 413 def _makeSlug(self, rel_path): |
| 407 slug, ext = os.path.splitext(rel_path) | 414 slug, ext = os.path.splitext(rel_path) |
| 408 slug = slug.replace('\\', '/') | 415 slug = slug.replace('\\', '/') |
| 409 if ext.lstrip('.') not in self.supported_extensions: | 416 if ext.lstrip('.') not in self.supported_extensions: |
| 410 slug += ext | 417 slug += ext |
| 420 def _filterPageFilename(self, f): | 427 def _filterPageFilename(self, f): |
| 421 return (f[0] != '.' and # .DS_store and other crap | 428 return (f[0] != '.' and # .DS_store and other crap |
| 422 f[-1] != '~' and # Vim temp files and what-not | 429 f[-1] != '~' and # Vim temp files and what-not |
| 423 f not in ['Thumbs.db']) # Windows bullshit | 430 f not in ['Thumbs.db']) # Windows bullshit |
| 424 | 431 |
| 425 def _populateMetadata(self, rel_path, metadata): | 432 def _populateMetadata(self, rel_path, metadata, mode=None): |
| 426 pass | 433 pass |
| 427 | 434 |
| 428 | 435 |
| 429 class DefaultPageSource(SimplePageSource, | 436 class DefaultPageSource(SimplePageSource): |
| 430 IPreparingSource, IListableSource, | |
| 431 SimplePaginationSourceMixin): | |
| 432 SOURCE_NAME = 'default' | 437 SOURCE_NAME = 'default' |
| 433 | 438 |
| 434 def __init__(self, app, name, config): | 439 def __init__(self, app, name, config): |
| 435 super(DefaultPageSource, self).__init__(app, name, config) | 440 super(DefaultPageSource, self).__init__(app, name, config) |
| 436 | |
| 437 def setupPrepareParser(self, parser, app): | |
| 438 parser.add_argument('uri', help='The URI for the new page.') | |
| 439 | |
| 440 def buildMetadata(self, args): | |
| 441 return {'path': args.uri} | |
| 442 | 441 |
| 443 | 442 |
| 444 class SourceFactoryIterator(object): | 443 class SourceFactoryIterator(object): |
| 445 def __init__(self, source): | 444 def __init__(self, source): |
| 446 self.source = source | 445 self.source = source |
