Esempio n. 1
0
 def tryLoad(cls, filesystem: JsonableFilesystem,
             path: PurePosixPath) -> "PrecomputedChunksInfo | Exception":
     url = filesystem.geturl(path.as_posix())
     if not filesystem.exists(path.as_posix()):
         return FileNotFoundError(f"Could not find info file at {url}")
     with filesystem.openbin(path.as_posix(), "r") as f:
         try:
             info_json = f.read().decode("utf8")
             return PrecomputedChunksInfo.from_json_value(
                 json.loads(info_json))
         except Exception:
             return ValueError(
                 f"Could not interpret json info file at {url}")
Esempio n. 2
0
    def create(
        cls,
        *,
        filesystem: JsonableFilesystem,
        base_path:
        Path,  # path to the "directory" that should contain the info file
        info: PrecomputedChunksInfo,
    ) -> "PrecomputedChunksSink":
        if filesystem.exists(base_path.as_posix()):
            filesystem.removedir(base_path.as_posix())
        filesystem.makedirs(base_path.as_posix())

        with filesystem.openbin(base_path.joinpath("info").as_posix(),
                                "w") as info_file:
            info_file.write(json.dumps(info.to_json_value()).encode("utf8"))

        for scale in info.scales_5d:
            scale_path = base_path.joinpath(scale.key.as_posix().lstrip("/"))
            filesystem.makedirs(scale_path.as_posix())

        return PrecomputedChunksSink(filesystem=filesystem,
                                     base_path=base_path,
                                     info=info)