Esempio n. 1
0
    def get(self, tiles):
        for metatile in tiles:
            metaimage = None if metatile.data is None else Image.open(
                StringIO(metatile.data))
            for tilecoord in metatile.tilecoord:
                if metatile.error:
                    tile = Tile(tilecoord)
                    tile.metadata = metatile.metadata
                    tile.error = metatile.error
                    yield tile
                    continue
                if metatile.data is None:
                    tile = Tile(tilecoord)
                    tile.metadata = metatile.metadata
                    tile.error = "Metatile data is None"
                    yield tile
                    continue

                x = self.border + (tilecoord.x -
                                   metatile.tilecoord.x) * self.tile_size
                y = self.border + (tilecoord.y -
                                   metatile.tilecoord.y) * self.tile_size
                image = metaimage.crop(
                    (x, y, x + self.tile_size, y + self.tile_size))
                string_io = StringIO()
                image.save(string_io, FORMAT_BY_CONTENT_TYPE[self.format])
                yield Tile(tilecoord,
                           data=string_io.getvalue(),
                           content_type=self.format,
                           **metatile.metadata)
Esempio n. 2
0
 def delete_one(self, tile: Tile) -> Tile:
     try:
         key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
         if not self.dry_run:
             self.client.delete_object(Bucket=self.bucket, Key=key_name)
     except botocore.exceptions.ClientError as exc:
         tile.error = exc
     return tile
Esempio n. 3
0
 def put_one(self, tile: Tile) -> Tile:
     try:
         self._master.xadd(name=self._name,
                           fields={"message": encode_message(tile)})
     except Exception as e:
         logger.warning("Failed sending Redis message", exc_info=True)
         tile.error = e
     return tile
Esempio n. 4
0
 def delete_one(self, tile: Tile) -> Tile:
     try:
         filename = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     except Exception as e:
         tile.error = e
         return tile
     if os.path.exists(filename):
         os.remove(filename)
     return tile
Esempio n. 5
0
 def delete_one(self, tile: Tile) -> Tile:
     try:
         key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
         if not self.dry_run:
             blob = self.container_client.get_blob_client(blob=key_name)
             blob.delete_blob()
     except Exception as exc:
         tile.error = exc
     return tile
Esempio n. 6
0
 def put_one(self, tile: Tile) -> Tile:
     assert isinstance(tile.data, bytes)
     try:
         filename = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     except Exception as e:
         tile.error = e
         return tile
     dirname = os.path.dirname(filename)
     if not os.path.exists(dirname):
         os.makedirs(dirname)
     with open(filename, "wb") as file:
         file.write(tile.data)
     return tile
Esempio n. 7
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     try:
         response = self.client.get_object(Bucket=self.bucket, Key=key_name)
         tile.data = response["Body"].read()
         tile.content_encoding = response.get("ContentEncoding")
         tile.content_type = response.get("ContentType")
     except botocore.exceptions.ClientError as exc:
         if _get_status(exc) == 404:
             return None
         else:
             tile.error = exc
     return tile
Esempio n. 8
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     try:
         blob = self.container_client.get_blob_client(blob=key_name)
         tile.data = blob.download_blob().readall()
         properties = blob.get_blob_properties()
         tile.content_encoding = properties.content_settings.content_encoding
         tile.content_type = properties.content_settings.content_type
     except ResourceNotFoundError:
         return None
     except Exception as exc:
         LOGGER.exception(exc)
         tile.error = exc
     return tile
Esempio n. 9
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     try:
         filename = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     except Exception as e:
         tile.error = e
         return tile
     try:
         with open(filename, "rb") as file:
             tile.data = file.read()
         if self.content_type is not None:
             tile.content_type = self.content_type
         return tile
     except OSError as e:
         if e.errno == errno.ENOENT:
             return None
         else:
             raise
Esempio n. 10
0
    def put_one(self, tile: Tile) -> Tile:
        assert tile.data is not None
        key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
        if not self.dry_run:
            try:
                blob = self.container_client.get_blob_client(blob=key_name)
                blob.upload_blob(
                    tile.data,
                    overwrite=True,
                    content_settings=ContentSettings(
                        content_type=tile.content_type,
                        content_encoding=tile.content_encoding,
                        cache_control=self.cache_control,
                    ),
                )
            except Exception as exc:
                tile.error = exc

        return tile
Esempio n. 11
0
 def put_one(self, tile: Tile) -> Tile:
     assert tile.data is not None
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     args = {}
     if tile.content_encoding is not None:
         args["ContentEncoding"] = tile.content_encoding
     if tile.content_type is not None:
         args["ContentType"] = tile.content_type
     if self.cache_control is not None:
         args["CacheControl"] = self.cache_control
     if not self.dry_run:
         try:
             self.client.put_object(ACL="public-read",
                                    Body=tile.data,
                                    Key=key_name,
                                    Bucket=self.bucket,
                                    **args)
         except botocore.exceptions.ClientError as exc:
             tile.error = exc
     return tile
Esempio n. 12
0
    def get_one(self, tile: Tile) -> Optional[Tile]:
        if tile is None:
            return None
        if self.bounding_pyramid is not None:
            if tile.tilecoord not in self.bounding_pyramid:
                return None
        tilelayout = self.tilelayouts[hash(tile.tilecoord) % len(self.tilelayouts)]
        try:
            url = tilelayout.filename(tile.tilecoord, tile.metadata)
        except Exception as e:
            tile.error = e
            return tile

        logger.info("GET %s", url)
        try:
            response = self.session.get(url)
            if response.status_code in (404, 204):
                return None
            tile.content_encoding = response.headers.get("Content-Encoding")
            tile.content_type = response.headers.get("Content-Type")
            if response.status_code < 300:
                if response.status_code != 200:
                    tile.error = f"Unsupported status code {response.status_code}: {response.reason}"
                if tile.content_type:
                    if tile.content_type.startswith("image/"):
                        tile.data = response.content
                    else:
                        tile.error = response.text
                else:
                    if self.allows_no_contenttype:
                        tile.data = response.content
                    else:
                        tile.error = "The Content-Type header is missing"

            else:
                tile.error = response.reason
        except requests.exceptions.RequestException as e:
            tile.error = e
        return tile