def replace_endpoints(self, d): children = OrderedDict() for k, v in d.items(): assert isinstance(k, str_), "Expected string, got %s" % (k,) if k == "typeid": assert v == self.typeid, \ "Dict has typeid %s but Class has %s" % (v, self.typeid) else: try: object.__getattribute__(self, k) except AttributeError: children[k] = deserialize_object(v, self.child_type_check) else: raise AttributeError( "Setting child %r would shadow an attribute" % (k,)) self.endpoints = list(children) for k, v in children.items(): self.set_endpoint_data(k, v, notify=False) if self.process: self.process.report_changes( [self.process_path, serialize_object(self)])
def replace_endpoints(self, d): children = OrderedDict() for k, v in d.items(): assert isinstance(k, str_), "Expected string, got %s" % (k, ) if k == "typeid": assert v == self.typeid, \ "Dict has typeid %s but Class has %s" % (v, self.typeid) else: try: object.__getattribute__(self, k) except AttributeError: children[k] = deserialize_object(v, self.child_type_check) else: raise AttributeError( "Setting child %r would shadow an attribute" % (k, )) self.endpoints = list(children) for k, v in children.items(): self.set_endpoint_data(k, v, notify=False) if self.process: self.process.report_changes( [self.process_path, serialize_object(self)])
def set_required(self, required, notify=True): """Set the required string list""" for r in required: assert r in self.elements, \ "Expected one of %r, got %r" % (list(self.elements), r) required = StringArray(deserialize_object(t, str_) for t in required) self.set_endpoint_data("required", required, notify)
def make_return_table(self, part_tasks): # Filter part tasks so that we only run the ones hooked to us columns = OrderedDict(name=StringArrayMeta("Part name")) for part in part_tasks: hooked = [ method_name for (method_name, hook, _) in get_hook_decorated(part) if hook is self ] for method_name, method_meta, func in get_method_decorated(part): if method_name in hooked: # Add return metas to the table columns for arg_name in method_meta.returns.elements: md = method_meta.returns.elements[arg_name].to_dict() if "ArrayMeta" in md["typeid"]: md["tags"] = md["tags"] + ["hook:return_array"] else: md["typeid"] = md["typeid"].replace( "Meta", "ArrayMeta") meta = deserialize_object(md, VArrayMeta) if arg_name in columns: column_d = columns[arg_name].to_dict() assert column_d == md, \ "%s != %s" % (column_d, md) columns[arg_name] = meta meta = TableMeta("Part returns from hook", columns=columns) return_table = Table(meta) return return_table
def set_takes(self, takes, notify=True): """Set the arguments and default values for the method Args: takes (MapMeta): Arguments to the function """ takes = deserialize_object(takes, MapMeta) self.set_endpoint_data("takes", takes, notify)
def set_id(self, id_): """ Set the identifier for the request Args: id_(int): Unique identifier for request """ if id_ is not None: id_ = deserialize_object(id_, int) self.set_endpoint_data("id", id_)
def set_meta(self, meta, notify=True): """Set the VMeta object""" meta = deserialize_object(meta) # Check that the meta attribute_class is ourself assert hasattr(meta, "attribute_class"), \ "Expected meta object, got %r" % meta assert isinstance(self, meta.attribute_class), \ "Meta object needs to be attached to %s, we are a %s" % ( meta.attribute_class, type(self)) self.set_endpoint_data("meta", meta, notify)
def set_delta(self, delta): delta = deserialize_object(delta, bool) self.set_endpoint_data("delta", delta)
def set_parameters(self, parameters): if parameters is not None: parameters = OrderedDict( (deserialize_object(k, str_), serialize_object(v)) for k, v in parameters.items()) self.set_endpoint_data("parameters", parameters)
def set_endpoint(self, endpoint): if endpoint is not None: endpoint = [deserialize_object(e, str_) for e in endpoint] self.set_endpoint_data("endpoint", endpoint)
def set_tags(self, tags, notify=True): """Set the tags list""" tags = [deserialize_object(t, str_) for t in tags] self.set_endpoint_data("tags", tags, notify)
def set_label(self, label, notify=True): """Set the label string""" label = deserialize_object(label, str_) self.set_endpoint_data("label", label, notify)
def set_elements(self, elements, notify=True): """Set the elements dict from a ScalarArrayMeta or serialized dict""" elements = deserialize_object(elements, TableElementMap) self.set_endpoint_data("elements", elements, notify)
def set_returns(self, returns, notify=True): """Set the return parameters for the method to validate against""" returns = deserialize_object(returns, MapMeta) self.set_endpoint_data("returns", returns, notify)
def set_elements(self, elements, notify=True): """Set the elements dict from a ScalarMeta or serialized dict""" elements = deserialize_object(elements, ElementMap) self.set_endpoint_data("elements", elements, notify)
def set_choices(self, choices, notify=True): """Set the choices list""" choices = [deserialize_object(c, str_) for c in choices] self.set_endpoint_data("choices", choices, notify)
def set_message(self, message): if message is not None: message = deserialize_object(message, str_) self.set_endpoint_data("message", message)
def set_description(self, description, notify=True): """Set the description string""" description = deserialize_object(description, str_) self.set_endpoint_data("description", description, notify)
def set_writeable(self, writeable, notify=True): """Set the writeable bool""" writeable = deserialize_object(writeable, bool) self.set_endpoint_data("writeable", writeable, notify)
def set_bar(self, bar): d = OrderedDict() for k, v in bar.items(): if k != "typeid": d[k] = deserialize_object(v) self.bar = d
def test_deserialize(self): a = EmptySerializable() d = a.to_dict() b = deserialize_object(d) assert a.to_dict() == b.to_dict()