WIP rdf graph processing library

This commit is contained in:
lash 2023-04-30 07:38:42 +01:00
parent 929a042c95
commit d4a1df3ec9
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 51 additions and 25 deletions

View File

@ -1,17 +1,44 @@
# standard imports
import sys
import enum
# external imports
from rdflib import Graph
from rdflib import Graph, RDF
from rdflib.term import URIRef
from rdflib.collection import Collection
class CIC(enum.Enum):
voucherContract = URIRef('https://defalsify.org/rdf-eth/0.1/voucherContract')
voucherTransfer = URIRef('https://defalsify.org/rdf-eth/0.1/voucherTransfer')
class Cert:
def __init__(self, path):
def __init__(self):
self.g = Graph()
self.g.parse(path, format='turtle')
self.txs = {}
self.typ = None
@classmethod
def from_path(cls, path):
c = cls()
c.g.parse(path, format='turtle')
c.__apply_graph()
return c
def __apply_graph(self):
current_subject = None
for (s, p, o) in self.g:
if s != current_subject:
current_subject = s
if p == RDF.first:
pass
print('{} {} {}'.format(s, p, o))
def export(self, w=sys.stdout):
v = self.g.serialize(format='xml')
w.write(v)

View File

@ -4,29 +4,24 @@ import json
import os
# external imports
from jsonschema import validate as json_validate
#from jsonschema import validate as json_validate
# local imports
from cic_schema.nft import NFT
test_dir = os.path.realpath(os.path.dirname(__file__))
testdata_dir = os.path.join(test_dir, 'testdata')
data_dir = os.path.join(test_dir, '..', 'cic_schema', 'data')
#data_dir = os.path.join(test_dir, '..', 'cic_schema', 'data')
class TestJsonData(unittest.TestCase):
def setUp(self):
fp = os.path.join(data_dir, 'cic.json')
f = open(fp, 'r')
self.schema = json.load(f)
f.close()
self.path = os.path.join(testdata_dir, 'test_valid.json')
self.nft = NFT.from_path(self.path)
def test_schema_load(self):
fp = os.path.join(testdata_dir, 'test_valid.json')
f = open(fp, 'r')
o = json.load(f)
f.close()
validator = json_validate(o, self.schema)
def test_valid(self):
pass
if __name__ == '__main__':

View File

@ -19,14 +19,15 @@ data_dir = os.path.join(test_dir, '..', 'cic_schema', 'data')
class TestNft(unittest.TestCase):
def setUp(self):
fp = os.path.join(testdata_dir, 'test_voucher.rdf')
self.graph_voucher = Cert(fp)
fp = os.path.join(testdata_dir, 'test_tx.rdf')
self.graph_tx = Cert(fp)
#fp = os.path.join(testdata_dir, 'test_voucher.rdf')
#self.graph_voucher = Cert.from_path(fp)
fp = os.path.join(testdata_dir, 'test_tx2.rdf')
self.graph_tx = Cert.from_path(fp)
def test_simplest(self):
print(self.graph_tx.export())
pass
if __name__ == '__main__':

View File

@ -1,12 +1,15 @@
@prefix evm: <https://defalsify.org/rdf-eth/0.1/#> .
@prefix evm: <https://defalsify.org/rdf-eth/0.1/> .
@prefix cic: <https://cicnet.org/rdf/0.1/> .
@prefix ge: <https://grassrootseconomics.net/rdf/0.1/ge#> .
@prefix redcross: <https://redcross.com/cic-def/0.1/> .
@prefix un: <https://un.com/esg/0.1/> .
@prefix un: <https://un.com/cic/0.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
evm:voucherTransfer
evm:id "6f4ed36c11345a9a48353cd2f93f1f92958c96df15f3112a192bc994250e8d03"^^xsd:hexBinary ;
(
"6f4ed36c11345a9a48353cd2f93f1f92958c96df15f3112a192bc994250e8d03"^^evm:id
"7f4ed36c11345a9a48353cd2f93f1f92958c96df15f3112a192bc994250e8d03"^^evm:id
"8f4ed36c11345a9a48353cd2f93f1f92958c96df15f3112a192bc994250e8d03"^^evm:id
)
ge:approvedActor <URN:uuid:18daebc7-23b9-41e4-af00-2559f7840f78> ;
redcross:interest ge:foodforest ;
un:esg "deforestation" ;