From 7d6f404bcd1401597b70c9057909e9ce37dd496b Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 9 Sep 2019 11:55:59 -0700 Subject: [PATCH] encode UUIDs as bytes.. This shrinks the pubkey string significantly... --- ui/cli.py | 21 +++++++++++++++------ ui/fixtures/genfixtures.py | 3 ++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ui/cli.py b/ui/cli.py index 511b4fc..c9e5f5d 100644 --- a/ui/cli.py +++ b/ui/cli.py @@ -45,7 +45,7 @@ def _makeuuid(s): if isinstance(s, uuid.UUID): return s - return uuid.UUID(s) + return uuid.UUID(bytes=s) # XXX - known issue, store is not atomic/safe, overwrites in place instead of renames @@ -188,7 +188,7 @@ class Identity(MDBase): def _trytodict(o): if isinstance(o, uuid.UUID): - return 'unicode', str(o) + return 'bytes', o.bytes try: return 'dict', o.__to_dict__() except Exception: # pragma: no cover @@ -688,6 +688,15 @@ class _TestCases(unittest.TestCase): # and that they are equal self.assertEqual(obj, decobj) + # and in the encoded object + eobj = _asn1coder.loads(coded) + + # the uuid property is a str instance + self.assertIsInstance(eobj['uuid'], str) + + # and has the length of 16 + self.assertEqual(len(eobj['uuid']), 16) + def test_mdbase_wrong_type(self): # that created_by_ref can be passed by kw obj = MetaData(created_by_ref=self.created_by_ref) @@ -855,7 +864,7 @@ class _TestCases(unittest.TestCase): objst.loadobj({ 'type': 'metadata', - 'uuid': 'c9a1d1e2-3109-4efd-8948-577dc15e44e7', + 'uuid': uuid.UUID('c9a1d1e2-3109-4efd-8948-577dc15e44e7'), 'modified': datetime.datetime(2019, 5, 31, 14, 3, 10), 'created_by_ref': self.created_by_ref, 'hashes': [ 'sha256:91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ada' ], @@ -885,11 +894,11 @@ class _TestCases(unittest.TestCase): self.assertEqual(len(objs), len(objst)) - self.assertEqual(objs['created_by_ref'], str(self.created_by_ref)) + self.assertEqual(objs['created_by_ref'], self.created_by_ref.bytes) for i in objs['objects']: - i['created_by_ref'] = uuid.UUID(i['created_by_ref']) - i['uuid'] = uuid.UUID(i['uuid']) + i['created_by_ref'] = uuid.UUID(bytes=i['created_by_ref']) + i['uuid'] = uuid.UUID(bytes=i['uuid']) self.assertEqual(objst.by_id(i['uuid']), i) testfname = os.path.join(self.tempdir, 'test.txt') diff --git a/ui/fixtures/genfixtures.py b/ui/fixtures/genfixtures.py index b3d36d5..d6424b1 100644 --- a/ui/fixtures/genfixtures.py +++ b/ui/fixtures/genfixtures.py @@ -1,6 +1,7 @@ import pasn1 import cli import datetime +import uuid persona = cli.Persona() persona.generate_key() @@ -10,7 +11,7 @@ map(objst.loadobj, [ { 'type': 'metadata', - 'uuid': '3e466e06-45de-4ecc-84ba-2d2a3d970e96', + 'uuid': uuid.UUID('3e466e06-45de-4ecc-84ba-2d2a3d970e96'), 'created_by_ref': cbr, 'modified': datetime.datetime(2019, 5, 31, 14, 5, 3), 'dc:creator': [ u'John-Mark Gurney' ],