comparison piecrust/sources/posts.py @ 5:474c9882decf

Upgrade to Python 3.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 11 Aug 2014 22:36:47 -0700
parents f485ba500df3
children f5ca5c5bed85
comparison
equal deleted inserted replaced
4:7dc71c2dc9a8 5:474c9882decf
18 18
19 def __init__(self, app, name, config): 19 def __init__(self, app, name, config):
20 super(PostsSource, self).__init__(app, name, config) 20 super(PostsSource, self).__init__(app, name, config)
21 self.fs_endpoint = config.get('fs_endpoint', name) 21 self.fs_endpoint = config.get('fs_endpoint', name)
22 self.fs_endpoint_path = os.path.join(self.root_dir, CONTENT_DIR, self.fs_endpoint) 22 self.fs_endpoint_path = os.path.join(self.root_dir, CONTENT_DIR, self.fs_endpoint)
23 self.supported_extensions = app.config.get('site/auto_formats').keys() 23 self.supported_extensions = list(app.config.get('site/auto_formats').keys())
24 24
25 @property 25 @property
26 def path_format(self): 26 def path_format(self):
27 return self.__class__.PATH_FORMAT 27 return self.__class__.PATH_FORMAT
28 28
103 103
104 def buildMetadata(self, args): 104 def buildMetadata(self, args):
105 today = datetime.date.today() 105 today = datetime.date.today()
106 year, month, day = today.year, today.month, today.day 106 year, month, day = today.year, today.month, today.day
107 if args.date: 107 if args.date:
108 year, month, day = filter( 108 year, month, day = [s for s in args.date.split('/') if int(s)]
109 lambda s: int(s),
110 args.date.split('/'))
111 return {'year': year, 'month': month, 'day': day, 'slug': args.slug} 109 return {'year': year, 'month': month, 'day': day, 'slug': args.slug}
112 110
113 def _checkFsEndpointPath(self): 111 def _checkFsEndpointPath(self):
114 if not os.path.isdir(self.fs_endpoint_path): 112 if not os.path.isdir(self.fs_endpoint_path):
115 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path) 113 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path)
161 def buildPageFactories(self): 159 def buildPageFactories(self):
162 logger.debug("Scanning for posts (shallow) in: %s" % self.fs_endpoint_path) 160 logger.debug("Scanning for posts (shallow) in: %s" % self.fs_endpoint_path)
163 year_pattern = re.compile(r'(\d{4})$') 161 year_pattern = re.compile(r'(\d{4})$')
164 file_pattern = re.compile(r'(\d{2})-(\d{2})_(.*)\.(\w+)$') 162 file_pattern = re.compile(r'(\d{2})-(\d{2})_(.*)\.(\w+)$')
165 _, year_dirs, __ = next(os.walk(self.fs_endpoint_path)) 163 _, year_dirs, __ = next(os.walk(self.fs_endpoint_path))
166 year_dirs = filter(lambda d: year_pattern.match(d), year_dirs) 164 year_dirs = [d for d in year_dirs if year_pattern.match(d)]
167 for yd in year_dirs: 165 for yd in year_dirs:
168 if year_pattern.match(yd) is None: 166 if year_pattern.match(yd) is None:
169 logger.warning("'%s' is not formatted as 'YYYY' and will be ignored. " 167 logger.warning("'%s' is not formatted as 'YYYY' and will be ignored. "
170 "Is that a typo?") 168 "Is that a typo?")
171 continue 169 continue
199 logger.debug("Scanning for posts (hierarchy) in: %s" % self.fs_endpoint_path) 197 logger.debug("Scanning for posts (hierarchy) in: %s" % self.fs_endpoint_path)
200 year_pattern = re.compile(r'(\d{4})$') 198 year_pattern = re.compile(r'(\d{4})$')
201 month_pattern = re.compile(r'(\d{2})$') 199 month_pattern = re.compile(r'(\d{2})$')
202 file_pattern = re.compile(r'(\d{2})_(.*)\.(\w+)$') 200 file_pattern = re.compile(r'(\d{2})_(.*)\.(\w+)$')
203 _, year_dirs, __ = next(os.walk(self.fs_endpoint_path)) 201 _, year_dirs, __ = next(os.walk(self.fs_endpoint_path))
204 year_dirs = filter(lambda d: year_pattern.match(d), year_dirs) 202 year_dirs = [d for d in year_dirs if year_pattern.match(d)]
205 for yd in year_dirs: 203 for yd in year_dirs:
206 year = int(yd) 204 year = int(yd)
207 year_dir = os.path.join(self.fs_endpoint_path, yd) 205 year_dir = os.path.join(self.fs_endpoint_path, yd)
208 206
209 _, month_dirs, __ = next(os.walk(year_dir)) 207 _, month_dirs, __ = next(os.walk(year_dir))
210 month_dirs = filter(lambda d: month_pattern.match(d), month_dirs) 208 month_dirs = [d for d in month_dirs if month_pattern.match(d)]
211 for md in month_dirs: 209 for md in month_dirs:
212 month = int(md) 210 month = int(md)
213 month_dir = os.path.join(year_dir, md) 211 month_dir = os.path.join(year_dir, md)
214 212
215 _, __, filenames = next(os.walk(month_dir)) 213 _, __, filenames = next(os.walk(month_dir))