Esempio n. 1
0
 def testLabel(self):
     labels = self._get_labels()
     c = Concept(1, labels=labels)
     self.assertEqual(label(labels), c.label())
     self.assertEqual(label(labels, 'nl'), c.label('nl'))
     self.assertEqual(label(labels, 'en'), c.label('en'))
     self.assertEqual(label(labels, None), c.label(None))
Esempio n. 2
0
 def test_concept_adapter(self):
     from pyramid_skosprovider.renderers import concept_adapter
     c = Concept(id=larch['id'],
                 labels=larch['labels'],
                 notes=larch['notes'],
                 sources=larch['sources'],
                 concept_scheme=trees.concept_scheme,
                 matches=larch['matches'])
     request = testing.DummyRequest()
     m = Mock()
     request.skos_registry = m
     request.locale_name = 'nl'
     concept = concept_adapter(c, request)
     self.assertIsInstance(concept, dict)
     self.assertEqual(concept['id'], 1)
     self.assertIn('uri', concept)
     self.assertIsInstance(concept['label'], text_type)
     self.assertIn(concept['type'], 'concept')
     self.assertEqual(len(concept['labels']), 3)
     self._assert_is_labels(concept['labels'])
     assert 'matches' in concept
     assert 0 == len(concept['matches']['broad'])
     assert 0 == len(concept['matches']['narrow'])
     assert 0 == len(concept['matches']['related'])
     assert 0 == len(concept['matches']['exact'])
     assert 1 == len(concept['matches']['close'])
     assert 'subordinate_arrays' in concept
     assert 0 == len(concept['subordinate_arrays'])
     assert 'sources' in concept
     assert 1 == len(concept['sources'])
     assert 'label' in concept
     assert concept['label'] == 'De Lariks'
Esempio n. 3
0
 def test_concept_adapter(self):
     from pyramid_skosprovider.utils import concept_adapter
     c = Concept(
         id=larch['id'],
         labels=larch['labels'],
         notes=larch['notes'],
         concept_scheme=trees.concept_scheme,
         matches=larch['matches']
     )
     concept = concept_adapter(c, {})
     self.assertIsInstance(concept, dict)
     self.assertEqual(concept['id'], 1)
     self.assertIn('uri', concept)
     self.assertIsInstance(concept['label'], text_type)
     self.assertIn(concept['type'], 'concept')
     self.assertEqual(len(concept['labels']), 2)
     self._assert_is_labels(concept['labels'])
     assert 'matches' in concept
     assert 0 == len(concept['matches']['broad'])
     assert 0 == len(concept['matches']['narrow'])
     assert 0 == len(concept['matches']['related'])
     assert 0 == len(concept['matches']['exact'])
     assert 1 == len(concept['matches']['close'])
     assert 'subordinate_arrays' in concept
     assert 0 == len(concept['subordinate_arrays'])
Esempio n. 4
0
 def testSortKey(self):
     labels = self._get_labels()
     sl = Label('allereerste', type='sortLabel', language='nl-BE')
     labels.append(sl)
     c = Concept(1, labels=labels)
     self.assertEqual('allereerste', c._sortkey('sortlabel'))
     self.assertEqual('allereerste', c._sortkey('sortlabel', 'nl'))
     self.assertEqual('knocke-heyst', c._sortkey('sortlabel', 'en'))
     self.assertEqual('', c._sortkey('uri'))
