def list(self, namespace=''): keys = (x.decode('utf-8') for x in self.db.keys()) if not namespace: tmp = (x.split('.')[0] for x in keys) else: Packet.validate_key(namespace) ns_len = len(namespace) + 1 tmp = (x[ns_len:] for x in keys if x.startswith(namespace + '.')) return list(set(tmp))
def save(self, packet): try: packets = self.backlog(packet.key) except KeyError: packets = [] _packet = Packet(packet.key, packet.payload) for (name, fh) in packet.attachments.items(): aid = self.write(fh) _packet.attachments[name] = aid packets.insert(0, _packet) dump = pickle.dumps(packets) self.db[packet.key] = dump return _packet
def test_attachments_backlog(self): # Save some packets packets = [ Packet('foo', None, attachments={'x': io.BytesIO(str(idx).encode('utf-8'))}) for idx in range(3) ] for packet in packets: self.storage.save(packet) for (expected, packet) in zip(packets, reversed(self.storage.backlog('foo'))): # Read attachments from storage engine for (name, aid) in packet.attachments.items(): with self.storage.open(aid) as fh: packet.attachments[name] = fh.read() # Read attachments from original packets for (name, aid) in expected.attachments.items(): expected.attachments[name].seek(0) expected.attachments[name] = expected.attachments[name].read() # Compare self.assertEqual(expected.payload, packet.payload) self.assertEqual(expected.attachments, packet.attachments)
def test_backlog(self): for x in range(3): self.storage.save(Packet('foo', x)) backlog = self.storage.backlog('foo') payloads = [x.payload for x in backlog] self.assertEqual(payloads, [2, 1, 0])
def test_attachments(self): contents = 'hi!'.encode('utf-8') p = Packet('foo', None, attachments={'stdout': io.BytesIO(contents)}) self.storage.save(p) p = self.storage.get('foo') with self.storage.open(p.attachments['stdout']) as fh: buff = fh.read() self.assertEqual(buff, contents)
def load_as_multipart(): attachments = { name.split(':')[1]: f.stream for (name, f) in data.items() if name.startswith('attachment:') } payload = data["payload"] if not isinstance(payload, str): payload = payload.read() payload = json.loads(payload) return Packet(key, payload, attachments=attachments)
def save(self, key, payload, attachments=None): kwargs = {} if attachments: kwargs['files'] = { 'attachment:' + k: v for (k, v) in attachments.items() } kwargs['files']['payload'] = json.dumps(payload) else: kwargs['json'] = payload resp = self.request('POST', key, **kwargs) if resp.status_code != 200: raise APIError() return Packet.fromdict(json.loads(resp.content.decode('utf-8')))
def test_invalid_keys(self): with self.assertRaises(TypeError): self.storage.save(Packet(1, 2)) with self.assertRaises(ValueError): self.storage.save(Packet('', '')) with self.assertRaises(ValueError): self.storage.save(Packet('foo..x', '')) with self.assertRaises(ValueError): self.storage.save(Packet('foo.', '')) with self.assertRaises(ValueError): self.storage.save(Packet('.foo', '')) with self.assertRaises(ValueError): self.storage.save(Packet('ñ', ''))
def load_as_json(): return Packet(key, data)
def get(self, key): resp = self.request('GET', key) if resp.status_code != 200: raise APIError() return Packet.fromdict(json.loads(resp.content.decode('utf-8')))
def test_sub_children(self): self.storage.save(Packet('ns.foo', None)) self.storage.save(Packet('ns.bar', None)) children = self.storage.list() self.assertTrue(set(children), set(['foo', 'bar']))
def test_root_children(self): self.storage.save(Packet('x', None)) self.storage.save(Packet('y', None)) children = self.storage.list() self.assertTrue(set(children), set(['x', 'y']))
def test_get_complex(self): payload = ['1', 2, False, {'foo': 'bar', 'a': {'b': 'c'}}, 1.8] self.storage.save(Packet('x', payload)) self.assertEqual(self.storage.get('x').payload, payload)
def test_get_complex(self): packet = self.storage.save(Packet('x', 1)) self.assertEqual(packet.payload, 1) self.assertEqual(self.storage.get('x').payload, 1)