Skip to content

Commit

Permalink
Refactor Serials to Kinds. This has bugged me for some time since the…
Browse files Browse the repository at this point in the history
… variable nanme we use is kind for serialization kind but the namedtuple was called Serials. This realigns the naming.
  • Loading branch information
SmithSamuelM committed Jul 26, 2024
1 parent 6fae00d commit 46dfb35
Show file tree
Hide file tree
Showing 23 changed files with 242 additions and 242 deletions.
2 changes: 1 addition & 1 deletion src/keri/app/forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def handle(self, serder, attachments=None):
pevt = bytearray()
for pather, atc in attachments:
ked = pather.resolve(embeds)
sadder = coring.Sadder(ked=ked, kind=eventing.Serials.json)
sadder = coring.Sadder(ked=ked, kind=eventing.Kinds.json)
pevt.extend(sadder.raw)
pevt.extend(atc)

Expand Down
4 changes: 2 additions & 2 deletions src/keri/app/indirecting.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def on_post(self, req, rep):
rep.set_header('connection', "close")

cr = httping.parseCesrHttpRequest(req=req)
sadder = coring.Sadder(ked=cr.payload, kind=eventing.Serials.json)
sadder = coring.Sadder(ked=cr.payload, kind=eventing.Kinds.json)
msg = bytearray(sadder.raw)
msg.extend(cr.attachments.encode("utf-8"))

Expand Down Expand Up @@ -1064,7 +1064,7 @@ def on_post(self, req, rep):
rep.set_header('connection', "close")

cr = httping.parseCesrHttpRequest(req=req)
serder = serdering.SerderKERI(sad=cr.payload, kind=eventing.Serials.json)
serder = serdering.SerderKERI(sad=cr.payload, kind=eventing.Kinds.json)

