Fix/core save api (#172)

* Acknowledge messaages from Pulsar, doh!
* Change API to deliver a boolean e if value is an entity
* Change loaders to use new API
* Changes, entity-aware API is complete
This commit is contained in:
cybermaggedon 2024-11-26 16:46:38 +00:00 committed by GitHub
parent 340d7a224f
commit 887fafcf8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 104 additions and 70 deletions

View file

@ -4,7 +4,7 @@ import json
import dataclasses
import base64
from trustgraph.knowledge import hash
from trustgraph.knowledge import hash, Uri, Literal
class ProtocolException(Exception):
pass
@ -12,14 +12,6 @@ class ProtocolException(Exception):
class ApplicationException(Exception):
pass
class Uri(str):
def is_uri(self): return True
def is_literal(self): return False
class Literal(str):
def is_uri(self): return False
def is_literal(self): return True
@dataclasses.dataclass
class Triple:
s : str
@ -213,9 +205,16 @@ class Api:
"limit": limit
}
if s: input["s"] = s
if p: input["p"] = p
if o: input["o"] = o
if not isinstance(s, Uri):
raise RuntimeError("s must be Uri")
if not isinstance(p, Uri):
raise RuntimeError("p must be Uri")
if not isinstance(o, Uri) and not isinstance(o, Literal):
raise RuntimeError("o must be Uri or Literal")
if s: input["s"] = { "v": str(s), "e": isinstance(s, Uri), }
if p: input["p"] = { "v": str(p), "e": isinstance(p, Uri), }
if o: input["o"] = { "v": str(o), "e": isinstance(o, Uri), }
url = f"{self.url}triples-query"
@ -273,9 +272,9 @@ class Api:
if metadata:
metadata.emit(
lambda t: triples.append({
"s": t.s.value,
"p": t.p.value,
"o": t.o.value
"s": { "v": t["s"], "e": isinstance(t["s"], Uri) },
"p": { "v": t["p"], "e": isinstance(t["p"], Uri) },
"o": { "v": t["o"], "e": isinstance(t["o"], Uri) }
})
)
@ -312,9 +311,9 @@ class Api:
if metadata:
metadata.emit(
lambda t: triples.append({
"s": t.s.value,
"p": t.p.value,
"o": t.o.value
"s": { "v": t["s"], "e": isinstance(t["s"], Uri) },
"p": { "v": t["p"], "e": isinstance(t["p"], Uri) },
"o": { "v": t["o"], "e": isinstance(t["o"], Uri) }
})
)

View file

@ -1,4 +1,5 @@
from . defs import *
from . identifier import *
from . publication import *
from . document import *

View file

@ -23,3 +23,11 @@ URL = 'https://schema.org/url'
IDENTIFIER = 'https://schema.org/identifier'
KEYWORD = 'https://schema.org/keywords'
class Uri(str):
def is_uri(self): return True
def is_literal(self): return False
class Literal(str):
def is_uri(self): return False
def is_literal(self): return True

View file

@ -1,6 +1,16 @@
from . defs import *
from .. schema import Triple, Value
def Value(value, is_uri):
if is_uri:
return Uri(value)
else:
return Literal(value)
def Triple(s, p, o):
return {
"s": s, "p": p, "o": o,
}
class DigitalDocument:

View file

@ -1,6 +1,16 @@
from . defs import *
from .. schema import Triple, Value
def Value(value, is_uri):
if is_uri:
return Uri(value)
else:
return Literal(value)
def Triple(s, p, o):
return {
"s": s, "p": p, "o": o,
}
class Organization:
def __init__(self, id, name=None, description=None):

View file

@ -1,6 +1,16 @@
from . defs import *
from .. schema import Triple, Value
def Value(value, is_uri):
if is_uri:
return Uri(value)
else:
return Literal(value)
def Triple(s, p, o):
return {
"s": s, "p": p, "o": o,
}
class PublicationEvent:
def __init__(