def onMessage(self, payload, isBinary): if isBinary: print("Binary message received: {0} bytes".format( len(payload))) else: readHandlers = marshal.getReadHandlers( localTargets, lambda targetId, v: self.sanitySend( ('put!', targetId, v)), lambda targetId: self.sanitySend( ('close!', targetId)), remoteResources) reader = Reader(TRANSIT_ENCODING) for tag, handler in readHandlers.items(): reader.register(tag, handler) msg = reader.read(StringIO(payload)) cmd, targetId, val = msg if cmd == 'put!' or cmd == 'close!': if targetId in localTargets: channelMarshal = localTargets[targetId] if cmd == 'put!': channelMarshal.ch.put(val) elif cmd == 'close!': channelMarshal.ch.close() if channelMarshal.useOnce: channelMarshal.ch.release() else: print "Unrecognized target! " + target else: print "Unrecognized command! " + cmdStr
def onMessage(self, payload, isBinary): if isBinary: print ("Binary message received: {0} bytes".format(len(payload))) else: readHandlers = marshal.getReadHandlers( localTargets, lambda targetId, v: self.sanitySend(("put!", targetId, v)), lambda targetId: self.sanitySend(("close!", targetId)), remoteResources, ) reader = Reader(TRANSIT_ENCODING) for tag, handler in readHandlers.items(): reader.register(tag, handler) msg = reader.read(StringIO(payload)) cmd, targetId, val = msg if cmd == "put!" or cmd == "close!": if targetId in localTargets: channelMarshal = localTargets[targetId] if cmd == "put!": channelMarshal.ch.put(val) elif cmd == "close!": channelMarshal.ch.close() if channelMarshal.useOnce: channelMarshal.ch.release() else: print "Unrecognized target! " + target else: print "Unrecognized command! " + cmdStr
def test_roundtrip(self): in_data = value io = StringIO() w = Writer(io, "json") w.write(in_data) r = Reader("json") out_data = r.read(StringIO(io.getvalue())) self.assertEqual(in_data, out_data)
def read_transit_handle(handle, handlers=None): reader = Reader("json") if handlers: for tag, handler in handlers.items(): reader.register(tag, handler) return reader.read(handle)
def test_reencode_json_verbose(self): io = StringIO() writer = Writer(io, protocol="json_verbose") writer.write(val) s = io.getvalue() io = StringIO(s) reader = Reader(protocol="json_verbose") newval = reader.read(io) self.assertEqual(val, newval)
def test_write_bool(self): for protocol in ("json", "json_verbose", "msgpack"): io = StringIO() w = Writer(io, protocol) w.write((True, False)) r = Reader(protocol) io.seek(0) out_data = r.read(io) assert out_data[0] == true assert out_data[1] == false
def test_reencode_msgpack(self): io = BytesIO() writer = Writer(io, protocol="msgpack") writer.write(val) s = io.getvalue() io = BytesIO(s) reader = Reader(protocol="msgpack") newval = reader.read(io) self.assertEqual(val, newval)
def test_reencode_json(self): io = StringIO() writer = Writer(io, protocol="json") writer.write(val) s = io.getvalue() # Uncomment when debugging to see what payloads fail # print(s) io = StringIO(s) reader = Reader(protocol="json") newval = reader.read(io) self.assertEqual(val, newval)
def load(name, workdir='work', backend='pickle'): """Load a simple object from a store (no db).""" if not os.path.exists(workdir): os.makedirs(workdir) if backend == 'transit': fname = os.path.join(workdir, name+'.json') from transit.reader import Reader with open(fname, 'r') as f: reader = Reader('json') data = reader.read(f) else: raise NotImplementedError("load: {} backend not implemented".format(backend)) return data
def _request(action, datoms): io = StringIO() writer = Writer(io) writer.write({Keyword("content"): datoms}) r = post("http://127.0.0.1:8080/" + action, data=io.getvalue().encode('utf-8'), headers={ 'Content-Type': "application/transit+json; charset=utf-8", }) try: reader = Reader() data = reader.read(StringIO(r.content)) except: print "ILLEGAL RESPONSE: " try: print r.content except UnboundLocalError: print "NO RESPONSE" pass if not r: raise DatomicError(data[Keyword("content")]) else: return data[Keyword("content")]
def get_post_data_from_req(request): if not request.data: return {} reader = Reader() return reader.read(BytesIO(request.data))
from transit.reader import Reader reader = Reader("json") with open("resources/transit_data.json", "r") as io: val = reader.read(io) print val
def get_post_data_from_req(request): if not request.data: return {} reader = Reader() return reader.read(StringIO(request.data))