示例#1
0
        def convert_v1_if_present_placholder_to_v2(
                arg: Dict[str,
                          Any]) -> Union[Dict[str, Any], ValidCommandArgs]:
            if isinstance(arg, str):
                arg = try_to_get_dict_from_string(arg)
            if not isinstance(arg, dict):
                return arg

            if 'if' in arg:
                if_placeholder_values = arg['if']
                if_placeholder_values_then = list(
                    if_placeholder_values['then'])
                try:
                    if_placeholder_values_else = list(
                        if_placeholder_values['else'])
                except KeyError:
                    if_placeholder_values_else = []
                return IfPresentPlaceholder(
                    if_structure=IfPresentPlaceholderStructure(
                        input_name=utils.sanitize_input_name(
                            if_placeholder_values['cond']['isPresent']),
                        then=[
                            convert_str_or_dict_to_placeholder(val)
                            for val in if_placeholder_values_then
                        ],
                        otherwise=[
                            convert_str_or_dict_to_placeholder(val)
                            for val in if_placeholder_values_else
                        ]))

            elif 'concat' in arg:

                return ConcatPlaceholder(items=[
                    convert_str_or_dict_to_placeholder(val)
                    for val in arg['concat']
                ])
            elif isinstance(arg, (ValidCommandArgTypes, dict)):
                return arg
            else:
                raise TypeError(
                    f'Unexpected argument {arg} of type {type(arg)}.')
示例#2
0
        def _transform_arg(
                arg: Union[str, Dict[str, str]]) -> ValidCommandArgs:
            if isinstance(arg, str):
                return arg
            if 'inputValue' in arg:
                return InputValuePlaceholder(
                    input_name=utils.sanitize_input_name(arg['inputValue']))
            if 'inputPath' in arg:
                return InputPathPlaceholder(
                    input_name=utils.sanitize_input_name(arg['inputPath']))
            if 'inputUri' in arg:
                return InputUriPlaceholder(
                    input_name=utils.sanitize_input_name(arg['inputUri']))
            if 'outputPath' in arg:
                return OutputPathPlaceholder(
                    output_name=utils.sanitize_input_name(arg['outputPath']))
            if 'outputUri' in arg:
                return OutputUriPlaceholder(
                    output_name=utils.sanitize_input_name(arg['outputUri']))
            if 'if' in arg:
                if_placeholder_values = arg['if']
                if_placeholder_values_then = list(
                    if_placeholder_values['then'])
                try:
                    if_placeholder_values_else = list(
                        if_placeholder_values['else'])
                except KeyError:
                    if_placeholder_values_else = []

                IfPresentPlaceholderStructure.update_forward_refs()
                return IfPresentPlaceholder(
                    if_structure=IfPresentPlaceholderStructure(
                        input_name=utils.sanitize_input_name(
                            if_placeholder_values['cond']['isPresent']),
                        then=list(
                            _transform_arg(val)
                            for val in if_placeholder_values_then),
                        otherwise=list(
                            _transform_arg(val)
                            for val in if_placeholder_values_else)))
            if 'concat' in arg:
                ConcatPlaceholder.update_forward_refs()

                return ConcatPlaceholder(concat=list(
                    _transform_arg(val) for val in arg['concat']))
            raise ValueError(
                f'Unexpected command/argument type: "{arg}" of type "{type(arg)}".'
            )
