Exemplo n.º 1
0
def capart_takes(*args):
    args = (
        "name", StringMeta("name of the created attribute"), REQUIRED,
        "description", StringMeta("desc of created attribute"), REQUIRED,
        "pv", StringMeta("full pv of demand and default for rbv"), None,
        "rbv", StringMeta("override for rbv"), None,
        "rbv_suff", StringMeta("set rbv ro pv + rbv_suff"), None,
    ) + args
    return takes(*args)
Exemplo n.º 2
0
def takes_with_default_meta(meta_cls, *meta_args):
    default_args = meta_args + (
        "Default value for parameter. If not specified, parameter is required",
    )
    return takes(
        "name",
        StringMeta("Specify that this class will take a parameter name"),
        REQUIRED, "description", StringMeta("Description of this parameter"),
        REQUIRED, "default", meta_cls(*default_args), OPTIONAL)
Exemplo n.º 3
0
    def test_takes_given_defaults(self):
        @takes("hello", StringMeta(), "Something")
        def say_hello(params):
            """Say hello"""
            print("Hello" + params.name)

        itakes = MapMeta()
        itakes.set_elements(OrderedDict(hello=StringMeta()))
        self.assertEqual(say_hello.Method.takes.to_dict(), itakes.to_dict())
        self.assertEqual(say_hello.Method.returns.to_dict(),
                         MapMeta().to_dict())
        self.assertEqual(say_hello.Method.defaults, {"hello": "Something"})
Exemplo n.º 4
0
    def test_takes_given_required(self):
        @takes("hello", StringMeta(), REQUIRED)
        def say_hello(params):
            """Say hello"""
            print("Hello" + params.name)

        itakes = MapMeta()
        itakes.set_elements(OrderedDict(hello=StringMeta()))
        itakes.set_required(["hello"])
        self.assertEqual(say_hello.Method.takes.to_dict(), itakes.to_dict())
        self.assertEqual(say_hello.Method.returns.to_dict(),
                         MapMeta().to_dict())
        self.assertEqual(say_hello.Method.defaults, {})
Exemplo n.º 5
0
    def test_returns_given_valid_sets(self):
        @returns("hello", StringMeta(), REQUIRED)
        def say_hello(ret):
            """Say hello"""
            ret.hello = "Hello"
            return ret

        ireturns = MapMeta()
        ireturns.set_elements(OrderedDict(hello=StringMeta()))
        ireturns.set_required(["hello"])
        self.assertEqual(say_hello.Method.takes.to_dict(), MapMeta().to_dict())
        self.assertEqual(say_hello.Method.returns.to_dict(),
                         ireturns.to_dict())
        self.assertEqual(say_hello.Method.defaults, {})
Exemplo n.º 6
0
class HelloController(Controller):
    @takes("name", StringMeta(description="a name"), REQUIRED)
    @returns("greeting", StringMeta(description="a greeting"), REQUIRED)
    def say_hello(self, parameters, return_map):
        """Says Hello to name

        Args:
            parameters(Map): The name of the person to say hello to
            return_map(Map): Return structure to complete and return

        Returns:
            Map: The greeting
        """

        return_map.greeting = "Hello %s" % parameters.name
        return return_map
Exemplo n.º 7
0
    def test_returns_not_given_req_or_opt_raises(self):
        with self.assertRaises(AssertionError):

            @returns("hello", StringMeta(), "A default")
            def say_hello(ret):
                """Say hello"""
                ret.hello = "Hello"
                return ret
Exemplo n.º 8
0
 def create_attributes(self):
     self.value = Attribute(NumberMeta(description="Value"))
     self.value.meta.set_dtype('float64')
     yield 'value', self.value
     self.generator = Attribute(
         PointGeneratorMeta(description="Scan Point Generator"))
     yield "generator", self.generator
     self.axis_name = Attribute(StringMeta(description="Name of the axis"))
     yield "axis_name", self.axis_name
     self.exposure = Attribute(NumberMeta(description="Exposure time"))
     self.value.meta.set_dtype('float64')
     yield "exposure", self.exposure
