#! /usr/bin/python """NTriples Parsing Stuff""" import string, re, urllib import thing import notation3 # N3 parsers and generators, and RDF generator import md5, binascii # for building md5 URIs from thing import * # Magic resources we know about LITERAL_URI_prefix = "data:application/n3;" DAML_LISTS = notation3.DAML_LISTS RDF_type_URI = notation3.RDF_type_URI DAML_equivalentTo_URI = notation3.DAML_equivalentTo_URI NTP_NS_URI = "http://infomesh.net/2002/ntp#" ############################################################################## # # N T P B U I L T - I N s # # This should be in a separate module, imported and called once by the user # to register the code with the store # # Light Built-in classes class BI_findall(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): import re st, reg = subj_py res = list(re.compile(reg).findall(st)) return store._fromPython(context, res) def termToCWM(store, term): from llyn import LITERAL, RESOURCE if term[0] == '"': return store.intern((LITERAL, term[1:-1])) elif term[0] == '<': return store.intern((RESOURCE, term[1:-1])) class BI_strToTriple(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): import re a = r'(<.*?>|_:\S+|\?\S+|"(?:\\"|[^"])*")[ \t]' rt = r'[ \t]*%s%s%s*.[ \t]*' % (a, a, a) x = list(re.compile(rt).findall(str(subj_py))) if len(x) > 0: if 0: print 'triples:::', x for t in x: store.storeQuad((context, # Store as P, S, O termToCWM(store, t[1]), termToCWM(store, t[0]), termToCWM(store, t[2]))) return None # Register the string built-ins with the store def isString(x): return type(x) is type('') or type(x) is type(u'') def register(store): str = store.internURI(NTP_NS_URI[:-1]) str.internFrag("findall", BI_findall) str.internFrag("strToTriple", BI_strToTriple)