pre = serder.ked["i"]
if self.aids is not None and pre not in self.aids:
Expand Down
22 changes: 11 additions & 11 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from ..kering import (Versionage, Version, Vrsn_1_0, Vrsn_2_0,
VERRAWSIZE, VERFMT, MAXVERFULLSPAN,
versify, deversify, Rever, smell)
from ..kering import (Serials, Serialage, Protocols, Protocolage, Ilkage, Ilks,
from ..kering import (Kinds, Kindage, Protocols, Protocolage, Ilkage, Ilks,
TraitDex, )

from ..help import helping
Expand Down Expand Up @@ -94,7 +94,7 @@ def sizeify(ked, kind=None, version=Version):
if not kind:
kind = knd

if kind not in Serials:
if kind not in Kinds:
raise ValueError("Invalid serialization kind = {}".format(kind))

raw = dumps(ked, kind)
Expand All @@ -118,7 +118,7 @@ def sizeify(ked, kind=None, version=Version):



def dumps(ked, kind=Serials.json):
def dumps(ked, kind=Kinds.json):
"""
utility function to handle serialization by kind
Expand All @@ -129,21 +129,21 @@ def dumps(ked, kind=Serials.json):
ked (Optional(dict, list)): key event dict or message dict to serialize
kind (str): serialization kind (JSON, MGPK, CBOR)
"""
if kind == Serials.json:
if kind == Kinds.json:
raw = json.dumps(ked, separators=(",", ":"), ensure_ascii=False).encode("utf-8")

elif kind == Serials.mgpk:
elif kind == Kinds.mgpk:
raw = msgpack.dumps(ked)

elif kind == Serials.cbor:
elif kind == Kinds.cbor:
raw = cbor.dumps(ked)
else:
raise ValueError("Invalid serialization kind = {}".format(kind))

return raw


def loads(raw, size=None, kind=Serials.json):
def loads(raw, size=None, kind=Kinds.json):
"""
utility function to handle deserialization by kind
Expand All @@ -156,21 +156,21 @@ def loads(raw, size=None, kind=Serials.json):
then consume all bytes
kind (str): serialization kind (JSON, MGPK, CBOR)
"""
if kind == Serials.json:
if kind == Kinds.json:
try:
ked = json.loads(raw[:size].decode("utf-8"))
except Exception as ex:
raise DeserializeError("Error deserializing JSON: {}"
"".format(raw[:size].decode("utf-8")))

elif kind == Serials.mgpk:
elif kind == Kinds.mgpk:
try:
ked = msgpack.loads(raw[:size])
except Exception as ex:
raise DeserializeError("Error deserializing MGPK: {}"
"".format(raw[:size]))

elif kind == Serials.cbor:
elif kind == Kinds.cbor:
try:
ked = cbor.loads(raw[:size])
except Exception as ex:
Expand Down Expand Up @@ -3747,7 +3747,7 @@ def _serialize(clas, sad: dict, kind: str = None):
otherwise default is Serials.json
"""
knd = Serials.json
knd = Kinds.json
if 'v' in sad: # versioned sad
_, _, knd, _, _ = deversify(sad['v'])

Expand Down
20 changes: 10 additions & 10 deletions src/keri/core/eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from ..help import helping

from . import coring
from .coring import (versify, Serials, Ilks, PreDex, DigDex,
from .coring import (versify, Kinds, Ilks, PreDex, DigDex,
NonTransDex,
Number, Seqner, Cigar, Dater,
Verfer, Diger, Prefixer, Tholder, Saider)
Expand Down Expand Up @@ -506,7 +506,7 @@ def state(pre,
cnfg=None, # default to []
dpre=None,
version=Version,
kind=Serials.json,
kind=Kinds.json,
intive = False,
):
"""
Expand Down Expand Up @@ -657,7 +657,7 @@ def incept(keys,
cnfg=None,
data=None,
version=Version,
kind=Serials.json,
kind=Kinds.json,
code=None,
intive=False,
delpre=None,
Expand Down Expand Up @@ -806,7 +806,7 @@ def rotate(pre,
adds=None,
data=None,
version=Version,
kind=Serials.json,
kind=Kinds.json,
intive = False,
):
"""
Expand Down Expand Up @@ -974,7 +974,7 @@ def interact(pre,
sn=1,
data=None,
version=Version,
kind=Serials.json,
kind=Kinds.json,
):
"""
Returns serder of interaction event message.
Expand Down Expand Up @@ -1020,7 +1020,7 @@ def receipt(pre,
said,
*,
version=Version,
kind=Serials.json
kind=Kinds.json
):
"""
Returns serder of event receipt message. Used for both non-trans and trans
Expand Down Expand Up @@ -1059,7 +1059,7 @@ def query(route="",
query=None,
stamp=None,
version=Version,
kind=Serials.json):
kind=Kinds.json):
"""
Returns serder of query 'qry' message.
Utility function to automate creation of query messages.
Expand Down Expand Up @@ -1117,7 +1117,7 @@ def reply(route="",
data=None,
stamp=None,
version=Version,
kind=Serials.json):
kind=Kinds.json):
"""
Returns serder of reply 'rpy' message.
Utility function to automate creation of reply messages.
Expand Down Expand Up @@ -1171,7 +1171,7 @@ def prod(route="",
query=None,
stamp=None,
version=Version,
kind=Serials.json):
kind=Kinds.json):
"""
Returns serder of prod, 'pro', msg to request disclosure via bare, 'bar' msg
of data anchored via seal(s) on KEL for identifier prefix, pre, when given
Expand Down Expand Up @@ -1215,7 +1215,7 @@ def bare(route="",
data=None,
stamp=None,
version=Version,
kind=Serials.json):
kind=Kinds.json):
"""
Returns serder of bare 'bar' message.
Utility function to automate creation of unhiding (bareing) messages for
Expand Down
12 changes: 6 additions & 6 deletions src/keri/core/scheming.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import msgpack

from . import coring
from .coring import MtrDex, Serials, Saider, Saids
from .coring import MtrDex, Kinds, Saider, Saids
from .. import help, kering
from ..kering import ValidationError, DeserializeError

Expand Down Expand Up @@ -109,7 +109,7 @@ def resolve(self, uri):

return self.resolver.resolve(uri)

def load(self, raw, kind=Serials.json):
def load(self, raw, kind=Kinds.json):
""" Schema loader
Loads schema based on kind by performing deserialization on raw bytes of schema
Expand All @@ -122,21 +122,21 @@ def load(self, raw, kind=Serials.json):
tuple: (dict, Serials, Saider) of schema
"""
if kind == Serials.json:
if kind == Kinds.json:
try:
sed = json.loads(raw.decode("utf-8"))
except Exception as ex:
raise DeserializeError("Error deserializing JSON: {} {}"
"".format(raw.decode("utf-8"), ex))

elif kind == Serials.mgpk:
elif kind == Kinds.mgpk:
try:
sed = msgpack.loads(raw)
except Exception as ex:
raise DeserializeError("Error deserializing MGPK: {} {}"
"".format(raw, ex))

elif kind == Serials.cbor:
elif kind == Kinds.cbor:
try:
sed = cbor.loads(raw)
except Exception as ex:
Expand All @@ -158,7 +158,7 @@ def load(self, raw, kind=Serials.json):
return sed, kind, saider

@staticmethod
def dump(sed, kind=Serials.json):
def dump(sed, kind=Kinds.json):
""" Serailize schema based on kind
Parameters:
Expand Down
32 changes: 16 additions & 16 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
MAXVERFULLSPAN, VER1FULLSPAN, VER2FULLSPAN)
from ..kering import SMELLSIZE, Smellage, smell

from ..kering import Protocols, Serials, versify, deversify, Ilks
from ..kering import Protocols, Kinds, versify, deversify, Ilks

from .. import help
from ..help import helping
Expand Down Expand Up @@ -379,7 +379,7 @@ class Serder:
Protocol = None # class based message protocol, None means any in Protocols is ok
Proto = Protocols.keri # default message protocol type for makify on base Serder
Vrsn = Vrsn_1_0 # default protocol version for protocol type
Kind = Serials.json # default serialization kind
Kind = Kinds.json # default serialization kind
CVrsn = Vrsn_2_0 # default CESR code table version


