comparison piecrust/sources/default.py @ 520:bab91fcef741

bake/serve: Improve support for unicode, add slugification options. * Add slugification options for taxonomies. * Sort out some unicode support problems on OSX. * Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 28 Jul 2015 18:34:21 -0700
parents 81d2fd526c82
children 0c74a6c4533d
comparison
equal deleted inserted replaced
519:9d1a89cd8146 520:bab91fcef741
1 import os
2 import os.path 1 import os.path
3 import logging 2 import logging
3 from piecrust import osutil
4 from piecrust.sources.base import ( 4 from piecrust.sources.base import (
5 PageFactory, PageSource, InvalidFileSystemEndpointError, 5 PageFactory, PageSource, InvalidFileSystemEndpointError,
6 MODE_CREATING) 6 MODE_CREATING)
7 from piecrust.sources.interfaces import IListableSource, IPreparingSource 7 from piecrust.sources.interfaces import IListableSource, IPreparingSource
8 from piecrust.sources.mixins import SimplePaginationSourceMixin 8 from piecrust.sources.mixins import SimplePaginationSourceMixin
39 if self.ignore_missing_dir: 39 if self.ignore_missing_dir:
40 return 40 return
41 raise InvalidFileSystemEndpointError(self.name, 41 raise InvalidFileSystemEndpointError(self.name,
42 self.fs_endpoint_path) 42 self.fs_endpoint_path)
43 43
44 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): 44 for dirpath, dirnames, filenames in osutil.walk(self.fs_endpoint_path):
45 rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path) 45 rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path)
46 dirnames[:] = list(filter(filter_page_dirname, dirnames)) 46 dirnames[:] = list(filter(filter_page_dirname, dirnames))
47 for f in sorted(filter(filter_page_filename, filenames)): 47 for f in sorted(filter(filter_page_filename, filenames)):
48 fac_path = f 48 fac_path = f
49 if rel_dirpath != '.': 49 if rel_dirpath != '.':
50 fac_path = os.path.join(rel_dirpath, f) 50 fac_path = os.path.join(rel_dirpath, f)
51
51 slug = self._makeSlug(fac_path) 52 slug = self._makeSlug(fac_path)
52 metadata = {'slug': slug} 53 metadata = {'slug': slug}
53 fac_path = fac_path.replace('\\', '/') 54 fac_path = fac_path.replace('\\', '/')
54 self._populateMetadata(fac_path, metadata) 55 self._populateMetadata(fac_path, metadata)
55 yield PageFactory(self, fac_path, metadata) 56 yield PageFactory(self, fac_path, metadata)
93 return None 94 return None
94 95
95 def listPath(self, rel_path): 96 def listPath(self, rel_path):
96 rel_path = rel_path.lstrip('\\/') 97 rel_path = rel_path.lstrip('\\/')
97 path = os.path.join(self.fs_endpoint_path, rel_path) 98 path = os.path.join(self.fs_endpoint_path, rel_path)
98 names = sorted(os.listdir(path)) 99 names = sorted(osutil.listdir(path))
99 items = [] 100 items = []
100 for name in names: 101 for name in names:
101 if os.path.isdir(os.path.join(path, name)): 102 if os.path.isdir(os.path.join(path, name)):
102 if filter_page_dirname(name): 103 if filter_page_dirname(name):
103 rel_subdir = os.path.join(rel_path, name) 104 rel_subdir = os.path.join(rel_path, name)