Skip to content

Commit

Permalink
added regex guts for supporting v2.xx version string. Prepatory to ma…
Browse files Browse the repository at this point in the history
…king version string tooling support it.

Added unit tests to confirm
  • Loading branch information
SmithSamuelM committed Feb 24, 2024
1 parent 98cbbac commit b7c28e6
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/keri/kering.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@
VERFMT = "{}{:x}{:x}{}{:0{}x}_" # version format string
VERFULLSIZE = 17 # number of characters in full version string

VEREX = b'(?P<proto>[A-Z]{4})(?P<major>[0-9a-f])(?P<minor>[0-9a-f])(?P<kind>[A-Z]{4})(?P<size>[0-9a-f]{6})_'
Rever = re.compile(VEREX) # compile is faster
VEREX0 = b'(?P<proto>[A-Z]{4})(?P<major>[0-9a-f])(?P<minor>[0-9a-f])(?P<kind>[A-Z]{4})(?P<size>[0-9a-f]{6})_'

Rever = re.compile(VEREX0) # compile is faster


VER1FULLSPAN = 17 # number of characters in full version string
VER1TERM = b'_'
VEREX1 = b'(?P<proto1>[A-Z]{4})(?P<major1>[0-9a-f])(?P<minor1>[0-9a-f])(?P<kind1>[A-Z]{4})(?P<size1>[0-9a-f]{6})_'

VER2FULLSPAN = 16 # number of characters in full version string
VER2TERM = b'.'
VEREX2 = b'(?P<proto2>[A-Z]{4})(?P<major2>[0-9A-Za-z_-])(?P<minor2>[0-9A-Za-z_-]{2})(?P<kind2>[A-Z]{4})(?P<size2>[0-9A-Za-z_-]{4}).'

VEREX = VEREX2 + b'|' + VEREX1

pattern = re.compile(VEREX) # compile is faster


def versify(proto=Protos.keri, version=Version, kind=Serials.json, size=0):
Expand Down Expand Up @@ -69,13 +83,17 @@ def deversify(vs, version=None):
serialization kind
serialization size
"""
# length of matched string is sum of lengths of returned group elements
# span

match = Rever.match(vs.encode("utf-8")) # match takes bytes
if match:
proto, major, minor, kind, size = match.group("proto",
"major",
"minor",
"kind",
"size")

proto = proto.decode("utf-8")
vrsn = Versionage(major=int(major, 16), minor=int(minor, 16))
kind = kind.decode("utf-8")
Expand Down
129 changes: 127 additions & 2 deletions tests/test_kering.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
tests.test_kering module
"""

import re
import json

import cbor2 as cbor
Expand All @@ -12,9 +12,11 @@
from keri import kering
from keri.kering import Protocolage, Protos
from keri.kering import Serialage, Serials
from keri.kering import Ilkage, Ilks
from keri.kering import (Versionage, Version, VERFULLSIZE,
versify, deversify, Rever)
from keri.kering import Ilkage, Ilks
from keri.kering import (VER1FULLSPAN, VER1TERM, VEREX1,
VER2FULLSPAN, VER2TERM, VEREX2, VEREX)



Expand All @@ -36,6 +38,128 @@ def test_protos():

"""End Test"""

def test_version_regex():
"""
Test version string regexing
"""

#VER1FULLSPAN = 17 # number of characters in full version string
#VER1TERM = b'_'
#VEREX1 = b'(?P<proto1>[A-Z]{4})(?P<major1>[0-9a-f])(?P<minor1>[0-9a-f])(?P<kind1>[A-Z]{4})(?P<size1>[0-9a-f]{6})_'

#VER2FULLSPAN = 16 # number of characters in full version string
#VER2TERM = b'.'
#VEREX2 = b'(?P<proto2>[A-Z]{4})(?P<major2>[0-9A-Za-z_-])(?P<minor2>[0-9A-Za-z_-]{2})(?P<kind2>[A-Z]{4})(?P<size2>[0-9A-Za-z_-]{4}).'

#VEREX = VEREX2 + b'|' + VEREX1

pattern = re.compile(VEREX2) # compile is faster

vs = b'KERICAAJSONAAAB.'

match = pattern.match(vs)
assert match

full = match.group() # not group args so returns full match
assert full == vs
span = len(full)
assert span == VER2FULLSPAN
assert VER2TERM == chr(full[-1]).encode("utf-8")
assert ord(VER2TERM) == full[-1]

groups = match.group("proto2",
"major2",
"minor2",
"kind2",
"size2")

assert groups == (b'KERI', b'C', b'AA', b'JSON', b'AAAB')


pattern = re.compile(VEREX) # compile is faster

vs = b'KERICAAJSONAAAB.'

match = pattern.match(vs)
assert match

full = match.group() # not group args so returns full match
assert full == vs
span = len(full)
assert span == VER2FULLSPAN
assert VER2TERM == chr(full[-1]).encode("utf-8")
assert ord(VER2TERM) == full[-1]

groups = match.group("proto2",
"major2",
"minor2",
"kind2",
"size2")

assert groups == (b'KERI', b'C', b'AA', b'JSON', b'AAAB')

vs = b'KERI10JSON000002_'

match = pattern.match(vs)
assert match

full = match.group() # not group args so returns full match
assert full == vs
span = len(full)
assert span == VER1FULLSPAN
assert VER1TERM == chr(full[-1]).encode("utf-8")
assert ord(VER1TERM) == full[-1]

groups = match.group("proto1",
"major1",
"minor1",
"kind1",
"size1")

assert groups == (b'KERI', b'1', b'0', b'JSON', b'000002')

raw = b'{"vs":"KERICAAJSONAAAB.","pre":"AaU6JR2nmwyZ-i0d8JZAoTNZH3ULvYAfSVPzhzS6b5CM"}'

match = pattern.search(raw)
assert match

full = match.group() # not group args so returns full match
span = len(full)
assert span == VER2FULLSPAN
assert VER2TERM == chr(full[-1]).encode("utf-8")
assert ord(VER2TERM) == full[-1]

groups = match.group("proto2",
"major2",
"minor2",
"kind2",
"size2")

assert groups == (b'KERI', b'C', b'AA', b'JSON', b'AAAB')



raw = b'{"vs":"KERI10JSON000002_","pre":"AaU6JR2nmwyZ-i0d8JZAoTNZH3ULvYAfSVPzhzS6b5CM"}'

match = pattern.search(raw)
assert match

full = match.group() # not group args so returns full match
span = len(full)
assert span == VER1FULLSPAN
assert VER1TERM == chr(full[-1]).encode("utf-8")
assert ord(VER1TERM) == full[-1]

groups = match.group("proto1",
"major1",
"minor1",
"kind1",
"size1")

assert groups == (b'KERI', b'1', b'0', b'JSON', b'000002')

"""End Test"""


def test_serials():
"""
Expand Down Expand Up @@ -285,6 +409,7 @@ def test_ilks():

if __name__ == "__main__":
test_protos()
test_version_regex()
test_serials()
test_versify()
test_ilks()

0 comments on commit b7c28e6

Please sign in to comment.