コード例 #1
0
 def __attrs_post_init__(self):
     self._cache = stdpopsim.CachedData(
         namespace=f"annotations/{self.species.id}",
         url=self.zarr_url,
         sha256=self.zarr_sha256,
         extract=False,
     )
コード例 #2
0
    def __init__(
        self,
        species,
        id=None,
        url=None,
        sha256=None,
        file_pattern=None,
        description=None,
        long_description=None,
        citations=None,
    ):
        self.id = id
        self.species = species
        self.description = description
        self.long_description = long_description
        self.url = url
        self.sha256 = sha256
        self.file_pattern = file_pattern
        self.description = description
        self.citations = citations

        self._cache = stdpopsim.CachedData(
            namespace=f"genetic_maps/{self.species.id}/{id}",
            url=url,
            sha256=sha256,
            extract=True,
        )
コード例 #3
0
    def test_caching(self):
        for extract in (True, False):
            with tempfile.TemporaryDirectory() as tmpdir:
                tmpdir = pathlib.Path(tmpdir)
                with utils.cd(tmpdir):
                    filename = "test.foo"
                    with open(filename, "w") as f:
                        print("foo", file=f)
                    tar = tmpdir / "test.tgz"
                    with tarfile.open(tar, "w:gz") as tf:
                        tf.add(filename)

                sha256 = utils.sha256(tar)
                cache = stdpopsim.CachedData(
                    namespace="test",
                    url=tar.resolve().as_uri(),
                    sha256=sha256,
                    extract=extract,
                )
                self.assertFalse(cache.is_cached())
                self.assertFalse(cache.is_valid())
                cache.download()
                self.assertTrue(cache.is_cached())
                self.assertTrue(cache.is_valid())

                # try to download with incorrect checksum
                cache.sha256 = "1234"
                self.assertTrue(cache.is_cached())
                self.assertFalse(cache.is_valid())
                with self.assertRaises(ValueError):
                    # checksum mismatch
                    cache.download()
                self.assertFalse(cache.is_cached())
                self.assertFalse(cache.is_valid())

                # fix the checksum and download again
                cache.sha256 = sha256
                cache.download()
                self.assertTrue(cache.is_cached())
                self.assertTrue(cache.is_valid())
コード例 #4
0
    def test_multiple_threads_downloading(self):
        with tempfile.TemporaryDirectory() as tmpdir:
            tmpdir = pathlib.Path(tmpdir)
            with utils.cd(tmpdir):
                filename = "test.foo"
                with open(filename, "w") as f:
                    print("foo", file=f)
                tar = tmpdir / "test.tgz"
                with tarfile.open(tar, "w:gz") as tf:
                    tf.add(filename)

            cache = stdpopsim.CachedData(
                namespace="test",
                url=tar.resolve().as_uri(),
                sha256=utils.sha256(tar),
                extract=True,
            )
            cache.download()
            # Trick the download code into thinking there's several happening
            # concurrently
            cache.is_cached = lambda: False
            with self.assertWarns(UserWarning):
                cache.download()