comparison piecrust/sources/autoconfig.py @ 156:683baa977d97

Simplify `AutoConfigSource` by inheriting from `SimplePageSource`.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 27 Dec 2014 18:14:46 -0800
parents 432cd534ce08
children f43f19975671
comparison
equal deleted inserted replaced
155:70b86e904b85 156:683baa977d97
1 import os 1 import os
2 import os.path 2 import os.path
3 import logging 3 import logging
4 from piecrust.sources.base import ( 4 from piecrust.sources.base import (
5 PageSource, IPreparingSource, SimplePaginationSourceMixin, 5 SimplePageSource, IPreparingSource, SimplePaginationSourceMixin,
6 PageNotFoundError, InvalidFileSystemEndpointError, 6 PageNotFoundError, InvalidFileSystemEndpointError,
7 PageFactory, MODE_CREATING, MODE_PARSING) 7 PageFactory, MODE_CREATING, MODE_PARSING)
8 8
9 9
10 logger = logging.getLogger(__name__) 10 logger = logging.getLogger(__name__)
11 11
12 12
13 class AutoConfigSource(PageSource, 13 class AutoConfigSource(SimplePageSource,
14 SimplePaginationSourceMixin): 14 SimplePaginationSourceMixin):
15 SOURCE_NAME = 'autoconfig' 15 SOURCE_NAME = 'autoconfig'
16 PATH_FORMAT = '%(values)s/%(slug)s.%(ext)s'
17 16
18 def __init__(self, app, name, config): 17 def __init__(self, app, name, config):
19 super(AutoConfigSource, self).__init__(app, name, config) 18 super(AutoConfigSource, self).__init__(app, name, config)
20 self.fs_endpoint = config.get('fs_endpoint', name)
21 self.fs_endpoint_path = os.path.join(self.root_dir, self.fs_endpoint)
22 self.supported_extensions = list(app.config.get('site/auto_formats').keys())
23 self.default_auto_format = app.config.get('site/default_auto_format')
24 self.setting_name = config.get('setting_name', name) 19 self.setting_name = config.get('setting_name', name)
25 self.collapse_single_values = config.get('collapse_single_values', False) 20 self.collapse_single_values = config.get('collapse_single_values', False)
26 self.only_single_values = config.get('only_single_values', False) 21 self.only_single_values = config.get('only_single_values', False)
27 22
28 def buildPageFactories(self): 23 def buildPageFactories(self):
52 "in source '%s'." % self.name) 47 "in source '%s'." % self.name)
53 if self.collapse_single_values and len(values) == 1: 48 if self.collapse_single_values and len(values) == 1:
54 values = values[0] 49 values = values[0]
55 return {self.setting_name: values} 50 return {self.setting_name: values}
56 51
57 def resolveRef(self, ref_path):
58 return os.path.normpath(
59 os.path.join(self.fs_endpoint_path, ref_path))
60
61 def findPagePath(self, metadata, mode): 52 def findPagePath(self, metadata, mode):
62 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): 53 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path):
63 for f in filenames: 54 for f in filenames:
64 slug, _ = os.path.splitext(f) 55 slug, _ = os.path.splitext(f)
65 if slug == metadata['slug']: 56 if slug == metadata['slug']: