Ejemplo n.º 1
0
    def __init__(self, value=None, *args, **kwargs):
        """ Initialize the 'List' class.

        Expect a 'content' string argument that describe the list content.

        Parameters
        ----------
        content: str (mandatory)
            description of the list content. If iterative object are contained
            use the '_' character as a separator: 'Int' or 'List_Int'.
        value: object (optional, default None)
            the parameter value.
        """
        # Avoid cycling import
        from casper.lib.controls import controls

        # Check if a 'content' argument has been defined
        if "content" not in kwargs:
            raise ValueError("A 'content' argument  describing the 'List' "
                             "content is expected.")

        # Create an inner control
        inner_desc = kwargs["content"].split("_")
        control_type = inner_desc[0]
        inner_kwargs = {"inner": True}
        if len(inner_desc) > 1:
            inner_kwargs["content"] = "_".join(inner_desc[1:])
        if control_type not in controls:
            raise ValueError("List creation: '{0}' is not a valid inner "
                             "control type. Allowed types are {1}.".format(
                                 kwargs["content"], controls.keys()))
        self.inner_control = controls[control_type](**inner_kwargs)

        # Create the list control
        Base.__init__(self, value, *args, **kwargs)
        self.iterable = True
Ejemplo n.º 2
0
    def _set_controls(self):
        """ Define the bbox input and output parameters. Each parameter is
        a control defined in 'casper.lib.controls'.

        Expected control attibutes are: 'type', 'name', 'description', 'from',
        'role'.
        """
        # Get the function default values
        args = inspect.getargspec(self._func)
        defaults = dict(zip(reversed(args.args or []),
                            reversed(args.defaults or [])))

        # Go through all controls defined in the function prototype
        shared_output_controls = []
        for desc in self.proto:

            # Detect shared output controls
            if "from" in desc and desc["role"] == "output":
                shared_output_controls.append(desc["from"])
                continue

            # Get the control type and description
            control_type = desc.get("type", None)
            if control_type is None:
                raise Exception("Impossible to warp Bbox '{0}': control type "
                                "undefined.".format(self.id))
            control_desc = desc.get("description", "")
            control_name = desc.get("name", None)
            if control_name is None:
                raise Exception("Impossible to warp Bbox '{0}': control name "
                                "undefined.".format(self.id))
            control_content = desc.get("content", None)

            # Check if the control type is valid
            if control_type in controls:

                # Create the control
                control = controls[control_type](
                    desc=control_desc, content=control_content)
                control.name = control_name
                if control_name in defaults:
                    control.optional = True
                    control.value = defaults[control_name]

                # Split output controls
                if desc["role"] == "output":
                    control.type = "output"
                    setattr(self.outputs, control_name, control)

                # And input controls
                else:
                    control.type = "input"
                    setattr(self.inputs, control_name, control)

            # Raise an exception otherwise
            else:
                raise Exception(
                    "Impossible to warp Bbox '{0}': unknown control type "
                    "'{1}', expect one in {2}.".format(self.id, control_type,
                                                       controls.keys()))

        # Deal with shared output controls
        for input_control_name in shared_output_controls:
            if input_control_name in self.inputs.controls:
                control = self.inputs[input_control_name]
                control.type = "reference"
                setattr(self.outputs, input_control_name, control)
            else:
                raise Exception(
                    "Impossible to warp Bbox '{0}': unknown input control "
                    "'{1}' detected in shared output creation.".format(
                        self.id, input_control_name))