def _load_urls(owner: HasLinks, element: ElementTree.Element): url_elements = _xpath(element, './ns:url') for url_element in url_elements: link = Link(str(url_element.get('href'))) link.relationship = 'external' link.label = url_element.get('description') owner.links.add(link)
def _parse_urls(owner: HasLinks, element: Element): url_elements = _xpath(element, './ns:url') for url_element in url_elements: link = Link(str(_xpath1(url_element, './@href'))) link.relationship = 'external' description_element = _xpath1(url_element, './@description') if description_element is not None: link.label = str(description_element) owner.links.add(link)
def _encode_identifiable_resource( self, encoded: Dict, resource: Union[Identifiable, Resource]) -> None: if 'links' not in encoded: encoded['links'] = [] canonical = Link(self._generate_url(resource)) canonical.relationship = 'canonical' canonical.media_type = 'application/json' encoded['links'].append(canonical) for locale in self._site.configuration.locales: if locale == self._locale: continue translation = Link(self._generate_url(resource, locale=locale)) translation.relationship = 'alternate' translation.locale = locale encoded['links'].append(translation) html = Link(self._generate_url(resource, media_type='text/html')) html.relationship = 'alternate' html.media_type = 'text/html' encoded['links'].append(html)
async def test_populate_link_should_set_relationship( self, expected: str, relationship: Optional[str], m_retriever) -> None: link = Link('http://en.wikipedia.org/wiki/Amsterdam') link.relationship = relationship with TemporaryDirectory() as output_directory_path: with TemporaryDirectory() as cache_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.cache_directory_path = cache_directory_path async with Site(configuration) as site: sut = _Populator(site, m_retriever) await sut.populate_link(link, 'en') self.assertEqual(expected, link.relationship)
def test_link_should_encode_full(self) -> None: link = Link('https://example.com') link.label = 'The Link' link.relationship = 'external' link.locale = 'nl-NL' link.media_type = MediaType('text/html') expected = { 'url': 'https://example.com', 'relationship': 'external', 'label': 'The Link', 'locale': 'nl-NL', 'mediaType': 'text/html', } self.assert_encodes(expected, link, 'link')
async def populate_link(self, link: Link, site: Site, entry_language: str, entry: Optional[Entry] = None) -> None: if link.url.startswith('http:'): link.url = 'https:' + link.url[5:] if link.media_type is None: link.media_type = 'text/html' if link.relationship is None: link.relationship = 'external' if link.locale is None: link.locale = entry_language if link.description is None: # There are valid reasons for links in locales that aren't supported. with suppress(ValueError): async with site.with_locale(link.locale): link.description = _('Read more on Wikipedia.') if entry is not None and link.label is None: link.label = entry.title