From ad6e37d93f8791d72d5103a5937a5255aa7550bc Mon Sep 17 00:00:00 2001 From: Alessandro Amici Date: Wed, 16 Dec 2020 22:20:30 +0100 Subject: [PATCH 1/5] Allow pathlib.Path to be used in open_dataset --- xarray/backends/zarr.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 1c0ebb1dbc9..c1b3815dfc2 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -1,3 +1,5 @@ +import os + import numpy as np from .. import coding, conventions @@ -667,6 +669,9 @@ def open_backend_dataset_zarr( consolidate_on_close=False, chunk_store=None, ): + # zarr doesn't support PathLike objects yet. zarr-python#601 + if isinstance(filename_or_obj, os.PathLike): + filename_or_obj = os.fspath(filename_or_obj) store = ZarrStore.open_group( filename_or_obj, From c1c553b93e2fba535cda7c2ce902e3c5a28cd3ee Mon Sep 17 00:00:00 2001 From: Alessandro Amici Date: Wed, 16 Dec 2020 22:48:02 +0100 Subject: [PATCH 2/5] Fix mypy run. It doesn't like isinstance with os.PathLike --- xarray/backends/zarr.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index c1b3815dfc2..de168f4ad77 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -1,4 +1,5 @@ import os +import pathlib import numpy as np @@ -669,8 +670,8 @@ def open_backend_dataset_zarr( consolidate_on_close=False, chunk_store=None, ): - # zarr doesn't support PathLike objects yet. zarr-python#601 - if isinstance(filename_or_obj, os.PathLike): + # zarr doesn't support pathlib.Path objects yet. zarr-python#601 + if isinstance(filename_or_obj, pathlib.Path): filename_or_obj = os.fspath(filename_or_obj) store = ZarrStore.open_group( From b11fd293e168b3085c55c25d5deb2103bbaf84ca Mon Sep 17 00:00:00 2001 From: Alessandro Amici Date: Fri, 18 Dec 2020 13:19:59 +0100 Subject: [PATCH 3/5] Add patlib.Path support to netCDF4 --- xarray/backends/netCDF4_.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xarray/backends/netCDF4_.py b/xarray/backends/netCDF4_.py index a6f80a1125d..92d0a6e004d 100644 --- a/xarray/backends/netCDF4_.py +++ b/xarray/backends/netCDF4_.py @@ -1,5 +1,7 @@ import functools import operator +import os +import pathlib from contextlib import suppress import numpy as np @@ -335,11 +337,12 @@ def open( ): import netCDF4 - if not isinstance(filename, str): + if not isinstance(filename, (str, pathlib.Path)): raise ValueError( "can only read bytes or file-like objects " "with engine='scipy' or 'h5netcdf'" ) + filename = os.fspath(filename) if format is None: format = "NETCDF4" From fe0a15c39a31bbfb1b02d7e5cfb826e781b73bd5 Mon Sep 17 00:00:00 2001 From: Alessandro Amici Date: Fri, 18 Dec 2020 13:32:52 +0100 Subject: [PATCH 4/5] Sympler code --- xarray/backends/netCDF4_.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/backends/netCDF4_.py b/xarray/backends/netCDF4_.py index 92d0a6e004d..2d23d75e11c 100644 --- a/xarray/backends/netCDF4_.py +++ b/xarray/backends/netCDF4_.py @@ -337,12 +337,14 @@ def open( ): import netCDF4 - if not isinstance(filename, (str, pathlib.Path)): + if isinstance(filename, pathlib.Path): + filename = os.fspath(filename) + + if not isinstance(filename, str): raise ValueError( "can only read bytes or file-like objects " "with engine='scipy' or 'h5netcdf'" ) - filename = os.fspath(filename) if format is None: format = "NETCDF4" From b468ca08054ff428941e5c4daeb20c93a8f40f5e Mon Sep 17 00:00:00 2001 From: Alessandro Amici Date: Fri, 18 Dec 2020 13:47:03 +0100 Subject: [PATCH 5/5] Move os.fspath down the call tree --- xarray/backends/zarr.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 12b4480303c..51539a1854b 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -287,6 +287,10 @@ def open_group( ): import zarr + # zarr doesn't support pathlib.Path objects yet. zarr-python#601 + if isinstance(store, pathlib.Path): + store = os.fspath(store) + open_kwargs = dict(mode=mode, synchronizer=synchronizer, path=group) if chunk_store: open_kwargs["chunk_store"] = chunk_store @@ -671,9 +675,6 @@ def open_backend_dataset_zarr( consolidate_on_close=False, chunk_store=None, ): - # zarr doesn't support pathlib.Path objects yet. zarr-python#601 - if isinstance(filename_or_obj, pathlib.Path): - filename_or_obj = os.fspath(filename_or_obj) store = ZarrStore.open_group( filename_or_obj,