| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import os |
| 21 |
import functools |
| 22 |
import itertools |
| 23 |
import logging |
| 24 |
from copy import deepcopy |
| 25 |
|
| 26 |
from rdflib.namespace import DC, RDF, RDFS, OWL, Namespace |
| 27 |
|
| 28 |
logger = logging.getLogger(__name__) |
| 29 |
|
| 30 |
DEBUG_ENABLED = int(os.environ.get('CORAS_PARSER_DEBUG', 0)) |
| 31 |
|
| 32 |
VS = Namespace('http://www.w3.org/2003/06/sw-vocab-status/ns#') |
| 33 |
META = { |
| 34 |
DC.description, |
| 35 |
DC.title, |
| 36 |
(RDF.type, RDF.Property), |
| 37 |
RDFS.comment, |
| 38 |
RDFS.label, |
| 39 |
RDFS.isDefinedBy, |
| 40 |
OWL.versionInfo, |
| 41 |
(RDF.type, OWL.AnnotationProperty), |
| 42 |
(RDF.type, OWL.Ontology), |
| 43 |
VS.term_status, |
| 44 |
} |
| 45 |
|
| 46 |
def register(f): |
| 47 |
@functools.wraps(f) |
| 48 |
def func(graph, *args): |
| 49 |
start_debug(graph) |
| 50 |
result = f(graph, *args) |
| 51 |
end_debug() |
| 52 |
return result |
| 53 |
return func |
| 54 |
|
| 55 |
def start_debug(graph): |
| 56 |
global GRAPH |
| 57 |
GRAPH = deepcopy(graph) |
| 58 |
|
| 59 |
def triples(f): |
| 60 |
global GRAPH |
| 61 |
|
| 62 |
@functools.wraps(f) |
| 63 |
def func(*args): |
| 64 |
triples = f(*args) |
| 65 |
|
| 66 |
triples, to_remove = itertools.tee(triples, 2) |
| 67 |
for t in to_remove: |
| 68 |
GRAPH.remove(t) |
| 69 |
|
| 70 |
return triples |
| 71 |
|
| 72 |
return func |
| 73 |
|
| 74 |
def end_debug(): |
| 75 |
global GRAPH |
| 76 |
|
| 77 |
for s, p, o in GRAPH: |
| 78 |
prefix = 'meta' if p in META or (p, o) in META else 'unsupported' |
| 79 |
logger.debug('{}: {}, {}, {}'.format(prefix, s, p, o)) |
| 80 |
del GRAPH |
| 81 |
|
| 82 |
|