Esempio n. 5
0
def from_thing(thing):
    """
        Map a :class:`skosprovider_sqlalchemy.models.Thing` to a
        :class:`skosprovider.skos.Concept` or
        a :class:`skosprovider.skos.Collection`, depending on the type.

        :param skosprovider_sqlalchemy.models.Thing thing: Thing to map.
        :rtype: :class:`~skosprovider.skos.Concept` or
            :class:`~skosprovider.skos.Collection`.
        """
    if thing.type and thing.type == 'collection':
        return Collection(
            id=thing.concept_id,
            uri=thing.uri,
            concept_scheme=ConceptScheme(thing.conceptscheme.uri),
            labels=[
                Label(l.label, l.labeltype_id, l.language_id)
                for l in thing.labels
            ],
            sources=[
                Source(s.citation)
                for s in thing.sources
            ],
            members=[member.concept_id for member in thing.members] if hasattr(thing, 'members') else [],
            member_of=[c.concept_id for c in thing.member_of],
            superordinates=[broader_concept.concept_id for broader_concept in thing.broader_concepts]
        )
    else:
        matches = {}
        for m in thing.matches:
            key = m.matchtype.name[:m.matchtype.name.find('Match')]
            if key not in matches:
                matches[key] = []
            matches[key].append(m.uri)
        return Concept(
            id=thing.concept_id,
            uri=thing.uri,
            concept_scheme=ConceptScheme(thing.conceptscheme.uri),
            labels=[
                Label(l.label, l.labeltype_id, l.language_id)
                for l in thing.labels
            ],
            notes=[
                Note(n.note, n.notetype_id, n.language_id)
                for n in thing.notes
            ],
            sources=[
                Source(s.citation)
                for s in thing.sources
            ],
            broader=[c.concept_id for c in thing.broader_concepts],
            narrower=[c.concept_id for c in thing.narrower_concepts],
            related=[c.concept_id for c in thing.related_concepts],
            member_of=[c.concept_id for c in thing.member_of],
            subordinate_arrays=[narrower_collection.concept_id for narrower_collection in thing.narrower_collections],
            matches=matches,
        )
Esempio n. 6
0
 def testIn(self):
     c = Concept(1)
     assert hasattr(c, 'id')
     assert hasattr(c, 'uri')
     assert hasattr(c, 'labels')
     assert hasattr(c, 'notes')
     assert hasattr(c, 'broader')
     assert hasattr(c, 'narrower')
     assert hasattr(c, 'related')
     assert hasattr(c, 'member_of')
Esempio n. 7
0
 def testMatches(self):
     c = Concept(
         1,
         uri='urn:x-skosprovider:gemeenten:1',
         matches={'broad': ['http://id.something.org/provincies/1']})
     assert 'close' in c.matches
     assert 'exact' in c.matches
     assert 'broad' in c.matches
     assert 'narrow' in c.matches
     assert 'related' in c.matches
     assert ['http://id.something.org/provincies/1'] == c.matches['broad']
Esempio n. 8
0
 def test_concept_adapter_en(self):
     from pyramid_skosprovider.renderers import concept_adapter
     c = Concept(
         id=larch['id'],
         labels=larch['labels'],
         concept_scheme=trees.concept_scheme,
     )
     request = testing.DummyRequest()
     m = Mock()
     request.skos_registry = m
     request.locale_name = 'en'
     concept = concept_adapter(c, request)
     assert 'label' in concept
     assert concept['label'] == 'The Larch'
Esempio n. 9
0
    def _from_graph(self):
        clist = []
        for sub, pred, obj in self.graph.triples(
            (None, RDF.type, SKOS.Concept)):
            uri = self.to_text(sub)
            matches = {}
            for k in Concept.matchtypes:
                matches[k] = self._create_from_subject_predicate(
                    sub, URIRef(SKOS + k + 'Match'))
            con = Concept(
                id=self._get_id_for_subject(sub, uri),
                uri=uri,
                concept_scheme=self.concept_scheme,
                labels=self._create_from_subject_typelist(
                    sub, Label.valid_types),
                notes=self._create_from_subject_typelist(
                    sub, Note.valid_types),
                sources=self._create_sources(sub),
                broader=self._create_from_subject_predicate(sub, SKOS.broader),
                narrower=self._create_from_subject_predicate(
                    sub, SKOS.narrower),
                related=self._create_from_subject_predicate(sub, SKOS.related),
                member_of=[],
                subordinate_arrays=self._create_from_subject_predicate(
                    sub, SKOS_THES.subordinateArray),
                matches=matches)
            clist.append(con)

        for sub, pred, obj in self.graph.triples(
            (None, RDF.type, SKOS.Collection)):
            uri = self.to_text(sub)
            col = Collection(
                id=self._get_id_for_subject(sub, uri),
                uri=uri,
                concept_scheme=self.concept_scheme,
                labels=self._create_from_subject_typelist(
                    sub, Label.valid_types),
                notes=self._create_from_subject_typelist(
                    sub, (Note.valid_types)),
                sources=self._create_sources(sub),
                members=self._create_from_subject_predicate(sub, SKOS.member),
                member_of=[],
                superordinates=self._create_from_subject_predicate(
                    sub, SKOS_THES.superOrdinate))
            clist.append(col)
        self._fill_member_of(clist)
        return clist
