Ejemplo n.º 1
0
def test_vcf_data_file_identical():
    with tempdir() as temp:
        localizer1 = StringLocalizer("##fileformat=VCFv4.2\n"
                                     "chr1\t1111\t.\tA\tG\t1000\tPASS\t.\tGT")
        v1 = VcfDataFile(temp / "foo1.vcf", localizer1)
        localizer2 = StringLocalizer("##fileformat=VCFv4.2\n"
                                     "chr1\t1111\t.\tA\tG\t999.9\tPASS\t.")
        v2 = VcfDataFile(temp / "foo2.vcf", localizer2)
        v1.assert_contents_equal(v2)
Ejemplo n.º 2
0
def test_vcf_data_file_different():
    with tempdir() as temp:
        localizer1 = StringLocalizer("##fileformat=VCFv4.2\n"
                                     "chr1\t1111\t.\tA\tG\t1000\tPASS\t.")
        v1 = VcfDataFile(temp / "foo1.vcf", localizer1, allowed_diff_lines=0)
        localizer2 = StringLocalizer("##fileformat=VCFv4.2\n"
                                     "chr1\t1111\t.\tA\tG\t999.9\tPASS\t.\n"
                                     "chr1\t2222\t.\tT\tC\t500\tPASS\t.")
        v2 = VcfDataFile(temp / "foo2.vcf", localizer2)
        with pytest.raises(AssertionError):
            v1.assert_contents_equal(v2)
        v1.set_compare_opts(allowed_diff_lines=1)
        v1.assert_contents_equal(v2)
Ejemplo n.º 3
0
def create_data_file(user_config: UserConfiguration,
                     type: Optional[Union[str, dict]] = DEFAULT_TYPE,
                     name: Optional[str] = None,
                     path: Optional[Union[str, Path]] = None,
                     url: Optional[str] = None,
                     contents: Optional[Union[str, dict]] = None,
                     env: Optional[str] = None,
                     http_headers: Optional[dict] = None,
                     digests: Optional[dict] = None,
                     datadirs: Optional[DataDirs] = None,
                     **kwargs) -> DataFile:
    if isinstance(type, dict):
        data_file_opts = cast(dict, type)
        type = data_file_opts.pop("name")
    else:
        data_file_opts = {}

    data_file_opts.update(kwargs)

    local_path = None
    localizer = None

    if path:
        local_path = ensure_path(path, [user_config.cache_dir])

    if local_path and local_path.exists():
        pass
    elif env and env in os.environ:
        env_path = ensure_path(os.environ[env], exists=True)
        if not local_path:
            local_path = env_path
        else:
            localizer = LinkLocalizer(env_path)
    elif url:
        localizer = UrlLocalizer(url, user_config, http_headers, digests)
        if not local_path:
            if name:
                local_path = ensure_path(user_config.cache_dir / name)
            else:
                filename = url.rsplit("/", 1)[1]
                local_path = ensure_path(user_config.cache_dir / filename)
    elif contents:
        if isinstance(contents, str):
            localizer = StringLocalizer(cast(str, contents))
        else:
            localizer = JsonLocalizer(cast(dict, contents))
            if type == DEFAULT_TYPE:
                type = "json"
        if not local_path:
            if name:
                local_path = ensure_path(user_config.cache_dir / name)
            else:
                local_path = ensure_path(
                    tempfile.mktemp(dir=user_config.cache_dir))
    elif name and datadirs:
        for dd in datadirs.paths:
            dd_path = dd / name
            if dd_path.exists():
                break
        else:
            raise FileNotFoundError(
                f"File {name} not found in any of the following datadirs: "
                f"{datadirs.paths}")

        if not local_path:
            local_path = dd_path
        else:
            localizer = LinkLocalizer(dd_path)
    else:
        raise FileNotFoundError(
            f"File {path or name} does not exist. Either a url, file contents, "
            f"or a local file must be provided.")

    data_file_class = DATA_TYPES.get(type, DefaultDataFile)
    return data_file_class(local_path, localizer, **data_file_opts)
Ejemplo n.º 4
0
def test_string_localizer():
    with tempdir() as d:
        foo = d / "foo"
        StringLocalizer("foo").localize(foo)
        with open(foo, "rt") as inp:
            assert inp.read() == "foo"