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 write(data): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(SERVER_ADDRESS) with closing(sock): sockfile = sock.makefile('wrb') with closing(sockfile): Writer(sockfile, TRANSFER_PROTOCOL).write(data) return Reader(TRANSFER_PROTOCOL).read(sockfile)
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))
def test_json_verbose(self): with open( "../transit-format/examples/0.8/simple/" + name + ".verbose.json", 'r') as stream: data = Reader(protocol="json_verbose").read(stream) self.assertEqual(val, data)
def test_msgpack(self): with open("../transit-format/examples/0.8/simple/" + name + ".mp", 'r') as stream: data = Reader(protocol="msgpack").read(stream) self.assertEqual(val, data)
except ImportError: from io import StringIO # In Case we want to use multiple threads try: import thread except ImportError: import _thread as thread import time # Creates the writer io = StringIO() writer = Writer(StringIO(), "json") # Creates the reader reader = Reader("json") # Gets the agent agent = run_student_bot() # Url For the Websocket url = "ws://emscript.regent.edu:3449/chat?name=ChAI&room=ChatTest" # Defines the action to do on reciept of a message def on_message(ws, message): # Uses the transit reader to parse the recieved message data = reader.read(StringIO(message)) # Checks to see if it is a new message
def transit_loads(s): return Reader('json').read(StringIO(s))