def testIsValidMarkup(self): self.assertTrue(Source.is_valid_markup('HTML')) self.assertFalse(Source.is_valid_markup('markdown')) citation = 'Van Daele, K; Meganck, L. & Mortier, S. 2015. Data-driven systems and system-driven data: the story of the Flanders Heritage Inventory (1995-2015)' s = Source( citation ) self.assertTrue(s.is_valid_markup(None))
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, )
def _get_concept_scheme(self): ''' Find a :class:`skosprovider.skos.ConceptScheme` for this provider. :param id: Id of a conceptscheme. :rtype: :class:`skosprovider.skos.ConceptScheme` ''' csm = self.session\ .query(ConceptSchemeModel)\ .options(joinedload('labels'))\ .options(joinedload('notes'))\ .options(joinedload('languages'))\ .options(joinedload('sources'))\ .get(self.conceptscheme_id) return ConceptScheme( uri=csm.uri, labels=[ Label(l.label, l.labeltype_id, l.language_id) for l in csm.labels ], notes=[ Note(n.note, n.notetype_id, n.language_id, n.markup) for n in csm.notes ], languages=[l.id for l in csm.languages], sources=[Source(s.citation, s.markup) for s in csm.sources])
def test_source_adapter(self): from pyramid_skosprovider.renderers import source_adapter s = Source('<em>My citation</em>', 'HTML') source = source_adapter(s, Mock()) assert isinstance(source, dict) assert 'citation' in source assert 'markup' in source assert source['markup'] == 'HTML'
def _create_sources(self, subject): ''' Create the sources for this subject. :param subject: Subject to get the sources for. :returns: A :class:`list` of :class:`skosprovider.skos.Source` objects. ''' ret = [] for s, p, o in self.graph.triples((subject, DCTERMS.source, None)): for si, pi, oi in self.graph.triples( (o, DCTERMS.bibliographicCitation, None)): ret.append( Source(self.to_text(oi), 'HTML' if oi.datatype == RDF.HTML else None)) return ret
def testDictToSourceWithSource(self): citation = 'Van Daele, K; Meganck, L. & Mortier, S. 2015. Data-driven systems and system-driven data: the story of the Flanders Heritage Inventory (1995-2015)' s = dict_to_source(Source(citation)) self.assertEqual(citation, s.citation)
def testConstructorInvalidMarkup(self): with self.assertRaises(ValueError): citation = 'Van Daele, K; Meganck, L. & Mortier, S. 2015. <em>Data-driven systems and system-driven data: the story of the Flanders Heritage Inventory (1995-2015)</em>' s = Source(citation, markup='markdown')
def testIsValidMarkup(self): self.assertTrue(Source.is_valid_markup('HTML')) self.assertFalse(Source.is_valid_markup('markdown')) citation = 'Van Daele, K; Meganck, L. & Mortier, S. 2015. Data-driven systems and system-driven data: the story of the Flanders Heritage Inventory (1995-2015)' s = Source(citation) self.assertTrue(s.is_valid_markup(None))
def testConstructorWithHTML(self): citation = 'Van Daele, K; Meganck, L. & Mortier, S. 2015. <em>Data-driven systems and system-driven data: the story of the Flanders Heritage Inventory (1995-2015)</em>' s = Source(citation, markup='HTML') self.assertEqual(citation, s.citation) self.assertEqual('HTML', s.markup)
def testConstructor(self): citation = 'Van Daele, K; Meganck, L. & Mortier, S. 2015. Data-driven systems and system-driven data: the story of the Flanders Heritage Inventory (1995-2015)' s = Source(citation) self.assertEqual(citation, s.citation)
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)
from skosprovider.skos import ConceptScheme, Label, Note, Source from skosprovider_rdf.utils import rdf_dumper ifile = open(os.path.join(os.path.dirname(__file__), 'data', 'menu.csv'), "r") reader = csv.reader(ifile) csvprovider = SimpleCsvProvider( {'id': 'MENU'}, reader, uri_generator=UriPatternGenerator('http://id.python.org/menu/%s'), concept_scheme=ConceptScheme( uri='http://id.python.org/menu', labels=[ Label(type='prefLabel', language='en', label='A pythonesque menu.') ], notes=[ Note( type='changeNote', language='en', note= "<strong>We didn't need no change notes when I was younger.</strong>", markup='HTML') ], sources=[Source("Monthy Python's Flying Circus, 1970. Spam.")])) graph = rdf_dumper(csvprovider) print graph.serialize(format='n3')