示例#3
0
    def from_v1_component_spec(
            cls,
            v1_component_spec: v1_structures.ComponentSpec) -> 'ComponentSpec':
        """Converts V1 ComponentSpec to V2 ComponentSpec.

        Args:
            v1_component_spec: The V1 ComponentSpec.

        Returns:
            Component spec in the form of V2 ComponentSpec.

        Raises:
            ValueError: If implementation is not found.
            TypeError: if any argument is neither a str nor Dict.
        """
        component_dict = v1_component_spec.to_dict()
        if component_dict.get('implementation') is None:
            raise ValueError('Implementation field not found')
        if 'container' not in component_dict.get('implementation'):
            raise NotImplementedError

        def _transform_arg(
                arg: Union[str, Dict[str, str]]) -> ValidCommandArgs:
            if isinstance(arg, str):
                return arg
            if 'inputValue' in arg:
                return InputValuePlaceholder(
                    input_name=utils.sanitize_input_name(arg['inputValue']))
            if 'inputPath' in arg:
                return InputPathPlaceholder(
                    input_name=utils.sanitize_input_name(arg['inputPath']))
            if 'inputUri' in arg:
                return InputUriPlaceholder(
                    input_name=utils.sanitize_input_name(arg['inputUri']))
            if 'outputPath' in arg:
                return OutputPathPlaceholder(
                    output_name=utils.sanitize_input_name(arg['outputPath']))
            if 'outputUri' in arg:
                return OutputUriPlaceholder(
                    output_name=utils.sanitize_input_name(arg['outputUri']))
            if 'if' in arg:
                if_placeholder_values = arg['if']
                if_placeholder_values_then = list(
                    if_placeholder_values['then'])
                try:
                    if_placeholder_values_else = list(
                        if_placeholder_values['else'])
                except KeyError:
                    if_placeholder_values_else = []

                IfPresentPlaceholderStructure.update_forward_refs()
                return IfPresentPlaceholder(
                    if_structure=IfPresentPlaceholderStructure(
                        input_name=utils.sanitize_input_name(
                            if_placeholder_values['cond']['isPresent']),
                        then=list(
                            _transform_arg(val)
                            for val in if_placeholder_values_then),
                        otherwise=list(
                            _transform_arg(val)
                            for val in if_placeholder_values_else)))
            if 'concat' in arg:
                ConcatPlaceholder.update_forward_refs()

                return ConcatPlaceholder(concat=list(
                    _transform_arg(val) for val in arg['concat']))
            raise ValueError(
                f'Unexpected command/argument type: "{arg}" of type "{type(arg)}".'
            )

        implementation = component_dict['implementation']['container']
        implementation['command'] = [
            _transform_arg(command)
            for command in implementation.pop('command', [])
        ]
        implementation['args'] = [
            _transform_arg(command)
            for command in implementation.pop('args', [])
        ]
        implementation['env'] = {
            key: _transform_arg(command)
            for key, command in implementation.pop('env', {}).items()
        }

        container_spec = ContainerSpec(image=implementation['image'])

        # Workaround for https://github.com/samuelcolvin/pydantic/issues/2079
        def _copy_model(obj):
            if isinstance(obj, BaseModel):
                return obj.copy(deep=True)
            return obj

        # Must assign these after the constructor call, otherwise it won't work.
        if implementation['command']:
            container_spec.command = [
                _copy_model(cmd) for cmd in implementation['command']
            ]
        if implementation['args']:
            container_spec.args = [
                _copy_model(arg) for arg in implementation['args']
            ]
        if implementation['env']:
            container_spec.env = {
                k: _copy_model(v)
                for k, v in implementation['env']
            }

        return ComponentSpec(
            name=component_dict.get('name', 'name'),
            description=component_dict.get('description'),
            implementation=Implementation(container=container_spec),
            inputs={
                utils.sanitize_input_name(spec['name']):
                InputSpec(type=spec.get('type', 'Artifact'),
                          default=spec.get('default', None))
                for spec in component_dict.get('inputs', [])
            },
            outputs={
                utils.sanitize_input_name(spec['name']):
                OutputSpec(type=spec.get('type', 'Artifact'))
                for spec in component_dict.get('outputs', [])
            })