Esempio n. 10
0
 def test_json_collection(self):
     from pyramid_skosprovider.renderers import json_renderer
     c = Collection(id=species['id'],
                    uri=species['uri'],
                    labels=species['labels'],
                    notes=species['notes'],
                    members=[larch['id']],
                    concept_scheme=trees.concept_scheme)
     r = json_renderer({})
     m = Mock()
     config = {
         'get_provider.return_value.get_by_id.return_value':
         Concept(id=larch['id'], uri=larch['uri'], labels=larch['labels'])
     }
     m.configure_mock(**config)
     request = testing.DummyRequest()
     request.skos_registry = m
     sys = {}
     sys['request'] = request
     jsonstring = r(c, sys)
     coll = json.loads(jsonstring)
     assert isinstance(coll, dict)
     assert coll['id'] == 3
     assert coll['uri'] == 'http://python.com/trees/species'
     assert isinstance(coll['label'], text_type)
     assert coll['type'] == 'collection'
     assert isinstance(coll['labels'], list)
     assert len(coll['labels']) == 3
     for l in coll['labels']:
         assert 'label' in l
         assert 'type' in l
         assert 'language' in l
     assert len(coll['notes']) == 1
     for n in coll['notes']:
         assert 'note' in n
         assert 'type' in n
         assert 'language' in n
         assert 'markup' in n
     assert len(coll['members']) == 1
     m = coll['members'][0]
     assert 'id' in m
     assert 'uri' in m
     assert 'type' in m
     assert 'label' in m
     assert 'matches' not in coll
Esempio n. 11
0
 def test_json_concept(self):
     from pyramid_skosprovider.renderers import json_renderer
     c = Concept(id=larch['id'],
                 uri=larch['uri'],
                 labels=larch['labels'],
                 notes=larch['notes'],
                 concept_scheme=trees.concept_scheme,
                 matches=larch['matches'])
     r = json_renderer({})
     request = testing.DummyRequest()
     m = Mock()
     request.skos_registry = m
     sys = {}
     sys['request'] = request
     jsonstring = r(c, sys)
     concept = json.loads(jsonstring)
     self.assertIsInstance(concept, dict)
     self.assertEqual(concept['id'], 1)
     self.assertEqual(concept['uri'], 'http://python.com/trees/larch')
     self.assertIsInstance(concept['label'], text_type)
     self.assertEqual(concept['type'], 'concept')
     self.assertIsInstance(concept['labels'], list)
     self.assertEqual(len(concept['labels']), 3)
     for l in concept['labels']:
         self.assertIsInstance(l, dict)
         self.assertIn('label', l)
         self.assertIn('type', l)
         self.assertIn('language', l)
     self.assertIsInstance(concept['notes'], list)
     self.assertEqual(len(concept['notes']), 1)
     for n in concept['notes']:
         self.assertIsInstance(n, dict)
         self.assertIn('note', n)
         self.assertIn('type', n)
         self.assertIn('language', n)
         self.assertIn('markup', n)
     self.assertIsInstance(concept['broader'], list)
     self.assertIsInstance(concept['related'], list)
     self.assertIsInstance(concept['narrower'], list)
     assert 'matches' in concept
     for mt in ['broad', 'narrow', 'related', 'close', 'exact']:
         assert mt in concept['matches']
         assert list == type(concept['matches'][mt])
     assert 1 == len(concept['matches']['close'])
Esempio n. 12
0
def things_from_graph(graph, concept_scheme):
    '''
    Read concepts and collections from a graph.

    :param rdflib.Graph graph: Graph to read from.
    :param skosprovider.skos.ConceptScheme concept_scheme: Conceptscheme the
        concepts and collections belong to.
    :rtype: :class:`list`
    '''
    clist = []
    valid_label_types = Label.valid_types[:]
    valid_label_types.remove('sortLabel')
    for sub, pred, obj in graph.triples((None, RDF.type, SKOS.Concept)):
        uri = str(sub)
        con = Concept(
            id=_split_uri(uri, 1),
            uri=uri,
            concept_scheme=concept_scheme,
            labels=_create_from_subject_typelist(graph, sub,
                                                 valid_label_types),
            notes=_create_from_subject_typelist(graph, sub, Note.valid_types),
            broader=_create_from_subject_predicate(graph, sub, SKOS.broader),
            narrower=_create_from_subject_predicate(graph, sub, SKOS.narrower),
            related=_create_from_subject_predicate(graph, sub, SKOS.related),
            subordinate_arrays=[])
        clist.append(con)

        # at this moment, Heritagedata does not support SKOS.Collection
    # for sub, pred, obj in graph.triples((None, RDF.type, SKOS.Collection)):
    # uri = str(sub)
    #     col = Collection(_split_uri(uri, 1), uri=uri)
    #     col.members = _create_from_subject_predicate(sub, SKOS.member)
    #     col.labels = _create_from_subject_typelist(sub, Label.valid_types)
    #     col.notes = _create_from_subject_typelist(sub, Note.valid_types)
    #     clist.append(col)

    return clist
Esempio n. 13
0
 def testSource(self):
     c = Concept(id=1, sources=[{'citation': 'My citation'}])
     self.assertEqual(1, len(c.sources))
     self.assertIsInstance(c.sources[0], Source)
     self.assertEqual('My citation', c.sources[0].citation)
Esempio n. 14
0
 def testMemberOf(self):
     c = Concept(1, uri='urn:x-skosprovider:gemeenten:1', member_of=[15])
     self.assertEqual(set([15]), set(c.member_of))
Esempio n. 15
0
 def testUri(self):
     c = Concept(1, uri='urn:x-skosprovider:gemeenten:1')
     self.assertEqual(1, c.id)
     self.assertEqual('urn:x-skosprovider:gemeenten:1', c.uri)
    def _from_thing(self, thing):
        '''
        Load one concept or collection from the database.

        :param :class:`skosprovider_sqlalchemy.models.Thing` thing: Thing
            to load.
        '''
        if thing.type and thing.type == 'collection':
            return Collection(
                id=thing.concept_id,
                uri=thing.uri
                if thing.uri is not None else self.uri_generator.generate(
                    type='collection', id=thing.concept_id),
                concept_scheme=self.concept_scheme,
                labels=[
                    Label(l.label, l.labeltype_id, l.language_id)
                    for l in thing.labels
                ],
                notes=[
                    Note(n.note, n.notetype_id, n.language_id, n.markup)
                    for n in thing.notes
                ],
                sources=[Source(s.citation, s.markup) for s in thing.sources],
                members=[member.concept_id for member in thing.members]
                if hasattr(thing, 'members') else [],
                member_of=[
                    member_of.concept_id for member_of in thing.member_of
                ],
                superordinates=[
                    broader_concept.concept_id
                    for broader_concept in thing.broader_concepts
                ])
        else:
            matches = {}
            for m in thing.matches:
                key = m.matchtype.name[:m.matchtype.name.find('Match')]
                if not key in matches:
                    matches[key] = []
                matches[key].append(m.uri)
            return Concept(
                id=thing.concept_id,
                uri=thing.uri
                if thing.uri is not None else self.uri_generator.generate(
                    type='concept', id=thing.concept_id),
                concept_scheme=self.concept_scheme,
                labels=[
                    Label(l.label, l.labeltype_id, l.language_id)
                    for l in thing.labels
                ],
                notes=[
                    Note(n.note, n.notetype_id, n.language_id, n.markup)
                    for n in thing.notes
                ],
                sources=[Source(s.citation, s.markup) for s in thing.sources],
                broader=[c.concept_id for c in thing.broader_concepts],
                narrower=[c.concept_id for c in thing.narrower_concepts],
                related=[c.concept_id for c in thing.related_concepts],
                member_of=[
                    member_of.concept_id for member_of in thing.member_of
                ],
                subordinate_arrays=[
                    narrower_collection.concept_id
                    for narrower_collection in thing.narrower_collections
                ],
                matches=matches)
Esempio n. 17
0
 def testRepr(self):
     c = Concept(1)
     self.assertEquals("Concept('1')", c.__repr__())