Expand Down Expand Up @@ -809,7 +809,7 @@ def _verify(self):
if self.kind != kind:
raise ValidationError(f"Inconsistent kind={self.kind} in {sad}.")

if self.kind in (Serials.json, Serials.cbor, Serials.mgpk):
if self.kind in (Kinds.json, Kinds.cbor, Kinds.mgpk):
if size != self.size != len(raw):
raise ValidationError(f"Inconsistent size={self.size} in {sad}.")
else: # size is not set in version string when kind is CESR
Expand Down Expand Up @@ -898,7 +898,7 @@ def makify(self, sad, *, proto=None, vrsn=None, kind=None,
ilk = (silk if silk is not None else
list(self.Fields[proto][vrsn])[0]) # list(dict) gives list of keys

if kind not in Serials:
if kind not in Kinds:
raise SerializeError(f"Invalid serialization kind = {kind}")

if ilk not in self.Fields[proto][vrsn]:
Expand Down Expand Up @@ -986,7 +986,7 @@ def makify(self, sad, *, proto=None, vrsn=None, kind=None,
raise SerializeError(f"Missing requires version string field 'v'"
f" in sad = {sad}.")

if kind in (Serials.json, Serials.cbor, Serials.mgpk):
if kind in (Kinds.json, Kinds.cbor, Kinds.mgpk):
# this size of sad needs to be computed based on actual version string span
# since not same for all versions
sad['v'] = self.Dummy * self.Spans[vrsn] # ensure span of vs is dummied MAXVERFULLSPAN
Expand Down Expand Up @@ -1015,7 +1015,7 @@ def makify(self, sad, *, proto=None, vrsn=None, kind=None,
sad[label] = dig

raw = self.dumps(sad, kind=kind, proto=proto, vrsn=vrsn) # compute final raw
if kind == Serials.cesr:# cesr kind version string does not set size
if kind == Kinds.cesr:# cesr kind version string does not set size
size = len(raw) # size of whole message

self._raw = raw
Expand Down Expand Up @@ -1074,7 +1074,7 @@ def _inhale(self, raw, *, smellage=None):



def loads(self, raw, size=None, kind=Serials.json):
def loads(self, raw, size=None, kind=Kinds.json):
"""method to handle deserialization by kind
assumes already sniffed and smelled to determine
serialization size and kind
Expand All @@ -1093,21 +1093,21 @@ def loads(self, raw, size=None, kind=Serials.json):
Notes:
loads of json uses str whereas loads of cbor and msgpack use bytes
"""
if kind == Serials.json:
if kind == Kinds.json:
try:
sad = json.loads(raw[:size].decode("utf-8"))
except Exception as ex:
raise DeserializeError(f"Error deserializing JSON: "
f"{raw[:size].decode('utf-8')}") from ex

elif kind == Serials.mgpk:
elif kind == Kinds.mgpk:
try:
sad = msgpack.loads(raw[:size])
except Exception as ex:
raise DeserializeError(f"Error deserializing MGPK: "
f"{raw[:size].decode('utf-8')}") from ex

elif kind == Serials.cbor:
elif kind == Kinds.cbor:
try:
sad = cbor.loads(raw[:size])
except Exception as ex:
Expand Down Expand Up @@ -1152,7 +1152,7 @@ def _exhale(self, sad):

raw = self.dumps(sad, kind)

if kind in (Serials.cesr): # cesr kind version string does not set size
if kind in (Kinds.cesr): # cesr kind version string does not set size
size = len(raw) # size of whole message

# must call .verify to ensure these are compatible
Expand All @@ -1164,7 +1164,7 @@ def _exhale(self, sad):
self._size = size


def dumps(self, sad=None, kind=Serials.json, proto=None, vrsn=None):
def dumps(self, sad=None, kind=Kinds.json, proto=None, vrsn=None):
"""Method to handle serialization by kind
Assumes sad fields are properly filled out for serialization kind.
Expand All @@ -1187,17 +1187,17 @@ def dumps(self, sad=None, kind=Serials.json, proto=None, vrsn=None):
"""
sad = sad if sad is not None else self.sad

if kind == Serials.json:
if kind == Kinds.json:
raw = json.dumps(sad, separators=(",", ":"),
ensure_ascii=False).encode("utf-8")

elif kind == Serials.mgpk:
elif kind == Kinds.mgpk:
raw = msgpack.dumps(sad)

elif kind == Serials.cbor:
elif kind == Kinds.cbor:
raw = cbor.dumps(sad)

elif kind == Serials.cesr: # does not support list only dict
elif kind == Kinds.cesr: # does not support list only dict
raw = self._dumps(sad, proto=proto, vrsn=vrsn)

else:
Expand Down
Loading

0 comments on commit 46dfb35

Please sign in to comment.