def update(self, filter, values, model=None): pkeys = [] for kv in self.select_r(filter): rec = decodes(kv.value) if updateFields(rec, values, model): pkeys.append(kv.key) self.set_r(kv.key, encodes(rec)) return pkeys
def testSerializationErrors(self): # NOTE: Proxy storage requires an RPCStorageCtx. proxy_enc = encodes(Record('ref', {'path':'p', 'node':'n'})) unk_enc = encodes(Record('unknown', 3)) msg_enc = encodes(Record('@', 'xyz', 35)) s = Storage({'foo': proxy_enc, 'unk': unk_enc, 'msg': msg_enc}) #self.assertRaises(SerializationError, s.__getitem__, 'foo') self.assertRaises(SerializationError, s.__getitem__, 'unk') self.assertEqual(s['msg'], Record('@', 'xyz', 35)) class NoSer(object): pass self.assertRaises(SerializationError, s.__setitem__, 'nos', NoSer()) ns = NameStore(s, {}) self.assertRaises(NoSuchName, ns.getn, 'bimbo')
def testSerializationErrors(self): # NOTE: Proxy storage requires an RPCStorageCtx. proxy_enc = encodes(Record('ref', {'path': 'p', 'node': 'n'})) unk_enc = encodes(Record('unknown', 3)) msg_enc = encodes(Record('@', 'xyz', 35)) s = Storage({'foo': proxy_enc, 'unk': unk_enc, 'msg': msg_enc}) #self.assertRaises(SerializationError, s.__getitem__, 'foo') self.assertRaises(SerializationError, s.__getitem__, 'unk') self.assertEqual(s['msg'], Record('@', 'xyz', 35)) class NoSer(object): pass self.assertRaises(SerializationError, s.__setitem__, 'nos', NoSer()) ns = NameStore(s, {}) self.assertRaises(NoSuchName, ns.getn, 'bimbo')
def test(self): net = MockNet() e = net.addNode('node') d = Dispatcher(e) hfb = d.addEndpoint('foo/bar') lfb = Listener(hfb) hf = d.addEndpoint('foo') lf = Listener(hf) h = d.addEndpoint('') l = Listener(h) bm_to_fred = {'pcol': 'json', 'message': '{"o": "fred"}'} e.notify('message', bm_to_fred) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, []) self.assertEqual(lfb.received, []) msg_to_ffred = {'pcol': 'serf', 'message': encodes('foo/fred')} e.notify('message', msg_to_ffred) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, [msg_to_ffred]) self.assertEqual(lfb.received, []) msg_to_fbb = {'pcol': 'serf', 'message': encodes('foo/bar/baz')} e.notify('message', msg_to_fbb) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, [msg_to_ffred]) self.assertEqual(lfb.received, [msg_to_fbb]) msg_to_fbark = {'pcol': 'serf', 'message': encodes('foo/bark')} e.notify('message', msg_to_fbark) self.assertEqual(l.received, [bm_to_fred]) self.assertEqual(lf.received, [msg_to_ffred, msg_to_fbark]) self.assertEqual(lfb.received, [msg_to_fbb])
def send(self, node, addr, msg, errh=None): msg['o'] = addr pcol = self._peer_protocol(node) if pcol == 'json': if self.verbose: print getattr(self.node, 'client_ip', ''), 'Out', msg enc = JSON_CODEC.encode(msg, self.json_ctx) elif pcol == 'local': enc = rmap(self.delocalize, msg) else: enc = encodes(msg, self.remote_ctx) self.node.send(node, enc, errh=errh)
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 _join1(self, l_pk, left, join, query, func): l_col, r_col, type = join.leftCol, join.rightCol, join.type if r_col != '#': r_col = ':' + r_col spec = '%s %s' % (r_col, type) limit = join.limit or sys.maxint try: key = l_pk if l_col == '#' else left[l_col] except (KeyError, AttributeError): to_join = [] else: filters = [query, Range(0, limit)] if query is not None else [Range(0, limit)] to_join = self.select_r([Key(spec, key)] + filters) if not to_join and join.nulls: to_join.append(KeyValue(0, encodes({}))) for kv in to_join: right = decodes(kv.value) func(l_pk, left, kv.key, right)
def _join1(self, l_pk, left, join, query, func): l_col, r_col, type = join.leftCol, join.rightCol, join.type if r_col != '#': r_col = ':' + r_col spec = '%s %s' % (r_col, type) limit = join.limit or sys.maxint try: key = l_pk if l_col == '#' else left[l_col] except (KeyError, AttributeError): to_join = [] else: filters = [query, Range(0, limit) ] if query is not None else [Range(0, limit)] to_join = self.select_r([Key(spec, key)] + filters) if not to_join and join.nulls: to_join.append(KeyValue(0, encodes({}))) for kv in to_join: right = decodes(kv.value) func(l_pk, left, kv.key, right)
def _update(self, pkey, values, vrec): rec = self.get(pkey) if updateFields(rec, values, vrec): self.set_r(pkey, encodes(rec))
def set(self, pkey, data, notify=True): self.set_r(pkey, encodes(data), notify)
def convert(v1, v2, value): if v1.node_id == v2.node_id: return v2.localize(v1.delocalize(value)) return decodes(encodes(value, v1.encodeRemote), v2.decodeRemote)
def setn(self, name, ref_or_value): ref = self._asRef(ref_or_value) ctx = self.storage.make_context(self.storage) self.store[name] = encodes(ref, ctx)
def append(self, value): index = self.log.append(encodes(value, self.storage_ctx)) self.obs.add(index, value) return index
def setKey(self, spec, message, replace=True): return self.setKey_r(spec, encodes(message), replace)
def buildJoin(s_pk, s_rec, r_pk, r_rec): out = mergeRecords(s_pk, s_rec, r_pk, r_rec, merge, {}) results.append(KeyValue(s_pk, encodes(out)))
def save(self, path, svalue=Unique): if svalue is Unique: svalue = self.cache[path] ctx = self.make_context(self, path) self.store[path] = encodes(svalue, ctx)