diff --git a/.prospector.yaml b/.prospector.yaml index b2bd4a9c5..9ccdb82e9 100644 --- a/.prospector.yaml +++ b/.prospector.yaml @@ -5,18 +5,19 @@ pylint: options: max-line-length: 110 disable: - - abstract-method + - too-many-instance-attributes + - too-many-ancestors + - too-many-return-statements + - too-many-branches - too-many-arguments + - too-many-locals + - too-few-public-methods + - wrong-import-order + - no-else-return + - abstract-method - invalid-name - redefined-builtin - broad-except - - too-many-locals - - no-else-return - - wrong-import-order - - too-many-instance-attributes - - too-many-ancestors - - too-few-public-methods - - too-many-return-statements - cyclic-import # see: https://github.com/PyCQA/pylint/issues/850 pep8: diff --git a/tc-copy b/tc-copy index 543f73da8..f6077e28e 100755 --- a/tc-copy +++ b/tc-copy @@ -72,7 +72,7 @@ def main(argv): try: output_tilestore = TileStore.load(args[-1]) for arg in args[:-1]: - input_tilestore = TileStore.load(arg) + input_tilestore = TileStore.load(arg, allows_no_contenttype=options.add_content_type) if bounding_pyramid: tilestream = BoundingPyramidTileStore(bounding_pyramid).list() else: diff --git a/tilecloud/__init__.py b/tilecloud/__init__.py index 6190e3b99..847cf873f 100644 --- a/tilecloud/__init__.py +++ b/tilecloud/__init__.py @@ -694,7 +694,7 @@ def put_one(tile): raise NotImplementedError @staticmethod - def load(name): # pragma: no cover + def load(name, allows_no_contenttype=False): # pragma: no cover """ Construct a :class:`TileStore` from a name. @@ -745,7 +745,7 @@ def load(name): # pragma: no cover from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.url import URLTileStore - return URLTileStore((TemplateTileLayout(name),)) + return URLTileStore((TemplateTileLayout(name),), allows_no_contenttype=allows_no_contenttype) if name.startswith("memcached://"): from tilecloud.layout.template import TemplateTileLayout from tilecloud.lib.memcached import MemcachedClient diff --git a/tilecloud/store/url.py b/tilecloud/store/url.py index b977c8d7d..8df96c36b 100644 --- a/tilecloud/store/url.py +++ b/tilecloud/store/url.py @@ -8,8 +8,9 @@ class URLTileStore(TileStore): - def __init__(self, tilelayouts, headers=None, **kwargs): + def __init__(self, tilelayouts, headers=None, allows_no_contenttype=False, **kwargs): TileStore.__init__(self, **kwargs) + self.allows_no_contenttype = allows_no_contenttype self.tilelayouts = tuple(tilelayouts) self.session = requests.session() if headers is not None: @@ -29,16 +30,26 @@ def get_one(self, tile): logger.info("GET %s", url) try: response = self.session.get(url) - if response.status_code == 404: + if response.status_code == 404 or response.status_code == 204: return None tile.content_encoding = response.headers.get("Content-Encoding") tile.content_type = response.headers.get("Content-Type") if response.status_code < 300: - tile.data = response.content - if tile.content_type.startswith("image/"): - tile.data = response.content + if response.status_code != 200: + tile.error = "Unsupportetd status code {}: {}".format( + 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: - tile.error = response.text + 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: