Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_all_absolute(cat):
for root, catalogs, items in cat.walk():
for link in root.links:
self.assertTrue(link.link_type == LinkType.ABSOLUTE)
self.assertTrue(is_absolute_href(link.get_href()))
for item in items:
for link in item.links:
self.assertTrue(link.link_type == LinkType.ABSOLUTE)
self.assertTrue(is_absolute_href(link.get_href()))
def check_all_relative(cat):
for root, catalogs, items in cat.walk():
for link in root.links:
if link.rel != 'self':
# print(l.rel)
self.assertTrue(link.link_type == LinkType.RELATIVE)
self.assertFalse(is_absolute_href(link.get_href()))
for item in items:
for link in item.links:
if link.rel != 'self':
self.assertTrue(link.link_type == LinkType.RELATIVE)
self.assertFalse(is_absolute_href(link.get_href()))
def test_is_absolute_href(self):
# Test cases of (href, expected)
test_cases = [('item.json', False), ('./item.json', False), ('../item.json', False),
('/item.json', True), ('http://stacspec.org/item.json', True)]
for href, expected in test_cases:
actual = is_absolute_href(href)
self.assertEqual(actual, expected)
def normalize_hrefs(self, root_href):
# Normalizing requires an absolute path
if not is_absolute_href(root_href):
root_href = make_absolute_href(root_href, os.getcwd(), start_is_dir=True)
# Fully resolve the STAC to avoid linking issues.
# This particularly can happen with unresolved links that have
# relative paths.
self.fully_resolve()
for child in self.get_children():
child_root = os.path.join(root_href, '{}/'.format(child.id))
child.normalize_hrefs(child_root)
for item in self.get_items():
item_root = os.path.join(root_href, '{}'.format(item.id))
item.normalize_hrefs(item_root)
self.set_self_href(os.path.join(root_href, self.DEFAULT_FILE_NAME))
def normalize_hrefs(self, root_href):
if not is_absolute_href(root_href):
root_href = make_absolute_href(root_href, os.getcwd(), start_is_dir=True)
old_self_href = self.get_self_href()
new_self_href = os.path.join(root_href, '{}.json'.format(self.id))
self.set_self_href(new_self_href)
# Make sure relative asset links remain valid.
# This will only work if there is a self href set.
for asset in self.assets.values():
asset_href = asset.href
if not is_absolute_href(asset_href):
if old_self_href is not None:
abs_href = make_absolute_href(asset_href, old_self_href)
new_relative_href = make_relative_href(abs_href, new_self_href)
asset.href = new_relative_href
def get_absolute_href(self):
"""Gets the absolute href for this asset, if possible.
If this Asset has no associated Item, this will return whatever the
href is (as it cannot determine the absolute path, if the asset
href is relative).
Returns:
str: The absolute HREF of this asset, or a relative HREF is an abslolute HREF
cannot be determined.
"""
if not is_absolute_href(self.href):
if self.owner is not None:
return make_absolute_href(self.href, self.owner.get_self_href())
return self.href
def get_href(self):
"""Gets the HREF for this link.
Returns:
str: Returns this link's HREF. If the link type is LinkType.RELATIVE,
and there is an owner of the link, then the HREF returned will be
relative. In all other cases, this method will return an absolute HREF.
"""
if self.link_type == LinkType.RELATIVE:
if self.is_resolved():
href = self.target.get_self_href()
else:
href = self.target
if is_absolute_href(href) and self.owner is not None:
href = make_relative_href(href, self.owner.get_self_href())
else:
href = self.get_absolute_href()
return href
d (dict): The dict that represents the Link in JSON
Returns:
Link: Link instance constructed from the dict.
"""
d = copy(d)
rel = d.pop('rel')
href = d.pop('href')
media_type = d.pop('type', None)
title = d.pop('title', None)
properties = None
if any(d):
properties = d
if rel == 'self' or is_absolute_href(href):
link_type = LinkType.ABSOLUTE
else:
link_type = LinkType.RELATIVE
return Link(rel=rel,
target=href,
media_type=media_type,
title=title,
properties=properties,
link_type=link_type)