Exemple #1
0
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)
Exemple #2
0
 def test_source_should_encode_full(self):
     source = IdentifiableSource('the_source', 'The Source')
     source.author = 'The Author'
     source.publisher = 'The Publisher'
     source.date = Date(2000, 1, 1)
     source.contained_by = IdentifiableSource(
         'the_containing_source', 'The Containing Source')
     link = Link('https://example.com/the-source')
     link.label = 'The Source Online'
     source.links.add(link)
     source.contains.append(
         IdentifiableSource('the_contained_source', 'The Contained Source'))
     IdentifiableCitation('the_citation', source)
     expected = {
         '$schema': '/schema.json#/definitions/source',
         '@context': {
             'name': 'https://schema.org/name',
         },
         '@type': 'https://schema.org/Thing',
         'id': 'the_source',
         'name': 'The Source',
         'author': 'The Author',
         'publisher': 'The Publisher',
         'contains': [
             '/en/source/the_contained_source/index.json',
         ],
         'citations': [
             '/en/citation/the_citation/index.json',
         ],
         'containedBy': '/en/source/the_containing_source/index.json',
         'date': {
             'year': 2000,
             'month': 1,
             'day': 1,
         },
         'links': [
             {
                 'url': '/en/source/the_source/index.json',
                 'relationship': 'canonical',
                 'mediaType': 'application/json',
             },
             {
                 'url': '/nl/source/the_source/index.json',
                 'relationship': 'alternate',
                 'locale': 'nl-NL',
             },
             {
                 'url': '/en/source/the_source/index.html',
                 'relationship': 'alternate',
                 'mediaType': 'text/html',
             },
             {
                 'url': 'https://example.com/the-source',
                 'label': 'The Source Online',
             },
         ],
     }
     self.assert_encodes(expected, source, 'source')
Exemple #3
0
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)
Exemple #4
0
 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')
Exemple #5
0
 async def test_populate_link_should_set_label(self, expected: str,
                                               label: Optional[str],
                                               m_retriever) -> None:
     link = Link('http://en.wikipedia.org/wiki/Amsterdam')
     link.label = label
     entry = Entry('en', 'The_city_of_Amsterdam', 'The city of Amsterdam',
                   'Amsterdam, such a lovely place!')
     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', entry)
     self.assertEqual(expected, link.label)
Exemple #6
0
 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
Exemple #7
0
 def test_place_should_encode_full(self):
     place_id = 'the_place'
     name = 'The Place'
     locale = 'nl-NL'
     latitude = 12.345
     longitude = -54.321
     coordinates = Point(latitude, longitude)
     place = Place(place_id, [PlaceName(name, locale)])
     place.coordinates = coordinates
     Enclosure(place, Place('the_enclosing_place', []))
     Enclosure(Place('the_enclosed_place', []), place)
     link = Link('https://example.com/the-place')
     link.label = 'The Place Online'
     place.links.add(link)
     place.events.append(IdentifiableEvent('E1', Birth()))
     expected = {
         '$schema':
         '/schema.json#/definitions/place',
         '@context': {
             'enclosedBy': 'https://schema.org/containedInPlace',
             'encloses': 'https://schema.org/containsPlace',
             'events': 'https://schema.org/event',
             'coordinates': 'https://schema.org/geo',
         },
         '@type':
         'https://schema.org/Place',
         'id':
         place_id,
         'names': [
             {
                 'name': name,
                 'locale': 'nl-NL',
             },
         ],
         'events': [
             '/en/event/E1/index.json',
         ],
         'links': [
             {
                 'url': '/en/place/the_place/index.json',
                 'relationship': 'canonical',
                 'mediaType': 'application/json',
             },
             {
                 'url': '/nl/place/the_place/index.json',
                 'relationship': 'alternate',
                 'locale': 'nl-NL',
             },
             {
                 'url': '/en/place/the_place/index.html',
                 'relationship': 'alternate',
                 'mediaType': 'text/html',
             },
             {
                 'url': 'https://example.com/the-place',
                 'label': 'The Place Online',
             },
         ],
         'coordinates': {
             '@context': {
                 'latitude': 'https://schema.org/latitude',
                 'longitude': 'https://schema.org/longitude',
             },
             '@type': 'https://schema.org/GeoCoordinates',
             'latitude': latitude,
             'longitude': longitude,
         },
         'encloses': [
             '/en/place/the_enclosed_place/index.json',
         ],
         'enclosedBy': [
             '/en/place/the_enclosing_place/index.json',
         ],
     }
     self.assert_encodes(expected, place, 'place')
Exemple #8
0
    def test_person_should_encode_full(self):
        parent_id = 'the_parent'
        parent = Person(parent_id)

        child_id = 'the_child'
        child = Person(child_id)

        sibling_id = 'the_sibling'
        sibling = Person(sibling_id)
        sibling.parents.append(parent)

        person_id = 'the_person'
        person_affiliation_name = 'Person'
        person_individual_name = 'The'
        person = Person(person_id)
        person.names.append(
            PersonName(person_individual_name, person_affiliation_name))
        person.parents.append(parent)
        person.children.append(child)
        person.private = False
        link = Link('https://example.com/the-person')
        link.label = 'The Person Online'
        person.links.add(link)
        person.citations.append(
            IdentifiableCitation('the_citation', Source('The Source')))
        Presence(person, Subject(), IdentifiableEvent('the_event', Birth()))

        expected = {
            '$schema':
            '/schema.json#/definitions/person',
            '@context': {
                'parents': 'https://schema.org/parent',
                'children': 'https://schema.org/child',
                'siblings': 'https://schema.org/sibling',
            },
            '@type':
            'https://schema.org/Person',
            'id':
            person_id,
            'names': [
                {
                    '@context': {
                        'individual': 'https://schema.org/givenName',
                        'affiliation': 'https://schema.org/familyName',
                    },
                    'individual': person_individual_name,
                    'affiliation': person_affiliation_name,
                },
            ],
            'parents': [
                '/en/person/the_parent/index.json',
            ],
            'children': [
                '/en/person/the_child/index.json',
            ],
            'siblings': [
                '/en/person/the_sibling/index.json',
            ],
            'private':
            False,
            'presences': [
                {
                    '@context': {
                        'event': 'https://schema.org/performerIn',
                    },
                    'role': 'subject',
                    'event': '/en/event/the_event/index.json',
                },
            ],
            'citations': [
                '/en/citation/the_citation/index.json',
            ],
            'links': [
                {
                    'url': '/en/person/the_person/index.json',
                    'relationship': 'canonical',
                    'mediaType': 'application/json',
                },
                {
                    'url': '/nl/person/the_person/index.json',
                    'relationship': 'alternate',
                    'locale': 'nl-NL',
                },
                {
                    'url': '/en/person/the_person/index.html',
                    'relationship': 'alternate',
                    'mediaType': 'text/html',
                },
                {
                    'url': 'https://example.com/the-person',
                    'label': 'The Person Online',
                },
            ],
        }
        self.assert_encodes(expected, person, 'person')