예제 #1
0
 def write(self, gzip: bool = None):
     """Write mosaicjson document to a file."""
     body = self.mosaic_def.dict(exclude_none=True)
     with open(self.path, "wb") as f:
         if gzip or (gzip is None and self.path.endswith(".gz")):
             f.write(_compress_gz_json(body))
         else:
             f.write(json.dumps(body).encode("utf-8"))
def test_compress():
    """Test valid gz compression."""
    with open(mosaic_json, "r") as f:
        mosaic = json.loads(f.read())

    body = utils._compress_gz_json(mosaic)
    assert type(body) == bytes
    res = json.loads(utils._decompress_gz(body))
    assert res == mosaic
예제 #3
0
    def write(self, gzip: bool = None, **kwargs: Any):
        """Write mosaicjson document to AWS S3."""
        mosaic_doc = self.mosaic_def.dict(exclude_none=True)
        if gzip or (gzip is None and self.key.endswith(".gz")):
            body = _compress_gz_json(mosaic_doc)
        else:
            body = json.dumps(mosaic_doc).encode("utf-8")

        _aws_put_data(self.key, self.bucket, body, client=self.client, **kwargs)
예제 #4
0
    def write(self, overwrite: bool = False, **kwargs: Any):
        """Write mosaicjson document to AWS S3."""
        if not overwrite and self._head_object(self.key, self.bucket):
            raise MosaicExistsError("Mosaic file already exist, use `overwrite=True`.")

        mosaic_doc = self.mosaic_def.dict(exclude_none=True)
        if self.key.endswith(".gz"):
            body = _compress_gz_json(mosaic_doc)
        else:
            body = json.dumps(mosaic_doc).encode("utf-8")

        self._put_object(self.key, self.bucket, body, **kwargs)
예제 #5
0
    def write(self, overwrite: bool = False):
        """Write mosaicjson document to a file."""
        if not overwrite and pathlib.Path(self.path).exists():
            raise MosaicExistsError(
                "Mosaic file already exist, use `overwrite=True`.")

        body = self.mosaic_def.dict(exclude_none=True)
        with open(self.path, "wb") as f:
            try:
                if self.path.endswith(".gz"):
                    f.write(_compress_gz_json(body))
                else:
                    f.write(json.dumps(body).encode("utf-8"))
            except Exception as e:
                exc = _FILE_EXCEPTIONS.get(e, MosaicError)  # type: ignore
                raise exc(str(e)) from e