示例#4
0
    def from_v1_component_spec(
            cls,
            v1_component_spec: v1_structures.ComponentSpec) -> 'ComponentSpec':
        """Converts V1 ComponentSpec to V2 ComponentSpec.

        Args:
            v1_component_spec: The V1 ComponentSpec.

        Returns:
            Component spec in the form of V2 ComponentSpec.

        Raises:
            ValueError: If implementation is not found.
            TypeError: if any argument is neither a str nor Dict.
        """
        component_dict = v1_component_spec.to_dict()
        if component_dict.get('implementation') is None:
            raise ValueError('Implementation field not found')

        if 'container' not in component_dict.get(
                'implementation'):  # type: ignore
            raise NotImplementedError

        def convert_v1_if_present_placholder_to_v2(
                arg: Dict[str,
                          Any]) -> Union[Dict[str, Any], ValidCommandArgs]:
            if isinstance(arg, str):
                arg = try_to_get_dict_from_string(arg)
            if not isinstance(arg, dict):
                return arg

            if 'if' in arg:
                if_placeholder_values = arg['if']
                if_placeholder_values_then = list(
                    if_placeholder_values['then'])
                try:
                    if_placeholder_values_else = list(
                        if_placeholder_values['else'])
                except KeyError:
                    if_placeholder_values_else = []
                return IfPresentPlaceholder(
                    if_structure=IfPresentPlaceholderStructure(
                        input_name=utils.sanitize_input_name(
                            if_placeholder_values['cond']['isPresent']),
                        then=[
                            convert_str_or_dict_to_placeholder(val)
                            for val in if_placeholder_values_then
                        ],
                        otherwise=[
                            convert_str_or_dict_to_placeholder(val)
                            for val in if_placeholder_values_else
                        ]))

            elif 'concat' in arg:

                return ConcatPlaceholder(items=[
                    convert_str_or_dict_to_placeholder(val)
                    for val in arg['concat']
                ])
            elif isinstance(arg, (ValidCommandArgTypes, dict)):
                return arg
            else:
                raise TypeError(
                    f'Unexpected argument {arg} of type {type(arg)}.')

        implementation = component_dict['implementation']['container']
        implementation['command'] = [
            convert_v1_if_present_placholder_to_v2(command)
            for command in implementation.pop('command', [])
        ]
        implementation['args'] = [
            convert_v1_if_present_placholder_to_v2(command)
            for command in implementation.pop('args', [])
        ]
        implementation['env'] = {
            key: convert_v1_if_present_placholder_to_v2(command)
            for key, command in implementation.pop('env', {}).items()
        }
        container_spec = ContainerSpec(image=implementation['image'])

        # Must assign these after the constructor call, otherwise it won't work.
        if implementation['command']:
            container_spec.command = implementation['command']
        if implementation['args']:
            container_spec.args = implementation['args']
        if implementation['env']:
            container_spec.env = implementation['env']

        return ComponentSpec(
            name=component_dict.get('name', 'name'),
            description=component_dict.get('description'),
            implementation=Implementation(container=container_spec),
            inputs={
                utils.sanitize_input_name(spec['name']):
                InputSpec(type=spec.get('type', 'Artifact'),
                          default=spec.get('default', None))
                for spec in component_dict.get('inputs', [])
            },
            outputs={
                utils.sanitize_input_name(spec['name']):
                OutputSpec(type=spec.get('type', 'Artifact'))
                for spec in component_dict.get('outputs', [])
            })
示例#5
0
def convert_str_or_dict_to_placeholder(
    element: Union[str, dict,
                   ValidCommandArgs]) -> Union[str, ValidCommandArgs]:
    """Converts command and args elements to a placholder type based on value
    of the key of the placeholder string, else returns the input.

    Args:
        element (Union[str, dict, ValidCommandArgs]): A ContainerSpec.command or ContainerSpec.args element.

    Raises:
        TypeError: If `element` is invalid.

    Returns:
        Union[str, ValidCommandArgs]: Possibly converted placeholder or original input.
    """

    if not isinstance(element, (dict, str)):
        return element

    elif isinstance(element, str):
        res = try_to_get_dict_from_string(element)
        if not isinstance(res, dict):
            return element

    elif isinstance(element, dict):
        res = element
    else:
        raise TypeError(
            f'Invalid type for arg: {type(element)}. Expected str or dict.')

    has_one_entry = len(res) == 1

    if not has_one_entry:
        raise ValueError(
            f'Got unexpected dictionary {res}. Expected a dictionary with one entry.'
        )

    first_key = list(res.keys())[0]
    first_value = list(res.values())[0]
    if first_key == 'inputValue':
        return InputValuePlaceholder(
            input_name=utils.sanitize_input_name(first_value))

    elif first_key == 'inputPath':
        return InputPathPlaceholder(
            input_name=utils.sanitize_input_name(first_value))

    elif first_key == 'inputUri':
        return InputUriPlaceholder(
            input_name=utils.sanitize_input_name(first_value))

    elif first_key == 'outputPath':
        return OutputPathPlaceholder(
            output_name=utils.sanitize_input_name(first_value))

    elif first_key == 'outputUri':
        return OutputUriPlaceholder(
            output_name=utils.sanitize_input_name(first_value))

    elif first_key == 'ifPresent':
        structure_kwargs = res['ifPresent']
        structure_kwargs['input_name'] = structure_kwargs.pop('inputName')
        structure_kwargs['otherwise'] = structure_kwargs.pop('else')
        structure_kwargs['then'] = [
            convert_str_or_dict_to_placeholder(e)
            for e in structure_kwargs['then']
        ]
        structure_kwargs['otherwise'] = [
            convert_str_or_dict_to_placeholder(e)
            for e in structure_kwargs['otherwise']
        ]
        if_structure = IfPresentPlaceholderStructure(**structure_kwargs)

        return IfPresentPlaceholder(if_structure=if_structure)

    elif first_key == 'concat':
        return ConcatPlaceholder(items=[
            convert_str_or_dict_to_placeholder(e) for e in res['concat']
        ])

    else:
        raise TypeError(
            f'Unexpected command/argument type: "{element}" of type "{type(element)}".'
        )