def makeFile(self, ref=False): file = File(self, randomString()) if ref: r = Ref(self, file.path) r._set(file) return r return file
def testFacets(self): data = TestData(14) vat = {'data': data} ref = Ref(vat, 'data', 'public') self.assertEqual(ref.get(), 14) self.assertRaises(AttributeError, getattr, ref, 'set') data.setMaster(True) ref.set(42) self.assertEqual(ref.get(), 42) self.assertRaises(RefError, ref._save) self.assertRaises(RefError, ref._erase) self.assertRaises(RefError, ref._set, 'Anything') self.assertRaises(RefError, ref._getFacet, 'nested') self.assertRaises(RefError, Ref, vat, 'data', '_private')
def localize(self, x): if isinstance(x, Exception): return x typ = type(x) if typ in POD_TYPES: return x if typ is Proxy: if x._node == self.node_id: return Ref(self.storage, x._path) return Proxy(x._node, x._path, self) raise SerializationError('cannot localize type %s' % typ)
def custom(self, name, data): if name == 'ref': node = data['node'] if node == self.node_id: return Ref(self.vat().storage, data['path']) if node == '-': node = self.msg_data.get('from') return Proxy(node, data['path'], self.vat()) if name == 'exc': return decodeException(data) if name == 'inst' and self._safe_cls(data['CLS']): cls = importSymbol(data['CLS']) try: args = [data[key.lstrip('_')] for key in cls.serialize] except KeyError: raise SerializationError(name + ': ' + repr(data)) else: return cls(*args) raise SerializationError(name)
def custom(self, name, data): storage = self.storage() if name == 'ref': return Ref(storage, data['path'], data.get('facet')) if name == 'inst': cls = importSymbol(storage.map_class(data['CLS'])) data['#vat'] = storage data.update(storage.resources) # Check for version change. data_version = data.get('$version', 0) cls_version = getattr(cls, '_version', 0) if cls_version != data_version: cls._upgrade(data, data_version) args = [data.get(key.lstrip('_')) for key in cls.serialize] inst = cls(*args) if cls_version != data_version: # Ensure new nested serializables get correct _save hook. encodes(inst, self) if hasattr(cls, '_save'): inst._save = self.save return inst raise SerializationError(name)
def makeRef(self, svalue=None, vat_id=None): ref = Ref(self, randomString()) if svalue is not None: ref._set(svalue) return ref
def getRef(self, path): return Ref(self, path)
def _addRef(self, path, svalue): if type(getattr(svalue, 'serialize', None)) is tuple: if type(getattr(svalue, 'ref', None)) is not Ref: svalue.ref = Ref(self, path) getattr(svalue, '_on_addref', lambda: None)() return True
def testData(self): store = Storage({}) store['people/data/tom'] = Data({}) t_data = store['people/data/tom'] t_data['name'] = 'Tom' t_data['age'] = 14 self.assertEqual(t_data['name'], 'Tom') env = Ref(store, 'people/data/tom') store['people/obj/tom'] = Person(env) tom = store['people/obj/tom'] self.assertEqual(tom.description(), 'Tom age 14') tom.haveBirthday() self.assertEqual(t_data['age'], 15) store['people/data/gary'] = Data({}) g_data = store['people/data/gary'] g_data['name'] = 'Gary' g_data['age'] = 48 t_data['dad'] = g_data self.assertEqual(type(t_data['dad']), Ref) self.assertEqual(tom.dadsName(), 'Gary') with Capture() as c: fcat(tom) self.assertEqual(c.getvalue(), "Person(env=ref(path='people/data/tom'))\n") # We can store refs anywhere in data. A top-level Data object # which has come from the store can turn itself into a ref. g_data['kids'] = [t_data] self.assertEqual(type(g_data['kids'][0]), Ref) self.assertEqual(g_data['kids'][0]['name'], 'Tom') # We do not support deep references into data (e.g. g_data['kids']) # because they would be very breakable, and copying would upset # the normal language expectation of node sharing. self.assertRaises(ValueError, t_data.__setitem__, 'siblings', g_data['kids']) store['caps/time'] = Time() t_data['time'] = store.getRef('caps/time') self.assertEqual(type(store['caps/time']), Time) self.assertEqual(type(t_data['time']), Ref) self.assertEqual(tom.whatTime(), 'Tea-time') t_data.save() g_data.save() del t_data, g_data, tom self.assertEqual(store.cache.values(), []) t_data = store['people/data/tom'] g_data = store['people/data/gary'] tom = store['people/obj/tom'] self.assertEqual(t_data['name'], 'Tom') self.assertEqual(t_data['age'], 15) self.assertEqual(type(t_data['time']), Ref) self.assertEqual(t_data['time'].time(), 'Tea-time') tom.haveBirthday() self.assertEqual(g_data['kids'][0]['age'], 16)