From c1593e3a81f1883238938a6165d6a95f23587d5c Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sun, 2 Jun 2019 22:25:50 -0700 Subject: [PATCH] make sure original object is not modified, and auto-generate some required common properties.. --- ui/cli.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ui/cli.py b/ui/cli.py index 6a7adf0..d486163 100644 --- a/ui/cli.py +++ b/ui/cli.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import copy import datetime import hashlib import pasn1 @@ -22,13 +23,22 @@ class MDBase(object): '''This is a simple wrapper that turns a JSON object into a pythonesc object where attribute accesses work.''' - _common_properties = [ 'uuid', 'type', 'modified', 'created_by_ref' ] + _generated_properties = { + 'uuid': uuid.uuid4, + 'modified': datetime.datetime.utcnow + } + _common_properties = [ 'type', 'created_by_ref' ] def __init__(self, obj): + obj = copy.deepcopy(obj) for x in self._common_properties: if x not in obj: raise ValueError('common property %s not present' % `x`) + for x, fun in self._generated_properties.iteritems(): + if x not in obj: + obj[x] = fun() + self._obj = obj @classmethod @@ -230,6 +240,15 @@ class _TestCases(unittest.TestCase): self.assertRaises(ValueError, MDBase.create_obj, { 'type': 'unknosldkfj' }) self.assertRaises(ValueError, MDBase.create_obj, { 'type': 'metadata' }) + baseobj = { + 'type': 'metadata', + 'created_by_ref': '867c7563-79ae-435c-a265-9d8509cefac5', + } + origbase = copy.deepcopy(baseobj) + md = MDBase.create_obj(baseobj) + + self.assertEqual(baseobj, origbase) + def test_makehash(self): self.assertRaises(ValueError, ObjectStore.makehash, 'slkj') self.assertRaises(ValueError, ObjectStore.makehash, 'sha256:91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ADA')