def findPageFactory(self, metadata, mode): uri_path = metadata.get('slug', '') if not uri_path: uri_path = '_index' path = os.path.join(self.fs_endpoint_path, uri_path) _, ext = os.path.splitext(path) if mode == MODE_CREATING: if ext == '': path = '%s.%s' % (path, self.default_auto_format) rel_path = os.path.relpath(path, self.fs_endpoint_path) rel_path = rel_path.replace('\\', '/') self._populateMetadata(rel_path, metadata, mode) return PageFactory(self, rel_path, metadata) if ext == '': paths_to_check = [ '%s.%s' % (path, e) for e in self.supported_extensions ] else: paths_to_check = [path] for path in paths_to_check: if os.path.isfile(path): rel_path = os.path.relpath(path, self.fs_endpoint_path) rel_path = rel_path.replace('\\', '/') self._populateMetadata(rel_path, metadata, mode) return PageFactory(self, rel_path, metadata) return None
def listPath(self, rel_path): rel_path = rel_path.lstrip('\\/') path = self._getFullPath(rel_path) names = self._sortFilenames(os.listdir(path)) items = [] for name in names: if os.path.isdir(os.path.join(path, name)): if self._filterPageDirname(name): rel_subdir = os.path.join(rel_path, name) items.append((True, name, rel_subdir)) else: if self._filterPageFilename(name): slug = self._makeSlug(os.path.join(rel_path, name)) metadata = {'slug': slug} fac_path = name if rel_path != '.': fac_path = os.path.join(rel_path, name) fac_path = fac_path.replace('\\', '/') self._populateMetadata(fac_path, metadata) fac = PageFactory(self, fac_path, metadata) name, _ = os.path.splitext(name) items.append((False, name, fac)) return items
def buildPageFactories(self): logger.debug("Scanning for pages in: %s" % self.fs_endpoint_path) if not os.path.isdir(self.fs_endpoint_path): raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path) for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path) dirnames[:] = list(filter(filter_page_dirname, dirnames)) # If `capture_mode` is `dirname`, we don't need to recompute it # for each filename, so we do it here. if self.capture_mode == 'dirname': config = self._extractConfigFragment(rel_dirpath) for f in filter(filter_page_filename, filenames): if self.capture_mode == 'path': path = os.path.join(rel_dirpath, f) config = self._extractConfigFragment(path) elif self.capture_mode == 'filename': config = self._extractConfigFragment(f) fac_path = f if rel_dirpath != '.': fac_path = os.path.join(rel_dirpath, f) slug = self._makeSlug(fac_path) metadata = {'slug': slug, 'config': config} yield PageFactory(self, fac_path, metadata)
def _makeFactory(self, path, slug, year, month, day): path = path.replace('\\', '/') timestamp = datetime.date(year, month, day) metadata = { 'slug': slug, 'year': year, 'month': month, 'day': day, 'date': timestamp} return PageFactory(self, path, metadata)
def findPageFactory(self, metadata, mode): # Pages from this source are effectively flattened, so we need to # find pages using a brute-force kinda way. for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): for f in filenames: slug, _ = os.path.splitext(f) if slug == metadata['slug']: path = os.path.join(dirpath, f) rel_path = os.path.relpath(path, self.fs_endpoint_path) config = self._extractConfigFragment(rel_path) metadata = {'slug': slug, 'config': config} return PageFactory(self, rel_path, metadata) return None
def findPageFactory(self, metadata, mode): uri_path = metadata.get('slug', '') if uri_path == '': uri_path = '_index' path = self.fs_endpoint_path uri_parts = uri_path.split('/') for i, p in enumerate(uri_parts): if i == len(uri_parts) - 1: # Last part, this is the filename. We need to check for either # the name, or the name with the prefix, but also handle a # possible extension. p_pat = r'(\d+_)?' + re.escape(p) _, ext = os.path.splitext(uri_path) if ext == '': p_pat += r'\.[\w\d]+' found = False for name in os.listdir(path): if re.match(p_pat, name): path = os.path.join(path, name) found = True break if not found: return None else: # Find each sub-directory. It can either be a directory with # the name itself, or the name with a number prefix. p_pat = r'(\d+_)?' + re.escape(p) found = False for name in os.listdir(path): if re.match(p_pat, name): path = os.path.join(path, name) found = True break if not found: return None fac_path = os.path.relpath(path, self.fs_endpoint_path) config = self._extractConfigFragment(fac_path) metadata = {'slug': uri_path, 'config': config} return PageFactory(self, fac_path, metadata)
def buildPageFactories(self): logger.debug("Scanning for pages in: %s" % self.fs_endpoint_path) if not os.path.isdir(self.fs_endpoint_path): if self.ignore_missing_dir: return raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path) for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path) dirnames[:] = list(filter(filter_page_dirname, dirnames)) for f in sorted(filter(filter_page_filename, filenames)): fac_path = f if rel_dirpath != '.': fac_path = os.path.join(rel_dirpath, f) slug = self._makeSlug(fac_path) metadata = {'slug': slug} fac_path = fac_path.replace('\\', '/') self._populateMetadata(fac_path, metadata) yield PageFactory(self, fac_path, metadata)
def listPath(self, rel_path): rel_path = rel_path.lstrip('\\/') path = os.path.join(self.fs_endpoint_path, rel_path) names = sorted(os.listdir(path)) items = [] for name in names: if os.path.isdir(os.path.join(path, name)): if filter_page_dirname(name): rel_subdir = os.path.join(rel_path, name) items.append((True, name, rel_subdir)) else: if filter_page_filename(name): cur_rel_path = os.path.join(rel_path, name) slug = self._makeSlug(cur_rel_path) config = self._extractConfigFragment(cur_rel_path) metadata = {'slug': slug, 'config': config} fac = PageFactory(self, cur_rel_path, metadata) name, _ = os.path.splitext(name) items.append((False, name, fac)) return items
def listPath(self, rel_path): rel_path = rel_path.lstrip('/') path = self.fs_endpoint_path if rel_path != '': parts = rel_path.split('/') for p in parts: p_pat = r'(\d+_)?' + re.escape(p) + '$' for name in os.listdir(path): if re.match(p_pat, name): path = os.path.join(path, name) break else: raise Exception("No such path: %s" % rel_path) items = [] names = sorted(os.listdir(path)) for name in names: clean_name = self.re_pattern.sub('', name) clean_name, _ = os.path.splitext(clean_name) if os.path.isdir(os.path.join(path, name)): if filter_page_dirname(name): rel_subdir = os.path.join(rel_path, name) items.append((True, clean_name, rel_subdir)) else: if filter_page_filename(name): slug = self._makeSlug(os.path.join(rel_path, name)) fac_path = name if rel_path != '.': fac_path = os.path.join(rel_path, name) fac_path = fac_path.replace('\\', '/') config = self._extractConfigFragment(fac_path) metadata = {'slug': slug, 'config': config} fac = PageFactory(self, fac_path, metadata) name, _ = os.path.splitext(name) items.append((False, clean_name, fac)) return items
def getFactory(self): return PageFactory(self.source, self.rel_path, copy.deepcopy(self.metadata))
def load_factory(app, info): source = app.getSource(info['source_name']) return PageFactory(source, info['rel_path'], info['metadata'])
def findPageFactory(self, metadata, mode): year = metadata.get('year') month = metadata.get('month') day = metadata.get('day') slug = metadata.get('slug') try: if year is not None: year = int(year) if month is not None: month = int(month) if day is not None: day = int(day) except ValueError: return None ext = metadata.get('ext') if ext is None: if len(self.supported_extensions) == 1: ext = self.supported_extensions[0] elif mode == MODE_CREATING and self.default_auto_format: ext = self.default_auto_format replacements = { 'year': '%04d' % year if year is not None else None, 'month': '%02d' % month if month is not None else None, 'day': '%02d' % day if day is not None else None, 'slug': slug, 'ext': ext } needs_recapture = False if year is None: needs_recapture = True replacements['year'] = '????' if month is None: needs_recapture = True replacements['month'] = '??' if day is None: needs_recapture = True replacements['day'] = '??' if slug is None: needs_recapture = True replacements['slug'] = '*' if ext is None: needs_recapture = True replacements['ext'] = '*' path = os.path.normpath(os.path.join( self.fs_endpoint_path, self.path_format % replacements)) if needs_recapture: if mode == MODE_CREATING: raise ValueError("Not enough information to find a post path.") possible_paths = osutil.glob(path) if len(possible_paths) != 1: return None path = possible_paths[0] elif mode == MODE_PARSING and not os.path.isfile(path): return None rel_path = os.path.relpath(path, self.fs_endpoint_path) rel_path = rel_path.replace('\\', '/') fac_metadata = self._parseMetadataFromPath(rel_path) return PageFactory(self, rel_path, fac_metadata)