comparison piecrust/sources/posts.py @ 11:617191dec18e

Fixes for Windows, make `findPagePath` return a ref path.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 18 Aug 2014 16:47:44 -0700
parents f5ca5c5bed85
children 485682a6de50
comparison
equal deleted inserted replaced
10:cd35d356ccce 11:617191dec18e
6 import datetime 6 import datetime
7 from piecrust import CONTENT_DIR 7 from piecrust import CONTENT_DIR
8 from piecrust.sources.base import (PageSource, IPreparingSource, 8 from piecrust.sources.base import (PageSource, IPreparingSource,
9 SimplePaginationSourceMixin, 9 SimplePaginationSourceMixin,
10 PageNotFoundError, InvalidFileSystemEndpointError, 10 PageNotFoundError, InvalidFileSystemEndpointError,
11 PageFactory, MODE_CREATING) 11 PageFactory, MODE_CREATING, MODE_PARSING)
12 12
13 13
14 logger = logging.getLogger(__name__) 14 logger = logging.getLogger(__name__)
15 15
16 16
20 def __init__(self, app, name, config): 20 def __init__(self, app, name, config):
21 super(PostsSource, self).__init__(app, name, config) 21 super(PostsSource, self).__init__(app, name, config)
22 self.fs_endpoint = config.get('fs_endpoint', name) 22 self.fs_endpoint = config.get('fs_endpoint', name)
23 self.fs_endpoint_path = os.path.join(self.root_dir, CONTENT_DIR, self.fs_endpoint) 23 self.fs_endpoint_path = os.path.join(self.root_dir, CONTENT_DIR, self.fs_endpoint)
24 self.supported_extensions = list(app.config.get('site/auto_formats').keys()) 24 self.supported_extensions = list(app.config.get('site/auto_formats').keys())
25 self.default_auto_format = app.config.get('site/default_auto_format')
25 26
26 @property 27 @property
27 def path_format(self): 28 def path_format(self):
28 return self.__class__.PATH_FORMAT 29 return self.__class__.PATH_FORMAT
29 30
30 def resolveRef(self, ref_path): 31 def resolveRef(self, ref_path):
31 return os.path.join(self.fs_endpoint_path, ref_path) 32 return os.path.normpath(os.path.join(self.fs_endpoint_path, ref_path))
32 33
33 def findPagePath(self, metadata, mode): 34 def findPagePath(self, metadata, mode):
34 year = metadata.get('year') 35 year = metadata.get('year')
35 month = metadata.get('month') 36 month = metadata.get('month')
36 day = metadata.get('day') 37 day = metadata.get('day')
38 39
39 ext = metadata.get('ext') 40 ext = metadata.get('ext')
40 if ext is None: 41 if ext is None:
41 if len(self.supported_extensions) == 1: 42 if len(self.supported_extensions) == 1:
42 ext = self.supported_extensions[0] 43 ext = self.supported_extensions[0]
44 elif mode == MODE_CREATING and self.default_auto_format:
45 ext = self.default_auto_format
43 46
44 replacements = { 47 replacements = {
45 'year': year, 48 'year': year,
46 'month': month, 49 'month': month,
47 'day': day, 50 'day': day,
62 needs_recapture = True 65 needs_recapture = True
63 replacements['slug'] = '*' 66 replacements['slug'] = '*'
64 if ext is None: 67 if ext is None:
65 needs_recapture = True 68 needs_recapture = True
66 replacements['ext'] = '*' 69 replacements['ext'] = '*'
67 path = os.path.join(self.fs_endpoint_path, self.path_format % replacements) 70 path = os.path.normpath(os.path.join(
71 self.fs_endpoint_path, self.path_format % replacements))
68 72
69 if needs_recapture: 73 if needs_recapture:
70 if mode == MODE_CREATING: 74 if mode == MODE_CREATING:
71 raise ValueError("Not enough information to find a post path.") 75 raise ValueError("Not enough information to find a post path.")
72 possible_paths = glob.glob(path) 76 possible_paths = glob.glob(path)
73 if len(possible_paths) != 1: 77 if len(possible_paths) != 1:
74 raise PageNotFoundError() 78 raise PageNotFoundError()
75 path = possible_paths[0] 79 path = possible_paths[0]
76 elif not os.path.isfile(path): 80 elif mode == MODE_PARSING and not os.path.isfile(path):
77 raise PageNotFoundError() 81 raise PageNotFoundError(path)
78 82
79 regex_repl = { 83 regex_repl = {
80 'year': '(?P<year>\d{4})', 84 'year': '(?P<year>\d{4})',
81 'month': '(?P<month>\d{2})', 85 'month': '(?P<month>\d{2})',
82 'day': '(?P<day>\d{2})', 86 'day': '(?P<day>\d{2})',
83 'slug': '(?P<slug>.*)', 87 'slug': '(?P<slug>.*)',
84 'ext': '(?P<ext>.*)' 88 'ext': '(?P<ext>.*)'
85 } 89 }
86 pattern = os.path.join(self.fs_endpoint_path, self.path_format) % regex_repl 90 #sanitized_fs_endpoint_path = (self.fs_endpoint_path.
87 m = re.match(pattern, path) 91 # replace('\\', '/').rstrip('/'))
92 #pattern = (re.escape(sanitized_fs_endpoint_path) + '/' +
93 # self.path_format % regex_repl)
94 pattern = self.path_format % regex_repl + '$'
95 m = re.search(pattern, path.replace('\\', '/'))
88 if not m: 96 if not m:
89 raise Exception("Expected to be able to match path with path " 97 raise Exception("Expected to be able to match path with path "
90 "format: %s" % path) 98 "format: %s" % path)
91 fac_metadata = { 99 fac_metadata = {
92 'year': m.group('year'), 100 'year': m.group('year'),
93 'month': m.group('month'), 101 'month': m.group('month'),
94 'day': m.group('day'), 102 'day': m.group('day'),
95 'slug': m.group('slug') 103 'slug': m.group('slug')
96 } 104 }
97 105 rel_path = os.path.relpath(path, self.fs_endpoint_path)
98 return path, fac_metadata 106 rel_path = rel_path.replace('\\', '/')
107 return rel_path, fac_metadata
99 108
100 def setupPrepareParser(self, parser, app): 109 def setupPrepareParser(self, parser, app):
101 parser.add_argument('-d', '--date', help="The date of the post, " 110 parser.add_argument('-d', '--date', help="The date of the post, "
102 "in `year/month/day` format (defaults to today).") 111 "in `year/month/day` format (defaults to today).")
103 parser.add_argument('slug', help="The URL slug for the new post.") 112 parser.add_argument('slug', help="The URL slug for the new post.")
112 def _checkFsEndpointPath(self): 121 def _checkFsEndpointPath(self):
113 if not os.path.isdir(self.fs_endpoint_path): 122 if not os.path.isdir(self.fs_endpoint_path):
114 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path) 123 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path)
115 124
116 def _makeFactory(self, path, slug, year, month, day): 125 def _makeFactory(self, path, slug, year, month, day):
126 path = path.replace('\\', '/')
117 timestamp = datetime.date(year, month, day) 127 timestamp = datetime.date(year, month, day)
118 metadata = { 128 metadata = {
119 'slug': slug, 129 'slug': slug,
120 'year': year, 130 'year': year,
121 'month': month, 131 'month': month,