Example #1
0
    def parse_value(self, value, load_value=None, target_config=None):
        """
        Parse an individual value from the input.

        probably this is the most important code in user value parsing
        :param str value: the value to parse.
        :return: the parsed value.
        """

        from dbnd._core.utils.task_utils import to_targets
        from targets.inmemory_target import InMemoryTarget
        from targets.values.target_values import _TargetValueType

        if load_value is None:
            load_value = self.load_on_build

        value = self._interpolate_from_str(value)
        if value is None:
            return value

        if isinstance(value, six.string_types):
            # we are in the string mode
            # it's can be "serialized to string" or path value
            if load_value:
                # we can just load value from string
                if self.support_from_str:
                    value = self.parse_from_str(value)
                    value = self.normalize(value)
                    return value

            # otherwise - the value is a path!
            target_kwargs = {}
            if target_config:
                target_kwargs["config"] = target_config

            return to_targets(json_utils.loads(value),
                              from_string_kwargs=target_kwargs)

        from dbnd._core.task import Task
        from targets import Target

        if isinstance(value, Task):
            return to_targets(value)

        if isinstance(value, Target):
            return value

        # so we have a value that is obviously "Data" type,
        # we want to be able to supporet "load_value" behaviour
        if not load_value and not isinstance(self, _TargetValueType):
            return InMemoryTarget(value, value_type=self)

        value = self.normalize(value)
        return value
Example #2
0
    def convert(self, value, param, ctx):
        from dbnd._core.utils import json_utils
        from targets.values.structure import _PARSABLE_PARAM_PREFIX

        value = value.strip()
        # support for --set '{ "a":2, "b":33}'
        if value[0] in _PARSABLE_PARAM_PREFIX:
            return json_utils.loads(value)

        name, sep, var = value.partition("=")
        return {name: var}
Example #3
0
    def parse_from_str(self, x):
        """
               Parses an immutable and ordered ``dict`` from a JSON string using standard JSON library.
        Parse an individual value from the input.

        """

        # if isinstance(value, Mapping):
        #     # we are good to go, it'x dictionary already
        #     return value
        if not x:
            return self._generate_empty_default()

        # this is string and we need to parse it
        if not isinstance(x, six.string_types):
            raise DatabandConfigError(
                "Can't parse '%x' into parameter. Value should be string" % x
            )

        x = x.strip()
        if not x:
            return self._generate_empty_default()

        if x[0] in _PARSABLE_PARAM_PREFIX:
            value = json_utils.loads(x)
        else:
            value = self._parse_from_str_simple(x)

            if not self.is_type_of(value):
                raise DatabandConfigError(
                    "Can't parse '%s' into %s" % (value, self.type)
                )
        if self.sub_value_type:
            value = traverse(value, self.sub_value_type.parse_value)

        return value