def test_create_children(self, attribute_mock, method_mock): # Load up items to create mi1, mi2 = MagicMock(), MagicMock() method_mock.side_effect = [mi1, mi2] ai1, ai2 = MagicMock(), MagicMock() attribute_mock.side_effect = [ai1, ai2] # Load up refs to get group_attr = Attribute() child_attr = self.make_grouped_attr() m1 = MethodMeta() m2 = MethodMeta() BlockItem.items[("endpoint", "foo")] = ai1 self.item.ref = OrderedDict(( ("foo", group_attr), ("c", child_attr), ("a", m1), ("b", m2))) self.item.create_children() # Check it made the right thing self.assertEqual(len(self.item.children), 3) attribute_mock.assert_any_call(("endpoint", "foo"), group_attr) self.assertEqual(self.item.children[0], ai1) attribute_mock.assert_any_call(("endpoint", "c"), child_attr) ai1.add_child.assert_called_once_with(ai2) method_mock.assert_any_call(("endpoint", "a"), m1) self.assertEqual(self.item.children[1], mi1) method_mock.assert_any_call(("endpoint", "b"), m2) self.assertEqual(self.item.children[2], mi2)
def create_methods(self): method_name = self.field_name.replace(".", ":") if self.arg_meta: # Decorate set_field with a MethodMeta @method_takes(self.arg_name, self.arg_meta, REQUIRED) def set_field(params): self.set_field(params) self.method = set_field.MethodMeta writeable_func = set_field else: self.method = MethodMeta() writeable_func = None self.method.set_description(self.description) self.method.set_tags(self.tags) label = self.field_name.replace(".", " ").replace("_", " ").title() self.method.set_label(label) yield method_name, self.method, writeable_func
def create_methods(self): label, method_name = make_label_attr_name(self.field_name) if self.arg_meta: self.arg_name = method_name # Decorate set_field with a MethodMeta @method_takes(self.arg_name, self.arg_meta, REQUIRED) def set_field(params): self.set_field(params) self.method = set_field.MethodMeta writeable_func = set_field else: self.method = MethodMeta() writeable_func = None self.method.set_description(self.description) self.method.set_tags(self.tags) self.method.set_label(label) yield method_name, self.method, writeable_func
def instantiate(self, substitutions): """Keep recursing down from base using dotted name, then call it with self.params and args Args: substitutions (dict): Substitutions to make to self.param_dict Returns: The found object called with (*args, map_from_d) E.g. if ob is malcolm.parts, and name is "ca.CADoublePart", then the object will be malcolm.parts.ca.CADoublePart """ param_dict = self.substitute_params(substitutions) pkg, ident = self.name.rsplit(".", 1) pkg = f"malcolm.modules.{pkg}" try: ob = importlib.import_module(pkg) except ImportError as e: raise_with_traceback( ImportError("\n%s:%d:\n%s" % (self.filename, self.lineno, e)) ) try: ob = getattr(ob, ident) except AttributeError: raise_with_traceback( ImportError( "\n%s:%d:\nPackage %r has no ident %r" % (self.filename, self.lineno, pkg, ident) ) ) try: meta = MethodMeta.from_callable(ob, returns=False) args = meta.takes.validate(param_dict) ret = ob(**args) except Exception as e: sourcefile = inspect.getsourcefile(ob) lineno = inspect.getsourcelines(ob)[1] raise_with_traceback( YamlError( "\n%s:%d:\n%s:%d:\n%s" % (self.filename, self.lineno, sourcefile, lineno, e) ) ) else: return ret
def _update_configure_args(self): # Look for all parts that hook into Configure configure_funcs = self.Configure.find_hooked_functions(self.parts) method_metas = [] for part, func_name in configure_funcs.items(): method_metas.append(part.method_metas[func_name]) # Update takes with the things we need default_configure = MethodMeta.from_dict( RunnableController.configure.MethodMeta.to_dict()) default_configure.defaults["axesToMove"] = self.axes_to_move.value method_metas.append(default_configure) # Decorate validate and configure with the sum of its parts self.block["validate"].recreate_from_others(method_metas) self.block["validate"].set_returns(self.block["validate"].takes) self.block["configure"].recreate_from_others(method_metas)
class PandABoxActionPart(Part): """This will normally be instantiated by the PandABox assembly, not created in yaml""" def __init__(self, process, control, block_name, field_name, description, tags, arg_name=None, arg_meta=None): super(PandABoxActionPart, self).__init__(process) self.control = control self.block_name = block_name self.field_name = field_name self.description = description self.tags = tags self.arg_name = arg_name self.arg_meta = arg_meta self.method = None def create_methods(self): method_name = self.field_name.replace(".", ":") if self.arg_meta: # Decorate set_field with a MethodMeta @method_takes(self.arg_name, self.arg_meta, REQUIRED) def set_field(params): self.set_field(params) self.method = set_field.MethodMeta writeable_func = set_field else: self.method = MethodMeta() writeable_func = None self.method.set_description(self.description) self.method.set_tags(self.tags) label = self.field_name.replace(".", " ").replace("_", " ").title() self.method.set_label(label) yield method_name, self.method, writeable_func def set_field(self, params=None): full_field = "%s.%s" % (self.block_name, self.field_name) if params is None: value = 0 else: value = params[self.arg_name] self.control.set_field(full_field, value)
class PandABoxActionPart(Part): """This will normally be instantiated by the PandABox assembly, not created in yaml""" def __init__(self, process, control, block_name, field_name, description, tags, arg_meta=None): params = Part.MethodMeta.prepare_input_map(name=field_name) super(PandABoxActionPart, self).__init__(process, params) self.control = control self.block_name = block_name self.field_name = field_name self.description = description self.tags = tags self.arg_name = None self.arg_meta = arg_meta self.method = None def create_methods(self): label, method_name = make_label_attr_name(self.field_name) if self.arg_meta: self.arg_name = method_name # Decorate set_field with a MethodMeta @method_takes(self.arg_name, self.arg_meta, REQUIRED) def set_field(params): self.set_field(params) self.method = set_field.MethodMeta writeable_func = set_field else: self.method = MethodMeta() writeable_func = None self.method.set_description(self.description) self.method.set_tags(self.tags) self.method.set_label(label) yield method_name, self.method, writeable_func def set_field(self, params=None): if params is None: value = 0 else: value = params[self.arg_name] self.control.set_field(self.block_name, self.field_name, value)
def create_methods(self): # MethodMeta instance self.method = MethodMeta(self.params.description) # TODO: set widget tag? yield self.params.name, self.method, self.caput
def create_methods(self): # MethodMeta instance self.method = MethodMeta(self.params.description) yield self.params.name, self.method, self.caput
def test_ref_children(self): self.item.ref = dict( a=MethodMeta(), b=MethodMeta(), c=Attribute()) self.assertEqual(self.item.ref_children(), 3)