Example #1
0
    def _build_value_type(self, context):
        s = self.parameter
        value_type = s.value_type
        default = s.default
        if value_type is None:
            if is_defined(default):
                value_type = get_value_type_of_obj(default)
                if value_type is None:
                    raise friendly_error.task_parameters.no_value_type_from_default(
                        default, context=context)
            elif value_type is None:  # we don't have value type! let's fail!
                if s.load_on_build is False:  # we are in data mode
                    s.value_type = TargetValueType
                else:
                    raise friendly_error.task_parameters.no_value_type_defined_in_parameter(
                        context=context)
        else:
            # let validate that what we have can be ValueType!
            resolved_value_type = get_value_type_of_type(value_type)
            if resolved_value_type is None:  # we don't have value type! let's fail!
                raise friendly_error.task_parameters.unknown_value_type_in_parameter(
                    value_type)
            value_type = resolved_value_type  # type: ValueType

        if s.sub_type:
            sub_value_type = get_value_type_of_type(s.sub_type)
            if isinstance(value_type, _StructureValueType):
                value_type = value_type.__class__(
                    sub_value_type=sub_value_type)
            else:
                raise friendly_error.task_parameters.sub_type_with_non_structural_value(
                    context=context,
                    value_type=value_type,
                    sub_type=s.sub_type)
        return value_type
Example #2
0
def register_custom_parameter(value_type, parameter):
    value_type = get_value_type_of_type(value_type, inline_value_type=True)

    _PARAMS_MAPPER.register_custom_parameter(value_type, parameter)
    get_types_registry().register_value_type(value_type)

    return parameter
Example #3
0
    def _get_result_parameter_part(self, p, name_hint, value_type_hint):
        if isinstance(p, six.string_types):
            # we got a string  - it's name of parameter
            name_hint = p
            p = None
        elif not isinstance(p, ParameterFactory):
            # we got a type  - it's inline type of parameter
            value_type_hint = get_value_type_of_type(p, inline_value_type=True)
            p = None
        else:
            pass

        if p is None:
            if value_type_hint:
                # we create based on value type
                p = get_parameter_for_value_type(value_type_hint)
            else:
                p = PARAMETER_FACTORY

        if p.parameter.name is None and name_hint:
            p = p.modify(name=name_hint)

        if p.parameter.value_type is None and value_type_hint:
            p = p.modify(value_type=value_type_hint)

        return p.output
Example #4
0
    def __getitem__(self, type_):
        # we want special treatment for Config classes
        if TaskEssence.CONFIG.is_included(type_):
            return self.nested_config(type_)

        value_type = get_value_type_of_type(type_, inline_value_type=True)
        if not value_type:
            raise unknown_value_type_in_parameter(type_)
        return self.modify(value_type=value_type)
Example #5
0
    def __getitem__(self, type_):
        # we want special treatment for Config classes
        if _ConfigParamContainer.is_type_config(type_):
            return self.nested_config(type_)

        value_type = get_value_type_of_type(type_, inline_value_type=True)
        if not value_type:
            raise unknown_value_type_in_parameter(type_)
        return self.modify(value_type=value_type)
Example #6
0
    def get_value_target_config(self, value_type):
        # type: (Type) -> TargetConfig

        type_handler = get_value_type_of_type(value_type)
        for possible_option in [str(type_handler), type_handler.config_name]:
            config_value = config.get_config_value(
                section=self._conf__task_family, key=possible_option)
            if config_value:
                return parse_target_config(config_value.value)
        return file.pickle
Example #7
0
def get_marshaller_ctrl(target, value_type, config=None):
    config = config or target.config

    value_type = get_value_type_of_type(value_type, inline_value_type=True)
    marshaller_options = MARSHALERS.get(value_type.type, {})
    object_marshaller_options = MARSHALERS.get(object)

    target_file_format = config.format
    if target_file_format is None:
        target_file_format = FileFormat.txt
        logger.warning("Can not find file type of '%s', assuming it's .txt",
                       target)
        # raise friendly_error.targets.no_format(
        #     target=target,
        #     options_message=_marshaller_options_message(
        #         value_type=value_type,
        #         value_options=marshaller_options,
        #         object_options=object_marshaller_options,
        #     ),
        # )

    marshaller = marshaller_options.get(target_file_format)
    if not marshaller:
        # let's try to check marshallers for object
        marshaller = object_marshaller_options.get(target_file_format)
        if not marshaller:
            raise friendly_error.targets.no_marshaller(
                target=target,
                value_type=value_type,
                config=config,
                options_message=_marshaller_options_message(
                    value_type=value_type,
                    value_options=marshaller_options,
                    object_options=object_marshaller_options,
                ),
            )
        # our value type is object!
        # may be we should print warning here?
        value_type = get_value_type_of_type(object)
    return MarshallerCtrl(target=target,
                          marshaller=marshaller,
                          value_type=value_type)
Example #8
0
    def dump(self, value, value_type=None, **kwargs):
        from targets.values import get_value_type_of_obj, get_value_type_of_type
        from targets.values import ObjectValueType

        if value_type:
            value_type = get_value_type_of_type(value_type)
        else:
            value_type = get_value_type_of_obj(value, ObjectValueType())
        try:
            m = get_marshaller_ctrl(self, value_type=value_type)
            m.dump(value, **kwargs)
        except Exception as ex:
            raise friendly_error.failed_to_write_task_output(
                ex, self, value_type=value_type
            )
        cache_key = TargetCacheKey(target=self, value_type=value_type)
        TARGET_CACHE[cache_key] = value
Example #9
0
 def type(self, type_):
     value_type = get_value_type_of_type(type_)
     if not value_type:
         value_type = InlineValueType(type_=type_)
     return self.modify(value_type=value_type)
Example #10
0
 def dump(self, value, value_type=None, **kwargs):
     self._obj = value
     if value_type:
         self.value_type = get_value_type_of_type(value_type)
     else:
         self.value_type = get_value_type_of_obj(value, ObjectValueType())