#!/usr/bin/python
""""""
__author__ = 'Sean B. Palmer'
__license__ = 'Copyright (C) 2001 Sean B. Palmer. GNU GPL 2'
import re, string, random
S, P, O = 0, 1, 2
class Article:
def __init__(self, s):
s, self.repr = str(s), str(s)
if s[0] == '"': self.val, self.type = s[1:-1], 'Literal'
elif s[0] == '?': self.val, self.type = s[1:], 'Univar'
elif s[0] == '_': self.val, self.type = s[2:], 'Exivar'
elif s[0] == '<': self.val, self.type = s[1:-1], 'URI'
else: self.val, self.type, self.repr = s, 'URI', '<%s>' % s
def __repr__(self): return self.repr
def set(self, s): self.__init__(s)
def parse(s, t=[]):
if len(s) == 0: raise 'Document has no content'
rc, a = re.compile(r'(\#[^\n]*)'), r'(<.*?>|_:\S+|\?\S+|"(?:\\"|[^"])*")[ \t]'
rw, rt, y = re.compile(r'[ \t]+'), r'[ \t]*%s%s%s*.[ \t]*' % (a, a, a), t[:]
for line in s.replace('\r\n', '\n').replace('\r', '\n').split('\n'):
if re.compile(rt).match(line):
x = re.compile(rt).findall(line)[0]
y.append([Article(x[0]), Article(x[1]), Article(x[2])])
elif rc.match(line) or rw.match(line) or (len(line) == 0): continue
else: raise 'Line is invalid', line
return y
def serialize(triples):
result = ''
for triple in triples:
result += '%s %s %s .\n' % (triple[S], triple[P], triple[O])
return result.rstrip()
def u(uri): return '<'+uri+'>'
def b(label=''):
if label == '': # 208,827,064,576 combinations
for x in range(8): label += string.lowercase[random.randrange(0, 25)]
return '_:'+label
else: return '_:'+label
def l(s): return '"'+s+'"'
def contains(triple, store):
result = 0
for t in store:
if ((triple[0].repr == t[0].repr) and
(triple[1].repr == t[1].repr) and
(triple[2].repr == t[2].repr)): result = 1
return result
class Namespace:
def __init__(self, ns='', sep=''):
self.ns, self.sep = ns, sep
def __getattr__(self, name):
return self.ns+self.sep+name
def remove(store, rt):
result = []
for t in store:
if ((t[0].repr != rt[0].repr) or (t[1].repr != rt[1].repr)
or (t[2].repr != rt[2].repr)): result.append(t)
return result
def test():
print serialize(parse('
_:q ?r .\n "x"\t"y" <#z> . '))
if __name__=="__main__":
print __doc__