Skip to content

Commit

Permalink
refactor Bexter to add class Method so external Labeler can decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
SmithSamuelM committed Aug 10, 2024
1 parent 6be8d8f commit 8c038c9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 13 deletions.
101 changes: 93 additions & 8 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2013,7 +2013,7 @@ class Tagger(Matter):
composable (bool): True when .qb64b and .qb2 are 24 bit aligned and round trip
Properties:
tag (str): B64 primitive without prepad (alias of .soft)
tag (str): B64 .soft portion of code but without prepad
Inherited Hidden: (See Matter)
Expand Down Expand Up @@ -2660,14 +2660,15 @@ def _rawify(self, bext):
raw = decodeB64(base)[ls:] # convert and remove leader
return raw # raw binary equivalent of text

@property
def bext(self):
"""
Property bext: Base64 text value portion of qualified b64 str
Returns the value portion of .qb64 with text code and leader removed
@classmethod
def _decode(cls, raw, code):
"""Returns decoded raw as B64 str aka bext value
Returns:
bext (str): decoded raw as B64 str aka bext value
"""
_, _, _, _, ls = self.Sizes[self.code]
bext = encodeB64(bytes([0] * ls) + self.raw)
_, _, _, _, ls = cls.Sizes[code]
bext = encodeB64(bytes([0] * ls) + raw)
ws = 0
if ls == 0 and bext:
if bext[0] == ord(b'A'): # strip leading 'A' zero pad
Expand All @@ -2677,6 +2678,24 @@ def bext(self):
return bext.decode('utf-8')[ws:]


@property
def bext(self):
"""
Property bext: Base64 text value portion of qualified b64 str
Returns the value portion of .qb64 with text code and leader removed
"""
return self._decode(raw=self.raw, code=self.code)
#_, _, _, _, ls = self.Sizes[self.code]
#bext = encodeB64(bytes([0] * ls) + self.raw)
#ws = 0
#if ls == 0 and bext:
#if bext[0] == ord(b'A'): # strip leading 'A' zero pad
#ws = 1
#else:
#ws = (ls + 1) % 4
#return bext.decode('utf-8')[ws:]


class Pather(Bexter):
"""
Pather is a subclass of Bexter that provides SAD Path language specific functionality
Expand Down Expand Up @@ -2909,6 +2928,72 @@ def _resolve(self, val, ptr):
return self._resolve(cur, ptr)


class Labeler(Matter):
"""
Labeler is subclass of Matter for CESR native field map labels and/or generic
textual field values. Labeler auto sizes the instance code to minimize
the total encoded size of associated field label or textual field value.
Attributes:
Inherited Properties:
(See Matter)
Properties:
label (str): base value without encoding
Inherited Hidden:
(See Matter)
Hidden:
_label (str): base value without encoding
Methods:
"""


def __init__(self, label='', code=None, **kwa):
"""
Inherited Parameters:
(see Matter)
Parameters:
label (str | bytes): base value before encoding
"""
self._label = None
if label:
if hasattr(label, "decode"): # make label a str
label = label.decode("utf-8")

if not Reb64.match(label.encode("utf-8")):
raise InvalidSoftError(f"Non Base64 chars in {tag=}.")

self._label = label


super(Labeler, self).__init__(code=code, **kwa)



if self.code not in LabelDex:
raise InvalidCodeError(f"Invalid code={self.code} for Labeler.")

@property
def label(self):
"""Returns:
label (str): base value without encoding
getter for ._label. Makes ._label read only
"""
return self._label



class Verfer(Matter):
"""
Expand Down
23 changes: 18 additions & 5 deletions tests/core/test_coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from keri.core import coring
from keri.core.coring import (Saids, Sadder, Tholder, Seqner, NumDex, Number,
Dater, Bexter, Texter,
TagDex, Tagger, Ilker, Traitor,
TagDex, Tagger, Ilker, Traitor, Labeler,
Verser, Versage, )
from keri.core.coring import Kindage, Kinds
from keri.core.coring import (Sizage, MtrDex, Matter)
Expand Down Expand Up @@ -4270,6 +4270,19 @@ def test_pather():
""" Done Test """


def test_labeler():
"""
Test Labeler subclass of Matter
"""


with pytest.raises(EmptyMaterialError):
labeler = Labeler() # defaults


""" Done Test """


def test_verfer():
"""
Test the support functionality for verifier subclass of crymat
Expand Down Expand Up @@ -5390,14 +5403,14 @@ def test_tholder():
test_traitor()
test_verser()
test_diger()
#test_texter()
test_texter()
test_bexter()
test_labeler()
#test_prodex()
#test_indexer()
test_number()
#test_seqner()
#test_siger()
#test_nexter()
#test_tholder()
#test_labels()
#test_prefixer()
test_prefixer()

0 comments on commit 8c038c9

Please sign in to comment.