Exemplo n.º 9
0
 def setUp(self):
     self.serialized = OrderedDict()
     self.serialized["typeid"] = "malcolm:core/Method:1.0"
     self.takes = MapMeta()
     self.takes.set_elements(OrderedDict({"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()
Exemplo n.º 10
0
 def _create_default_attributes(self):
     self.state = Attribute(
         ChoiceMeta(description="State of Block",
                    choices=self.stateMachine.possible_states))
     self.state.set_parent(self.block, 'state')
     self.state.set_value(self.stateMachine.DISABLED)
     yield ('state', self.state)
     self.status = Attribute(StringMeta(description="Status of Block"))
     self.status.set_value("Disabled")
     yield ('status', self.status)
     self.busy = Attribute(
         BooleanMeta(description="Whether Block busy or not"))
     self.busy.set_value(False)
     yield ('busy', self.busy)
Exemplo n.º 11
0
class TestValidate(unittest.TestCase):
    def setUp(self):
        self.string_meta = StringMeta("test string description")

    def test_given_value_str_then_return(self):
        response = self.string_meta.validate("TestValue")

        self.assertEqual("TestValue", response)

    def test_given_value_int_then_cast_and_return(self):
        response = self.string_meta.validate(15)

        self.assertEqual("15", response)

    def test_given_value_float_then_cast_and_return(self):
        response = self.string_meta.validate(12.8)

        self.assertEqual("12.8", response)

    def test_given_value_None_then_return(self):
        response = self.string_meta.validate(None)

        self.assertEqual(None, response)
Exemplo n.º 12
0
class TestValidate(unittest.TestCase):

    def setUp(self):
        self.string_meta = StringMeta("test string description")

    def test_given_value_str_then_return(self):
        response = self.string_meta.validate("TestValue")

        self.assertEqual("TestValue", response)

    def test_given_value_int_then_cast_and_return(self):
        response = self.string_meta.validate(15)

        self.assertEqual("15", response)

    def test_given_value_float_then_cast_and_return(self):
        response = self.string_meta.validate(12.8)

        self.assertEqual("12.8", response)

    def test_given_value_None_then_return(self):
        response = self.string_meta.validate(None)

        self.assertEqual(None, response)
Exemplo n.º 13
0
class ScanPointTickerController(Controller):
    def create_attributes(self):
        self.value = Attribute(NumberMeta(description="Value"))
        self.value.meta.set_dtype('float64')
        yield 'value', self.value
        self.generator = Attribute(
            PointGeneratorMeta(description="Scan Point Generator"))
        yield "generator", self.generator
        self.axis_name = Attribute(StringMeta(description="Name of the axis"))
        yield "axis_name", self.axis_name
        self.exposure = Attribute(NumberMeta(description="Exposure time"))
        self.value.meta.set_dtype('float64')
        yield "exposure", self.exposure

    @takes("generator", PointGeneratorMeta(description="Generator instance"),
           REQUIRED, "axis_name", StringMeta(description="Specifier for axis"),
           REQUIRED, "exposure",
           NumberMeta(description="Detector exposure time"), REQUIRED)
    def configure(self, params):
        """
        Configure the controller

        Args:
            generator(PointGenerator): Generator to create points
            axis_name(String): Specifier for axis
            exposure(Double): Exposure time for detector
        """

        self.generator.set_value(params.generator)
        self.axis_name.set_value(params.axis_name)
        self.exposure.set_value(params.exposure)
        self.exposure.meta.set_dtype('float64')
        self.block.notify_subscribers()

    @Method.wrap_method
    def run(self):
        """
        Start the ticker process

        Yields:
            Point: Scan points from PointGenerator
        """
        axis_name = self.axis_name.value
        for point in self.generator.value.iterator():
            self.value.set_value(point.positions[axis_name])
            self.block.notify_subscribers()
            time.sleep(self.exposure.value)
Exemplo n.º 14
0
    def test_defaults(self):
        func = Mock(return_value={"first_out": "test"})
        m = Method("test_description", writeable=True)
        m.set_parent(Mock(), "test_method")
        s = StringMeta(description='desc')
        args_meta = MapMeta()
        args_meta.elements = {"first": s, "second": s}
        m.set_takes(args_meta)
        m.set_defaults({"second": "default"})
        m.set_function(func)

        self.assertEquals({"first_out": "test"},
                          m.call_function(dict(first="test")))
        call_arg = func.call_args[0][0]
        self.assertEqual("test", call_arg.first)
        self.assertEqual("default", call_arg.second)
        self.assertEqual(args_meta, call_arg.meta)
Exemplo n.º 15
0
    def test_call_with_map(self):
        @takes("desc", StringMeta("description"), REQUIRED)
        def f(params, *args):
            return 2

        d = dict(desc="my name")
        CAPart = Mock(wraps=f)
        ca = Mock(CAPart=CAPart)
        parts = Mock(ca=ca)

        result = call_with_map(parts, "ca.CAPart", d, 43)
        self.assertEqual(result, 2)
        self.assertEqual(CAPart.call_count, 1)
        call_args = CAPart.call_args_list[0][0]
        self.assertEqual(len(call_args), 2)
        self.assertEqual(call_args[1], 43)
        self.assertEqual(call_args[0].desc, "my name")
Exemplo n.º 16
0
 def test_put_root_update_response(self):
     attr1 = StringMeta("dummy")
     attr2 = StringMeta("dummy2")
     new_block_structure = {}
     new_block_structure["attr1"] = attr1.to_dict()
     new_block_structure["attr2"] = attr2.to_dict()
     self.b.replace_children = MagicMock()
     response = MagicMock(
         id_=self.cc.BLOCK_ID,
         changes=[[[], new_block_structure]])
     self.cc.put(response)
     self.assertIs(self.b, self.cc.block)
     deserialized_changes = self.b.replace_children.call_args_list[0][0][0]
     serialized_changes = [x.to_dict() for x in
                           deserialized_changes.values()]
     expected = [attr1.to_dict(), attr2.to_dict()]
     # dicts are not hashable, so cannot use set compare
     for x in expected:
         self.assertTrue(x in serialized_changes)
     for x in serialized_changes:
         self.assertTrue(x in expected)
Exemplo n.º 17
0
 def test_replace_children(self):
     b = Block()
     b.name = "blockname"
     b.methods["m1"] = 2
     b.attributes["a1"] = 3
     setattr(b, "m1", 2)
     setattr(b, "a1", 3)
     attr_meta = StringMeta(description="desc")
     attr = Attribute(attr_meta)
     b.add_attribute('attr', attr)
     method = Method(description="desc")
     b.add_method('method', method)
     b.on_changed = MagicMock(wrap=b.on_changed)
     b.replace_children({'attr': attr, 'method': method})
     self.assertEqual(b.attributes, dict(attr=attr))
     self.assertEqual(b.methods, dict(method=method))
     b.on_changed.assert_called_once_with([[], b.to_dict()], True)
     self.assertFalse(hasattr(b, "m1"))
     self.assertFalse(hasattr(b, "a1"))
Exemplo n.º 18
0
    def test_incomplete_return(self):
        func = Mock(return_value={"output1": 2})
        m = Method("test_description", writeable=True)
        m.name = "test_method"
        m.set_function(func)
        s = StringMeta(description='desc')
        args_meta = MapMeta()
        args_meta.set_elements({"first": s, "second": s})
        return_meta = MapMeta()
        return_meta.set_elements({"output1": s, "output2": s})
        return_meta.set_required(["output2"])
        m.set_takes(args_meta)
        m.set_returns(return_meta)

        with self.assertRaises(KeyError):
            m.call_function(dict(first=1, second=2))
        call_arg1, call_arg2 = func.call_args_list[0][0]
        self.assertEqual('1', call_arg1.first)
        self.assertEqual('2', call_arg1.second)
        self.assertEqual(args_meta, call_arg1.meta)
        self.assertEqual(return_meta, call_arg2.meta)
Exemplo n.º 19
0
def with_takes_from(parameters, include_name):
    """Create an @takes decorator from parameters dict.

    Args:
        parameters (dict): Parameters sub dictionary. E.g.
            {"string": {"name": "something"}}
        include_name (bool): If True then put a "name" meta first

    Returns:
        function: Decorator that will set a "Method" attribute on the callable
            with the arguments it should take
    """
    # find all the Takes objects and create them
    if include_name:
        takes_arguments = [
            "name", StringMeta("Name of the created block"), REQUIRED
        ]
    else:
        takes_arguments = []
    for name, d in parameters.items():
        takes_arguments += call_with_map(malcolm.parameters, name, d)
    return takes(*takes_arguments)
Exemplo n.º 20
0
 def setUp(self):
     self.string_meta = StringMeta("test string description")
Exemplo n.º 21
0
 def create_meta(self, description):
     return StringMeta("meta", description)
Exemplo n.º 22
0
 def test_from_dict(self):
     a = Serializable.from_dict(self.serialized)
     self.assertEquals(a.meta.parent, a)
     self.assertEquals(a.meta.to_dict(), StringMeta("desc").to_dict())
     self.assertEquals(a.value, "some string")
Exemplo n.º 23
0
 def test_to_dict(self):
     a = Attribute(StringMeta("desc"))
     a.set_value("some string")
     self.assertEqual(a.to_dict(), self.serialized)
Exemplo n.º 24
0
 def setUp(self):
     self.serialized = OrderedDict()
     self.serialized["typeid"] = "epics:nt/NTAttribute:1.0"
     self.serialized["meta"] = StringMeta("desc").to_dict()
     self.serialized["value"] = "some string"
Exemplo n.º 25
0
 def setUp(self):
     self.meta = StringMeta("something")
Exemplo n.º 26
0
 def setUp(self):
     self.string_meta = StringMeta("test string description")