def load_layout(self, value): # TODO: Look for value in our save file location filename = "/tmp/" + value + ".json" text = open(filename, "r").read() structure = json_decode(text) self._load_from_structure(structure) self.layout_name.set_value(value)
def get_doc_json(fname): malcolm_root = os.path.join(os.path.dirname(__file__), "..", "..") json_root = os.path.join(malcolm_root, "docs", "reference", "json") with open(os.path.join(json_root, fname)) as f: lines = f.readlines() text = "\n".join(lines[1:]) return json_decode(text)
def post(self, endpoint_str): # called from tornado thread path = endpoint_str.split("/") parameters = json_decode(self.get_body_argument("parameters")) request = Post(path=path, parameters=parameters, callback=self.on_response) self._server_part.on_request(request)
def load_layout(self, value): # TODO: race condition if we get 2 loads at once... # Do we need a Loading state? filename = self._validated_config_filename(value) text = open(filename, "r").read() structure = json_decode(text) self._load_from_structure(structure) self.layout_name.set_value(value)
def on_message(self, message): # called in tornado's thread if self._writeable is None: # Work out if the remote ip is within the netmask of any of our # interfaces. If not, Put and Post are forbidden 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] self._writeable = max(v(remoteaddr) for v in self._validators) 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) log.info("Request: %s", request) 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_load(self, design): filename = self._validated_config_filename(design) with open(filename, "r") as f: text = f.read() structure = json_decode(text) # Set the layout table layout_table = Table(self.layout.meta) for part_name, part_structure in structure.get("layout", {}).items(): layout_table.append([ part_name, "", part_structure["x"], part_structure["y"], part_structure["visible"]]) self.set_layout(layout_table) # Set the exports table exports_table = Table(self.exports.meta) for name, export_name in structure.get("exports", {}).items(): exports_table.append([name, export_name]) self.exports.set_value(exports_table) # Run the load hook to get parts to load their own structure self.run_hook(self.Load, self.create_part_contexts(only_visible=False), structure) self._mark_clean(design)
def on_message(self, message): """Pass response from server to process receive queue Args: message(str): Received message """ try: self.log.debug("Got message %s", message) d = json_decode(message) response = deserialize_object(d, Response) if isinstance(response, (Return, Error)): request, old_id = self._request_lookup.pop(response.id) if request.generate_key() in self._subscription_keys: self._subscription_keys.pop(request.generate_key()) else: request, old_id = self._request_lookup[response.id] response.set_id(old_id) # TODO: should we spawn here? request.callback(response) except Exception: # If we don't catch the exception here, tornado will spew odd # error messages about 'HTTPRequest' object has no attribute 'path' self.log.exception("on_message(%r) failed", message)
def on_message(self, message): """Pass response from server to process receive queue Args: message(str): Received message """ # Called in tornado loop try: self.log.debug("Got message %s", message) d = json_decode(message) response = deserialize_object(d, Response) if isinstance(response, (Return, Error)): request = self._request_lookup.pop(response.id) if isinstance(response, Error): # Make the message an exception so it can be raised response.message = ResponseError(response.message) else: request = self._request_lookup[response.id] # Transfer the work of the callback to cothread cothread.Callback(request.callback, response) except Exception: # If we don't catch the exception here, tornado will spew odd # error messages about 'HTTPRequest' object has no attribute 'path' self.log.exception("on_message(%r) failed", message)
def on_message(self, message): # called in tornado's thread d = json_decode(message) request = deserialize_object(d, Request) request.set_callback(self.on_response) self._server_part.on_request(request)
def post(self, endpoint_str): # called from tornado thread path = endpoint_str.split("/") parameters = json_decode(self.get_body_argument("parameters")) request = Post(path=path, parameters=parameters) self.report_request(request)