Skip to content

Commit

Permalink
Merge pull request #202 from bird-house/fix-199
Browse files Browse the repository at this point in the history
Add support for Path objects in guess_type
  • Loading branch information
huard authored Apr 27, 2021
2 parents 1fa66cf + 99e8557 commit 81d69a7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changes:

* Added a converter for loading GeoTIFF using xarray/rioxarray (#193).
* Update notebook process forms. See `client.gui` function.
* Add support for Path objects in `utils.guess_type`
* Support multiple mimetypes in converters. API change: mimetype (str) replaced by mimetypes (tuple)
* Removed geojson mimetypes from BINARY_MIMETYPES so it's embedded as a string rather than bytes.

Expand Down
13 changes: 7 additions & 6 deletions birdy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def guess_type(url, supported):
Parameters
----------
url : str
url : str, Path
Path or URL to file.
supported : list, tuple
Supported mimetypes.
Expand All @@ -148,7 +148,7 @@ def guess_type(url, supported):
import mimetypes

try:
mime, enc = mimetypes.guess_type(url, strict=False)
mime, enc = mimetypes.guess_type(str(url), strict=False)
except TypeError:
mime, enc = None, None

Expand All @@ -158,7 +158,7 @@ def guess_type(url, supported):
# netCDF
if (
mime == "application/x-netcdf"
and "dodsC" in url
and "dodsC" in str(url)
and "application/x-ogc-dods" in supported
):
mime = "application/x-ogc-dods"
Expand All @@ -169,14 +169,15 @@ def guess_type(url, supported):
if mime in zips and set(zips).intersection(supported):
mime = set(zips).intersection(supported).pop()

# GeoJSON
if mime == "application/json" and "application/geo+json" in supported:
mime = "application/geo+json"

# FIXME: Verify whether this code is needed. Remove if not.
# # GeoTIFF (workaround since this mimetype isn't correctly understoud)
# if mime == "image/tiff" and (".tif" in url or ".tiff" in "url"):
# mime = "image/tiff; subtype=geotiff"
#
# # GeoJSON (workaround since this mimetype isn't correctly understoud)
# if mime == "application/geo+json":
# mime = "application/vnd.geo+json"

# All the various XML schemes
# TODO
Expand Down
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,22 @@ def test_nc(self): # noqa: D102
["application/x-ogc-dods", "application/x-netcdf"],
)
assert mime == "application/x-netcdf"

def test_path(self): # noqa: D102
from pathlib import Path

mime, enc = utils.guess_type(
Path("shape.json"), ["wrong", "application/geo+json"]
)
assert mime == "application/geo+json"

mime, enc = utils.guess_type(
Path("data.nc"), ["application/x-ogc-dods", "application/x-netcdf"]
)
assert mime == "application/x-netcdf"

mime, enc = utils.guess_type(
Path("file:///dodsC/data.nc"),
["application/x-netcdf", "application/x-ogc-dods"],
)
assert mime == "application/x-ogc-dods"

0 comments on commit 81d69a7

Please sign in to comment.