Exemplo n.º 1
0
    def _handle_set_component(self, args, field_name, finfo):
        base_cls_name = finfo["type"]["name"]

        models = [
            self._talus_client.code_find(base_cls_name, type="component")
        ]
        descendants = list(
            self._talus_client.code_iter(type_="component",
                                         bases=base_cls_name))
        models += descendants

        pick_component_cls = True
        component = None
        sub_params = {}

        # don't clobber existing settings! be nice and ask the user
        # psychomatic complexity TO THE MAX!! :^(
        if field_name in self._params and self._params[field_name] is not None:
            if len(descendants) == 0:
                pick_component_cls = False
                sub_params = self._params[field_name]["params"]

            else:
                res = self.ask(
                    "Do you want to change the component class? (y/n) ")
                if res.strip().lower() in ["y", "yes"]:
                    pick_component_cls = True
                else:
                    pick_component_cls = False
                    component = filter(
                        lambda x: x.name == self._params[field_name]["class"],
                        models)[0]
                    try:
                        sub_params = self._params[field_name]["params"]
                    except KeyError as e:
                        self.err(
                            "something wrong happened, can't keep old params, need to reset all values"
                        )
                        sub_params = {}

        if len(descendants) > 0 and pick_component_cls:
            component = utils.model_prompt(
                models, "Which component subclass should be used?")
        elif component is None:
            component = models[0]

        processor = ParameterCmd(sub_params, component, self._talus_host,
                                 self._talus_client)
        processor.prompt = self.prompt[:-2] + ":{}> ".format(field_name)
        processor.cmdloop()

        self._params[field_name] = {
            "class": component.name,
            "params": sub_params
        }
Exemplo n.º 2
0
    def _handle_set_component(self, args, field_name, finfo):
        base_cls_name = finfo["type"]["name"]

        models = [self._talus_client.code_find(base_cls_name, type="component")]
        descendants = list(self._talus_client.code_iter(type_="component", bases=base_cls_name))
        models += descendants

        pick_component_cls = True
        component = None
        sub_params = {}

        # don't clobber existing settings! be nice and ask the user
        # psychomatic complexity TO THE MAX!! :^(
        if field_name in self._params and self._params[field_name] is not None:
            if len(descendants) == 0:
                pick_component_cls = False
                sub_params = self._params[field_name]["params"]

            else:
                res = self.ask("Do you want to change the component class? (y/n) ")
                if res.strip().lower() in ["y", "yes"]:
                    pick_component_cls = True
                else:
                    pick_component_cls = False
                    component = filter(lambda x: x.name == self._params[field_name]["class"], models)[0]
                    try:
                        sub_params = self._params[field_name]["params"]
                    except KeyError as e:
                        self.err("something wrong happened, can't keep old params, need to reset all values")
                        sub_params = {}

        if len(descendants) > 0 and pick_component_cls:
            component = utils.model_prompt(models, "Which component subclass should be used?")
        elif component is None:
            component = models[0]

        processor = ParameterCmd(sub_params, component, self._talus_host, self._talus_client)
        processor.prompt = self.prompt[:-2] + ":{}> ".format(field_name)
        processor.cmdloop()

        self._params[field_name] = {"class": component.name, "params": sub_params}
