def setUp(self): n = NumberMeta(description='a number') s = StringMeta(description="a string") self.meta = MapMeta() self.meta.set_elements(ElementMap({"a": s, "b": s})) self.meta.set_required(["a"]) self.nmeta = MapMeta() self.nmeta.set_elements(ElementMap({"a": n, "b": n})) self.nmeta.set_required(["a"])
def _prepare_map_meta(args, allow_defaults, defaults=None, elements=None, required=None): # prepare some data structures that will be used for the takes MapMeta if defaults is None: defaults = OrderedDict() if elements is None: elements = OrderedDict() if required is None: required = [] for index in range(0, len(args), 3): # pick out 3 arguments name = args[index] check_camel_case(name) meta = args[index + 1] default = args[index + 2] # store them in the right structures elements[name] = meta if default is REQUIRED: required.append(name) elif default is not OPTIONAL: assert allow_defaults, \ "Defaults not allowed in this structure" defaults[name] = default # Setup the takes MapMeta and attach it to the function's MethodMeta meta = MapMeta() meta.set_elements(ElementMap(elements)) meta.set_required(required) return meta, defaults
def recreate_from_others(self, method_metas, without=None): if without is None: without = [] defaults = OrderedDict() elements = OrderedDict() required = [] # Populate the intermediate data structures for method_meta in method_metas: for element in method_meta.takes.elements: if element not in without: # Serialize it to copy it serialized = method_meta.takes.elements[element].to_dict() elements[element] = serialized if element in method_meta.takes.required and \ element not in required: required.append(element) if element in method_meta.defaults: defaults.pop(element, None) defaults[element] = method_meta.defaults[element] # TODO: what about returns? # remove required args that are now defaulted required = [r for r in required if r not in defaults] # Update ourself from these structures takes = MapMeta() takes.set_elements(ElementMap(elements)) takes.set_required(required) self.set_takes(takes) self.set_defaults(defaults)
def test_recreate(self): @method_takes( "arg1", StringMeta("Arg1"), REQUIRED, "extra", StringMeta(), REQUIRED, ) def method1(): pass @method_takes( "arg8", StringMeta("Arg8"), REQUIRED, "arg3", StringMeta("Arg3"), "32", "arg4", StringMeta("Arg4"), OPTIONAL, ) def method2(): pass @method_takes( "arg8", StringMeta("Arg8"), "2", "arg3", StringMeta("Arg3"), "33", ) def method3(): pass m = MethodMeta("Test") m.recreate_from_others( [method1.MethodMeta, method2.MethodMeta, method3.MethodMeta], without=["extra"]) itakes = MapMeta() elements = OrderedDict() elements["arg1"] = StringMeta("Arg1") elements["arg8"] = StringMeta("Arg8") elements["arg3"] = StringMeta("Arg3") elements["arg4"] = StringMeta("Arg4") itakes.set_elements(ElementMap(elements)) itakes.set_required(["arg1"]) defaults = OrderedDict() defaults["arg8"] = "2" defaults["arg3"] = "33" self.assertEqual(m.takes.to_dict(), itakes.to_dict()) self.assertEqual(m.returns.to_dict(), MapMeta().to_dict()) self.assertEqual(m.defaults, defaults)
def setUp(self): self.sam = StringArrayMeta() self.serialized = OrderedDict() self.serialized["typeid"] = "malcolm:core/MapMeta:1.0" self.serialized["elements"] = ElementMap(dict(c1=self.sam)).to_dict() self.serialized["description"] = "desc" self.serialized["tags"] = () self.serialized["writeable"] = False self.serialized["label"] = "" self.serialized["required"] = ("c1",)
def test_method_also_takes(self): @method_takes("hello", StringMeta(), REQUIRED, "hello2", BooleanMeta(), False) class Thing(object): pass @method_also_takes("world", BooleanMeta(), REQUIRED, "hello2", BooleanMeta(), True, "default", StringMeta(), "nothing") class Thing2(Thing): pass # Check original hasn't been modified itakes = MapMeta() elements = OrderedDict() elements["hello"] = StringMeta() elements["hello2"] = BooleanMeta() itakes.set_elements(ElementMap(elements)) itakes.set_required(["hello"]) defaults = OrderedDict() defaults["hello2"] = False self.assertEqual(Thing.MethodMeta.takes.to_dict(), itakes.to_dict()) self.assertEqual(Thing.MethodMeta.returns.to_dict(), MapMeta().to_dict()) self.assertEqual(Thing.MethodMeta.defaults, defaults) # Check new one overrides/improves on original itakes = MapMeta() elements = OrderedDict() elements["hello"] = StringMeta() elements["hello2"] = BooleanMeta() elements["world"] = BooleanMeta() elements["default"] = StringMeta() itakes.set_elements(ElementMap(elements)) itakes.set_required(["hello", "world"]) defaults = OrderedDict() defaults["hello2"] = True defaults["default"] = "nothing" self.assertEqual(Thing2.MethodMeta.takes.to_dict(), itakes.to_dict()) self.assertEqual(Thing2.MethodMeta.returns.to_dict(), MapMeta().to_dict()) self.assertEqual(Thing2.MethodMeta.defaults, defaults)
def setUp(self): self.serialized = OrderedDict() self.serialized["typeid"] = "malcolm:core/MethodMeta:1.0" self.takes = MapMeta() self.takes.set_elements(ElementMap({"in_attr": StringMeta("desc")})) self.serialized["takes"] = self.takes.to_dict() self.serialized["defaults"] = OrderedDict({"in_attr": "default"}) self.serialized["description"] = "test_description" self.serialized["tags"] = () self.serialized["writeable"] = True self.serialized["label"] = "" self.serialized["returns"] = MapMeta().to_dict()
def test_takes_given_optional(self): @method_takes("hello", StringMeta(), OPTIONAL) def say_hello(params): """Say hello""" print("Hello" + params.name) itakes = MapMeta() itakes.set_elements(ElementMap(OrderedDict(hello=StringMeta()))) self.assertEqual(say_hello.MethodMeta.takes.to_dict(), itakes.to_dict()) self.assertEqual(say_hello.MethodMeta.returns.to_dict(), MapMeta().to_dict()) self.assertEqual(say_hello.MethodMeta.defaults, {})
def test_returns_given_valid_sets(self): @method_returns("hello", StringMeta(), REQUIRED) def say_hello(ret): """Say hello""" ret.hello = "Hello" return ret ireturns = MapMeta() ireturns.set_elements(ElementMap(OrderedDict(hello=StringMeta()))) ireturns.set_required(["hello"]) self.assertEqual(say_hello.MethodMeta.takes.to_dict(), MapMeta().to_dict()) self.assertEqual(say_hello.MethodMeta.returns.to_dict(), ireturns.to_dict()) self.assertEqual(say_hello.MethodMeta.defaults, {})
def setUp(self): self.callback_result = 0 self.callback_value = '' meta = StringMeta("meta for unit tests") self.proc = MagicMock(q=queue.Queue()) self.proc.create_queue = MagicMock(side_effect=queue.Queue) self.block = Block() self.block.set_process_path(self.proc, ("testBlock", )) self.attr = meta.make_attribute() self.attr2 = meta.make_attribute() self.method = MethodMeta("method for unit tests") self.method.returns.set_elements(ElementMap(dict(ret=StringMeta()))) self.method2 = MethodMeta("method for unit tests") self.block.replace_endpoints( dict(testFunc=self.method, testFunc2=self.method2, testAttr=self.attr, testAttr2=self.attr2)) self.bad_called_back = False
def test_to_dict(self): tm = MapMeta("desc") tm.set_elements(ElementMap(dict(c1=self.sam))) tm.set_required(["c1"]) self.assertEqual(tm.to_dict(), self.serialized)
def test_set_elements(self): els = ElementMap(dict(sam=StringArrayMeta())) self.mm.set_elements(els) self.assertEqual(self.mm.elements, els)
def __init__(self, description="", tags=None, writeable=False, label=""): super(MapMeta, self).__init__(description, tags, writeable, label) self.set_elements(ElementMap()) self.set_required([])