def is_child_of(self, path, strict=False): """Checks if the current object is a child of the passed object or path. """ if isinstance(path, SourceObject): path = path.path if self.path is None or path is None: return False this_path = cleanup_path(self.path).split('/') crumbs = cleanup_path(path).split('/') return this_path[:len(crumbs)] == crumbs and \ (not strict or len(this_path) > len(crumbs))
def test_cleanup_path(): assert cleanup_path("/") == "/" assert cleanup_path("/foo") == "/foo" assert cleanup_path("/foo/") == "/foo" assert cleanup_path("/////foo/") == "/foo" assert cleanup_path("/////foo////") == "/foo" assert cleanup_path("/////foo/.///") == "/foo" assert cleanup_path("/////foo/..///") == "/foo" assert cleanup_path("/foo/./bar/") == "/foo/bar" assert cleanup_path("/foo/../bar/") == "/foo/bar"
def test_cleanup_path(): from lektor.utils import cleanup_path assert cleanup_path('/') == '/' assert cleanup_path('/foo') == '/foo' assert cleanup_path('/foo/') == '/foo' assert cleanup_path('/////foo/') == '/foo' assert cleanup_path('/////foo////') == '/foo' assert cleanup_path('/////foo/.///') == '/foo' assert cleanup_path('/////foo/..///') == '/foo' assert cleanup_path('/foo/./bar/') == '/foo/bar' assert cleanup_path('/foo/../bar/') == '/foo/bar'
def test_cleanup_path(): from lektor.utils import cleanup_path assert cleanup_path("/") == "/" assert cleanup_path("/foo") == "/foo" assert cleanup_path("/foo/") == "/foo" assert cleanup_path("/////foo/") == "/foo" assert cleanup_path("/////foo////") == "/foo" assert cleanup_path("/////foo/.///") == "/foo" assert cleanup_path("/////foo/..///") == "/foo" assert cleanup_path("/foo/./bar/") == "/foo/bar" assert cleanup_path("/foo/../bar/") == "/foo/bar"
def resolve_url_path(self, url_path, include_invisible=False, include_assets=True, alt_fallback=True): """Given a URL path this will find the correct record which also might be an attachment. If a record cannot be found or is unexposed the return value will be `None`. """ clean_path = cleanup_path(url_path).strip('/') # Split off the alt and if no alt was found, point it to the # primary alternative. If the clean path comes back as `None` # then the config does not include a rooted alternative and we # have to skip handling of regular records. alt, clean_path = _split_alt_from_url(self.db.config, clean_path) if clean_path is not None: if not alt: if alt_fallback: alt = self.db.config.primary_alternative or PRIMARY_ALT else: alt = PRIMARY_ALT node = self.get_root(alt=alt) if node is None: raise RuntimeError('Tree root could not be found.') pieces = clean_path.split('/') if pieces == ['']: pieces = [] rv = node.resolve_url_path(pieces) if rv is not None and (include_invisible or rv.is_visible): return rv if include_assets: return self.asset_root.resolve_url_path(pieces)
def load_raw_data(self, path, alt=PRIMARY_ALT, cls=None): """Internal helper that loads the raw record data. This performs very little data processing on the data. """ path = cleanup_path(path) if cls is None: cls = dict fn_base = self.to_fs_path(path) rv = cls() choiceiter = _iter_filename_choices(fn_base, [alt], self.config) for fs_path, source_alt, is_attachment in choiceiter: try: with open(fs_path, 'rb') as f: for key, lines in metaformat.tokenize(f, encoding='utf-8'): rv[key] = u''.join(lines) except IOError as e: if e.errno not in (errno.ENOTDIR, errno.ENOENT): raise if not is_attachment or not os.path.isfile(fs_path[:-3]): continue rv = {} rv['_path'] = path rv['_id'] = posixpath.basename(path) rv['_gid'] = hashlib.md5(path.encode('utf-8')).hexdigest() rv['_alt'] = alt rv['_source_alt'] = source_alt if is_attachment: rv['_attachment_for'] = posixpath.dirname(path) return rv
def load_raw_data(self, path, cls=None): """Internal helper that loads the raw record data. This performs very little data processing on the data. """ path = cleanup_path(path) if cls is None: cls = dict fn_base = self.to_fs_path(path) rv = cls() for fs_path, is_attachment in _iter_filename_choices(fn_base): try: with open(fs_path, 'rb') as f: for key, lines in metaformat.tokenize(f, encoding='utf-8'): rv[key] = u''.join(lines) except IOError as e: if e.errno not in (errno.ENOTDIR, errno.ENOENT): raise if not is_attachment or not os.path.isfile(fs_path[:-3]): continue rv = {} rv['_path'] = path rv['_id'] = posixpath.basename(path) rv['_gid'] = hashlib.md5(path.encode('utf-8')).hexdigest() if is_attachment: rv['_attachment_for'] = posixpath.dirname(path) return rv
def edit(self, path, is_attachment=None, datamodel=None): """Edits a record by path.""" path = cleanup_path(path) return make_editor_session(self, path, is_attachment=is_attachment, datamodel=datamodel)
def get_asset(self, path): """Loads an asset by path.""" clean_path = cleanup_path(path).strip('/') node = self.asset_root for piece in clean_path.split('/'): node = node.get_child(piece) if node is None: break return node
def pad_get(pad, path, alt=PRIMARY_ALT, page_num=None, persist=True, allow_virtual=True): virt_markers = path.count('@') # If the virtual marker is included, we also want to look up the # virtual path below an item. Special case: if virtual paths are # not allowed but one was passed, we just return `None`. if virt_markers == 1: if page_num is not None: raise RuntimeError('Cannot use both virtual paths and ' 'explicit page number lookups. You ' 'need to one or the other.') if not allow_virtual: return None path, virtual_path = path.split('@', 1) rv = pad_get(pad, path, alt=alt, page_num=page_num, persist=persist) if rv is None: return None return pad.get_virtual(rv, virtual_path) # Sanity check: there must only be one or things will get weird. elif virt_markers > 1: return None path = cleanup_path(path) virtual_path = None if page_num is not None: virtual_path = str(page_num) rv = pad.cache.get(path, alt, virtual_path) if rv is not Ellipsis: if rv is not None: #pad.db.track_record_dependency(rv) pass return rv raw_data = pad.db.load_raw_data(path, alt=alt) if raw_data is None: pad.cache.remember_as_missing(path, alt, virtual_path) return None rv = pad.instance_from_data(raw_data, page_num=page_num) if persist: pad.cache.persist(rv) else: pad.cache.remember(rv) #return self.db.track_record_dependency(rv) return rv
def resolve_url_path(self, url_path, include_invisible=False, include_assets=True): """Given a URL path this will find the correct record which also might be an attachment. If a record cannot be found or is unexposed the return value will be `None`. """ node = self.root pieces = cleanup_path(url_path).strip('/').split('/') if pieces == ['']: pieces = [] rv = node.resolve_url_path(pieces) if rv is not None and (include_invisible or rv.is_visible): return rv if include_assets: return self.asset_root.resolve_url_path(pieces)
def is_child_of(self, path): this_path = cleanup_path(self['_path']).split('/') crumbs = cleanup_path(path).split('/') return this_path[:len(crumbs)] == crumbs
def is_child_of(self, path): if isinstance(path, Record): path = path['_path'] this_path = cleanup_path(self['_path']).split('/') crumbs = cleanup_path(path).split('/') return this_path[:len(crumbs)] == crumbs
def edit(self, path, is_attachment=None, alt=PRIMARY_ALT, datamodel=None): """Edits a record by path.""" return make_editor_session(self.pad, cleanup_path(path), alt=alt, is_attachment=is_attachment, datamodel=datamodel)