-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Versa Literate (Markdown) reader fixes. add versa.util.lookup(). Add …
…NTriples & CSV writers.
- Loading branch information
Showing
8 changed files
with
161 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from amara3 import iri | ||
|
||
from . import iriref | ||
from . import I, VERSA_BASEIRI | ||
|
||
RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' | ||
RDFS_NS = 'http://www.w3.org/2000/01/rdf-schema#' | ||
|
||
VERSA_TYPE_REL = I(VERSA_BASEIRI + 'type') | ||
RDF_TYPE_REL = I(RDF_NS + 'type') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#http://legacy.python.org/dev/peps/pep-0440/ | ||
version_info = ('0', '3', '6') | ||
version_info = ('0', '3', '7') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#versa.writer.ntriples | ||
""" | ||
Render a Versa vocab model as CSV, using a given set of ruls to flatten | ||
Import as: | ||
from versa.writer import csv as vcsv | ||
""" | ||
|
||
import logging | ||
|
||
from amara3 import iri | ||
|
||
from versa import I, VERSA_BASEIRI, ORIGIN, RELATIONSHIP, TARGET | ||
from versa.terms import VERSA_BASEIRI, RDF_NS, RDFS_NS, VERSA_TYPE_REL, RDF_TYPE_REL | ||
from versa.util import all_origins, lookup, resourcetypes | ||
|
||
|
||
def fromlist(l): | ||
return '|'.join(l) | ||
|
||
|
||
def write(models, csvout, rulelist, write_header, base=None, logger=logging): | ||
''' | ||
models - one or more input Versa models from which output is generated. | ||
''' | ||
properties = [ k for (k, v) in rulelist ] | ||
numprops = len(properties) | ||
headers = [ v for (k, v) in rulelist ] | ||
if write_header: | ||
csvout.writerow(['id', 'type'] + headers) | ||
|
||
rules = { k: v for (k, v) in rulelist } | ||
|
||
if not isinstance(models, list): models = [models] | ||
for m in models: | ||
for rid in all_origins(m): | ||
#print(rid, list(m.match(rid, RDF_TYPE_REL))) | ||
rtypes = list(lookup(m, rid, RDF_TYPE_REL)) | ||
#if not rtypes: rtypes = list(lookup(m, rid, VERSA_TYPE_REL)) | ||
#Ignore if no type | ||
if not rtypes: continue | ||
row = [rid, fromlist(rtypes)] + [None] * numprops | ||
for ix, p in enumerate(properties): | ||
#v = next(lookup(m, rid, RDF_TYPE_REL), None) | ||
v = list(lookup(m, rid, p)) | ||
if v: | ||
row[ix + 2] = fromlist(v) | ||
csvout.writerow(row) | ||
|
||
return | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#versa.writer.ntriples | ||
""" | ||
Render a Versa vocab model as NTriples | ||
https://www.w3.org/TR/rdf-testcases/#ntriples | ||
""" | ||
|
||
from amara3 import iri | ||
|
||
from versa import I, VERSA_BASEIRI, ORIGIN, RELATIONSHIP, TARGET | ||
from versa.terms import VERSA_BASEIRI, RDF_NS, RDFS_NS, VERSA_TYPE_REL, RDF_TYPE_REL | ||
from versa.driver import memory | ||
from versa import VERSA_BASEIRI | ||
|
||
RESOURCE_MAPPING = { | ||
I(VERSA_BASEIRI + 'Resource'): I(RDFS_NAMESPACE + 'Class'), | ||
I(VERSA_BASEIRI + 'Property'): I(RDF_NAMESPACE + 'Property'), | ||
I(VERSA_BASEIRI + 'description'): I(RDFS_NAMESPACE + 'comment'), | ||
I(VERSA_BASEIRI + 'label'): I(RDFS_NAMESPACE + 'label'), | ||
} | ||
|
||
|
||
def strconv(item): | ||
''' | ||
Prepare a statement into a triple ready for rdflib | ||
''' | ||
if isinstance(item, I): | ||
return('<' + str(item) + '>') | ||
else: | ||
return('"' + str(item) + '"') | ||
|
||
|
||
def write(models, out=None, base=None, logger=logging): | ||
''' | ||
models - one or more input Versa models from which output is generated. | ||
''' | ||
assert out is not None #Output stream required | ||
if not isinstance(models, list): models = [models] | ||
for m in models: | ||
for link in m.match(): | ||
s, p, o = link[:3] | ||
#Skip docheader statements | ||
if s == (base or '') + '@docheader': continue | ||
if p in RESOURCE_MAPPING: p = RESOURCE_MAPPING[p] | ||
if o in RESOURCE_MAPPING: o = RESOURCE_MAPPING[o] | ||
|
||
if p == VERSA_TYPE_REL: p = RDF_TYPE_REL | ||
print(strconv(s), strconv(p), strconv(o), '.', file=out) | ||
return | ||
|
Empty file.