def from_transit(in_data, protocol): io = StringIO(in_data) reader = Reader(protocol) reader.register("testobj", TestObjHandler) return Reader(protocol).read(io)
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_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 get_post_data_from_req(request): if not request.data: return {} reader = Reader() return reader.read(StringIO(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 test_json(self): with open( "../transit-format/examples/0.8/simple/" + name + ".json", 'r') as stream: data = Reader(protocol="json").read(stream) self.assertEqual(val, data)
def test_msgpack(self): with open("../transit-format/examples/0.8/simple/" + name + ".mp", 'rb') as stream: data = Reader(protocol="msgpack").read(stream) self.assertEqual(val, data)
def transit_loads(s): return Reader('json').read(StringIO(s))