Example #1
0
def test_yaml_filepath_validate_bad(val):
    assert not is_yaml_filepath(val)
Example #2
0
def parse_config_source(path: Union[str, TextIO, Dict],
                        allow_stream: bool = True,
                        allow_yaml_file: bool = True,
                        allow_builtin_resource: bool = True,
                        allow_raw_yaml_content: bool = True,
                        allow_raw_driver_yaml_content: bool = True,
                        allow_class_type: bool = True,
                        allow_dict: bool = True,
                        allow_json: bool = True,
                        *args,
                        **kwargs) -> Tuple[TextIO, Optional[str]]:
    """Check if the text or text stream is valid.

    # noqa: DAR401
    :param path: the multi-kind source of the configs.
    :param allow_stream: flag
    :param allow_yaml_file: flag
    :param allow_builtin_resource: flag
    :param allow_raw_yaml_content: flag
    :param allow_raw_driver_yaml_content: flag
    :param allow_class_type: flag
    :param allow_dict: flag
    :param allow_json: flag
    :param *args: *args
    :param **kwargs: **kwargs
    :return: a tuple, the first element is the text stream, the second element is the file path associate to it
            if available.
    """
    import io
    from pkg_resources import resource_filename
    if not path:
        raise BadConfigSource
    elif allow_dict and isinstance(path, dict):
        from . import JAML
        tmp = JAML.dump(path)
        return io.StringIO(tmp), None
    elif allow_stream and hasattr(path, 'read'):
        # already a readable stream
        return path, None
    elif allow_yaml_file and is_yaml_filepath(path):
        comp_path = complete_path(path)
        return open(comp_path, encoding='utf8'), comp_path
    elif allow_builtin_resource and path.lstrip(
    ).startswith('_') and os.path.exists(
            resource_filename('jina', '/'.join(
                ('resources', f'executors.{path}.yml')))):
        # NOTE: this returns a binary stream
        comp_path = resource_filename(
            'jina', '/'.join(('resources', f'executors.{path}.yml')))
        return open(comp_path, encoding='utf8'), comp_path
    elif allow_raw_yaml_content and path.lstrip().startswith('!'):
        # possible YAML content
        path = path.replace('|', '\n    with: ')
        return io.StringIO(path), None
    elif allow_raw_driver_yaml_content and path.lstrip().startswith('- !'):
        # possible driver YAML content, right now it is only used for debugging
        with open(
                resource_filename(
                    'jina', '/'.join(('resources', 'executors.base.all.yml'
                                      if path.lstrip().startswith('- !!') else
                                      'executors.base.yml')))) as fp:
            _defaults = fp.read()
        path = path.replace('- !!', '- !').replace(
            '|', '\n        with: ')  # for indent, I know, its nasty
        path = _defaults.replace('*', path)
        return io.StringIO(path), None
    elif allow_class_type and path.isidentifier():
        # possible class name
        return io.StringIO(f'!{path}'), None
    elif allow_json and isinstance(path, str):
        try:
            from . import JAML
            tmp = json.loads(path)
            tmp = JAML.dump(tmp)
            return io.StringIO(tmp), None
        except json.JSONDecodeError:
            raise BadConfigSource(path)
    else:
        raise BadConfigSource(
            f'{path} can not be resolved, it should be a readable stream,'
            ' or a valid file path, or a supported class name.')
Example #3
0
def test_yaml_filepath_validate_good(val):
    assert is_yaml_filepath(val)
Example #4
0
def parse_config_source(
    path: Union[str, TextIO, Dict],
    allow_stream: bool = True,
    allow_yaml_file: bool = True,
    allow_raw_yaml_content: bool = True,
    allow_class_type: bool = True,
    allow_dict: bool = True,
    allow_json: bool = True,
    extra_search_paths: Optional[List[str]] = None,
    *args,
    **kwargs,
) -> Tuple[TextIO, Optional[str]]:
    """
    Check if the text or text stream is valid.

    .. # noqa: DAR401
    :param path: the multi-kind source of the configs.
    :param allow_stream: flag
    :param allow_yaml_file: flag
    :param allow_raw_yaml_content: flag
    :param allow_class_type: flag
    :param allow_dict: flag
    :param allow_json: flag
    :param extra_search_paths: extra paths to search for
    :param args: unused
    :param kwargs: unused
    :return: a tuple, the first element is the text stream, the second element is the file path associate to it
            if available.
    """
    import io

    if not path:
        raise BadConfigSource
    elif allow_dict and isinstance(path, dict):
        from jina.jaml import JAML

        tmp = JAML.dump(path)
        return io.StringIO(tmp), None
    elif allow_stream and hasattr(path, 'read'):
        # already a readable stream
        return path, None
    elif allow_yaml_file and is_yaml_filepath(path):
        comp_path = complete_path(path, extra_search_paths)
        return open(comp_path, encoding='utf8'), comp_path
    elif allow_raw_yaml_content and path.lstrip().startswith(('!', 'jtype')):
        # possible YAML content
        path = path.replace('|', '\n    with: ')
        return io.StringIO(path), None
    elif allow_class_type and path.isidentifier():
        # possible class name
        return io.StringIO(f'!{path}'), None
    elif allow_json and isinstance(path, str):
        try:
            from jina.jaml import JAML

            tmp = json.loads(path)
            tmp = JAML.dump(tmp)
            return io.StringIO(tmp), None
        except json.JSONDecodeError:
            raise BadConfigSource(path)
    else:
        raise BadConfigSource(
            f'{path} can not be resolved, it should be a readable stream,'
            ' or a valid file path, or a supported class name.')