def check_blocks_equal(self): src_block = self.process.block_view("TESTDET") block = self.process2.block_view("TESTDET") for k in src_block: assert json_encode(block[k].to_dict(), indent=2) == json_encode(src_block[k].to_dict(), indent=2)
def _on_response(self, response: Response) -> None: # called from tornado thread message = json_encode(response) try: self.write_message(message) except WebSocketError: # The websocket is dead. If the response was a Delta or Update, then # unsubscribe so the local controller doesn't keep on trying to # respond if isinstance(response, (Delta, Update)): # Websocket is dead so we can clear the subscription key. # Subsequent updates may come in before the unsubscribe, but # ignore them as we can't do anything about it mri = self._id_to_mri.pop(response.id, None) if mri: log.info("WebSocket Error: unsubscribing from stale handle") unsubscribe = Unsubscribe(response.id) unsubscribe.set_callback(self.on_response) if self._registrar: self._registrar.report( builtin.infos.RequestInfo(unsubscribe, mri) ) finally: assert self._queue, "No queue" cothread.Callback(self._queue.put, None)
def _send_request(self, request): # Called in tornado thread request.id = self._next_id self._next_id += 1 self._request_lookup[request.id] = request message = json_encode(request) self.log.debug("Sending message %s", message) self._conn.write_message(message)
def handle_response(self, response): # called from tornado thread if isinstance(response, Return): message = json_encode(response.value) self.finish(message + "\n") else: if isinstance(response, Error): message = response.message else: message = "Unknown response %s" % type(response) self.set_status(500, message) self.write_error(500)
def send_messages(self, messages, convert_json=True): conn = yield websocket_connect("ws://localhost:%s/ws" % self.socket) num = len(messages) for msg in messages: if convert_json: msg = json_encode(msg) conn.write_message(msg) for _ in range(num): resp = yield conn.read_message() resp = json.loads(resp) cothread.Callback(self.result.put, resp) conn.close()
def on_message(self, message): # called in tornado's thread if self._writeable is None: ipv4_ip = self.request.remote_ip if ipv4_ip == "::1": # Special case IPV6 loopback ipv4_ip = "127.0.0.1" remoteaddr = struct.unpack("!I", socket.inet_aton(ipv4_ip))[0] if self._validators: # Work out if the remote ip is within the netmask of any of our # interfaces. If not, Put and Post are forbidden self._writeable = max(v(remoteaddr) for v in self._validators) else: self._writeable = True log.info( "Puts and Posts are %s from %s", "allowed" if self._writeable else "forbidden", self.request.remote_ip, ) msg_id = -1 try: d = json_decode(message) try: msg_id = d["id"] except KeyError: raise FieldError("id field not present in JSON message") request = deserialize_object(d, Request) request.set_callback(self.on_response) if isinstance(request, Subscribe): assert msg_id not in self._id_to_mri, ( "Duplicate subscription ID %d" % msg_id ) self._id_to_mri[msg_id] = request.path[0] if isinstance(request, Unsubscribe): mri = self._id_to_mri[msg_id] else: mri = request.path[0] if isinstance(request, (Put, Post)) and not self._writeable: raise ValueError( "Put/Post is forbidden from %s" % self.request.remote_ip ) self._registrar.report(builtin.infos.RequestInfo(request, mri)) except Exception as e: log.exception("Error handling message:\n%s", message) error = Error(msg_id, e) error_message = error.to_dict() self.write_message(json_encode(error_message))
def do_save(self, design=""): if not design: design = self.design.value assert design, "Please specify save design name when saving from new" assert not design.startswith( "template_"), "Cannot save over a template" structure = OrderedDict() attributes = structure.setdefault("attributes", OrderedDict()) # Add the layout table layout = attributes.setdefault("layout", OrderedDict()) for name, mri, x, y, visible in self.layout.value.rows(): layout_structure = OrderedDict() layout_structure["x"] = x layout_structure["y"] = y layout_structure["visible"] = visible layout[name] = layout_structure # Add the exports table exports = attributes.setdefault("exports", OrderedDict()) for source, export in self.exports.value.rows(): exports[source] = export # Add other attributes for name, attribute in self.our_config_attributes.items(): attributes[name] = attribute.value # Add any structure that a child part wants to save structure["children"] = self.run_hooks( SaveHook(p, c) for p, c in self.create_part_contexts(only_visible=False).items()) text = json_encode(structure, indent=2) filename = self._validated_config_filename(design) if filename.startswith("/tmp"): self.log.warning("Saving to tmp directory %s" % filename) with open(filename, "w") as f: f.write(text) # Run a sync command to make sure we flush this file to disk subprocess.call("sync") # Try and commit the file to git, don't care if it fails self._run_git_cmd("add", filename) msg = "Saved %s %s" % (self.mri, design) self._run_git_cmd("commit", "--allow-empty", "-m", msg, filename) self._mark_clean(design)
def merge_non_writeable_table( default: Table, supplied: Table, non_writeable: List[int] ) -> Table: default_rows = list(default.rows()) for supplied_row in supplied.rows(): key = [supplied_row[i] for i in non_writeable] for default_row in default_rows: if key == [default_row[i] for i in non_writeable]: break else: d = OrderedDict() for i, k in enumerate(supplied.call_types): if i in non_writeable: d[k] = supplied_row[i] raise ValueError( "Table row with %s doesn't match a row in the default table" % json_encode(d) ) for i, v in enumerate(supplied_row): if i not in non_writeable: default_row[i] = v table = default.from_rows(default_rows) return table
def test_post_hello(self): IOLoopHelper.call(self.post, "hello", "greet", json_encode(dict(name="me"))) result = self.result.get(timeout=2) assert result.body.decode().strip() == json_encode("Hello me")
def test_get_hello(self): IOLoopHelper.call(self.get, "hello") result = self.result.get(timeout=2) assert result.body.decode().strip() == json_encode(self.hello._block)
def test_enum_serialize(self): class MyEnum(Enum): ME = "me" s = json_encode({"who": MyEnum.ME}) assert s == '{"who": "me"}'
def test_exception_serialize(self): s = json_encode({"message": ValueError("Bad result")}) assert s == '{"message": "ValueError: Bad result"}'
def test_json_numpy_array(self): s1 = DummySerializable(3, {}, np.array([3, 4])) assert json_encode(s1) == \ '{"typeid": "foo:1.0", "boo": 3, "bar": {}, "NOT_CAMEL": [3, 4]}'