Exemplo n.º 3
0
    def do_set(self, args):
        """Set parameter
		"""

        orig_args = args
        args = shlex.split(args)

        if len(args) == 0:
            print("wat - you must supply a fieldname to set")
            return

        field_name = args[0]

        if field_name == "params":
            self._param_cmd.prompt = self.prompt[:-2] + ":params> "
            self._param_cmd.cmdloop("\nEditing params\n")
            return

        if field_name not in self._model.fields and field_name not in self._shim_fields:
            self.err("Field '{}' does not exist. Try again".format(field_name))
            return

        is_shim = False
        if field_name in self._model.fields:
            field_cls = self._model.fields[field_name]
        else:
            is_shim = True
            field_cls = self._shim_fields[field_name]["type"]

        if isinstance(field_cls, RefField):
            refd_cls = field_cls.get_ref_cls()
            search = field_cls.search
            if search is None:
                search = {}

            while True:
                refd_model = utils.model_prompt(
                    refd_cls.objects(api_base=None, **search),
                    "Which one should {!r} be set to?".format(field_name),
                    new_allowed=(refd_cls.interactive_create_command
                                 is not None))
                if refd_model is None:
                    return
                elif refd_model == "NEW":
                    self._root.onecmd(refd_cls.interactive_create_command)
                    continue
                else:
                    break

            if not is_shim:
                setattr(self._model, field_name, refd_model.id)
            else:
                try:
                    self._shim_fields[field_name]["setter"](self._model,
                                                            refd_model.id)
                except Exception as e:
                    self.err(e.message)
                    return

            # update the code ref
            if (isinstance(self._model, Job) and field_name == "task"):
                self._model.params = refd_model.params
                self._update_code_and_param_cmd()
            elif (isinstance(self._model, Task) and field_name == "tool"):
                self._update_code_and_param_cmd()
        else:
            if len(args) < 2:
                print("Error, you must supply a value to set {!r}".format(
                    field_name))
                return

            if isinstance(field_cls.value, (list, tuple)):
                field_value = args[1:]
            else:
                field_value = args[1]

            field_value = field_cls.cast(field_value)
            if not field_cls.validate(field_value):
                self.err("Invalid value")
                return

            if not is_shim:
                setattr(self._model, field_name, field_value)
            else:
                try:
                    self._shim_fields[field_name]["setter"](self._model,
                                                            field_value)
                except Exception as e:
                    self.err(e.message)
                    return
Exemplo n.º 4
0
    def do_set(self, args):
        """Set parameter
        """

        orig_args = args
        args = shlex.split(args)

        if len(args) == 0:
            print("wat - you must supply a fieldname to set")
            return

        field_name = args[0]

        if field_name == "params":
            self._param_cmd.prompt = self.prompt[:-2] + ":params> "
            self._param_cmd.cmdloop("\nEditing params\n")
            return

        if field_name not in self._model.fields and field_name not in self._shim_fields:
            self.err("Field '{}' does not exist. Try again".format(field_name))
            return

        is_shim = False
        if field_name in self._model.fields:
            field_cls = self._model.fields[field_name]
        else:
            is_shim = True
            field_cls = self._shim_fields[field_name]["type"]

        if isinstance(field_cls, RefField):
            refd_cls = field_cls.get_ref_cls()
            search = field_cls.search
            if search is None:
                search = {}

            while True:
                refd_model = utils.model_prompt(
                    refd_cls.objects(api_base=None, **search),
                    "Which one should {!r} be set to?".format(field_name),
                    new_allowed=(refd_cls.interactive_create_command is not None),
                )
                if refd_model is None:
                    return
                elif refd_model == "NEW":
                    self._root.onecmd(refd_cls.interactive_create_command)
                    continue
                else:
                    break

            if not is_shim:
                setattr(self._model, field_name, refd_model.id)
            else:
                try:
                    self._shim_fields[field_name]["setter"](self._model, refd_model.id)
                except Exception as e:
                    self.err(e.message)
                    return

            # update the code ref
            if isinstance(self._model, Job) and field_name == "task":
                self._model.params = refd_model.params
                self._update_code_and_param_cmd()
            elif isinstance(self._model, Task) and field_name == "tool":
                self._update_code_and_param_cmd()
        else:
            if len(args) < 2:
                print("Error, you must supply a value to set {!r}".format(field_name))
                return

            if isinstance(field_cls.value, (list, tuple)):
                field_value = args[1:]
            else:
                field_value = args[1]

            field_value = field_cls.cast(field_value)
            if not field_cls.validate(field_value):
                self.err("Invalid value")
                return

            if not is_shim:
                setattr(self._model, field_name, field_value)
            else:
                try:
                    self._shim_fields[field_name]["setter"](self._model, field_value)
                except Exception as e:
                    self.err(e.message)
                    return