Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 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 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 get_asset(self, path):
"""Loads an asset by path."""
clean_path = cleanup_path(path).strip('/')
nodes = [self.asset_root] + self.theme_asset_roots
for node in nodes:
for piece in clean_path.split('/'):
node = node.get_child(piece)
if node is None:
break
if node is not None:
return node
return None
'explicit page number lookups. You '
'need to one or the other.')
if not allow_virtual:
return None
path, virtual_path = path.split('@', 1)
rv = self.get(path, alt=alt, page_num=page_num,
persist=persist)
if rv is None:
return None
return self.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 = self.cache.get(path, alt, virtual_path)
if rv is not Ellipsis:
if rv is not None:
self.db.track_record_dependency(rv)
return rv
raw_data = self.db.load_raw_data(path, alt=alt)
if raw_data is None:
self.cache.remember_as_missing(path, alt, virtual_path)
return None
rv = self.instance_from_data(raw_data, page_num=page_num)
def alt_exists(self, path, alt=PRIMARY_ALT, fallback=False):
"""Checks if an alt exists."""
path = cleanup_path(path)
if '@' in path:
return False
# If we find the path in the cache, check if it was loaded from
# the right source alt.
rv = self.get(path, alt)
if rv is not None:
if rv['_source_alt'] == alt:
return True
elif (fallback or
(rv['_source_alt'] == PRIMARY_ALT and
alt == self.config.primary_alternative)):
return True
return False
return False
def load_raw_data(self, path, alt=PRIMARY_ALT, cls=None,
fallback=True):
"""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()
rv_type = None
choiceiter = _iter_filename_choices(fn_base, [alt], self.config,
fallback=fallback)
for fs_path, source_alt, is_attachment in choiceiter:
# If we already determined what our return value is but the
# type mismatches what we try now, we have to abort. Eg:
# a page can not become an attachment or the other way round.
if rv_type is not None and rv_type != is_attachment:
break
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`.
"""
pieces = 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.')