From f1c9bd2dcce49f8d654a42395c77b0ff3a19307e Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Sun, 26 Feb 2023 09:33:05 -0700 Subject: [PATCH] MNT: Update pathlib replacements Address code review comments and simplify some path handling. Also change back to returning string from zip_file_contents. --- docs/source/conf.py | 3 +-- lib/cartopy/__init__.py | 2 +- lib/cartopy/io/__init__.py | 3 +-- lib/cartopy/io/img_nest.py | 8 +------ lib/cartopy/io/shapereader.py | 28 ++++++++++------------ lib/cartopy/io/srtm.py | 3 +-- lib/cartopy/mpl/geoaxes.py | 13 +++++----- lib/cartopy/tests/io/test_downloaders.py | 2 +- lib/cartopy/tests/test_coding_standards.py | 2 +- lib/cartopy/tests/test_img_nest.py | 6 ++--- 10 files changed, 28 insertions(+), 42 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 386ca23c26..0df0a8d527 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -25,8 +25,7 @@ from sphinx_gallery.sorting import ExampleTitleSortKey, ExplicitOrder # If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root. +# add these directories to sys.path here. sys.path.insert(0, str(Path(__file__).parent.resolve())) # -- General configuration ----------------------------------------------------- diff --git a/lib/cartopy/__init__.py b/lib/cartopy/__init__.py index f03a2e1db7..96e0b79ae7 100644 --- a/lib/cartopy/__init__.py +++ b/lib/cartopy/__init__.py @@ -20,7 +20,7 @@ _cache_dir = Path(tempfile.gettempdir()) / 'cartopy_cache_dir' config = {'pre_existing_data_dir': Path(os.environ.get('CARTOPY_DATA_DIR', - _data_dir)), + '')), 'data_dir': _data_dir, 'cache_dir': _cache_dir, 'repo_data_dir': Path(__file__).parent / 'data', diff --git a/lib/cartopy/io/__init__.py b/lib/cartopy/io/__init__.py index 92a5db7137..f5cbd88187 100644 --- a/lib/cartopy/io/__init__.py +++ b/lib/cartopy/io/__init__.py @@ -219,8 +219,7 @@ def acquire_resource(self, target_path, format_dict): """ target_path = Path(target_path) target_dir = target_path.parent - if not target_dir.is_dir(): - target_dir.mkdir(parents=True) + target_dir.mkdir(parents=True, exist_ok=True) url = self.url(format_dict) diff --git a/lib/cartopy/io/img_nest.py b/lib/cartopy/io/img_nest.py index addc35ed26..441a6d16ab 100644 --- a/lib/cartopy/io/img_nest.py +++ b/lib/cartopy/io/img_nest.py @@ -122,13 +122,7 @@ def world_files(fname): fext_types.extend([ext.upper() for ext in fext_types]) result = [path.with_suffix(ext) for ext in fext_types] - def _convert_basename(path): - dirname, basename = path.parent, Path(path.name) - base, ext = basename.stem, basename.suffix - result = dirname / (base.swapcase() + ext) - return result - - result += [_convert_basename(r) for r in result] + result += [path.with_stem(path.stem.swapcase()) for path in result] # return string paths rather than Path objects return [str(r) for r in result] diff --git a/lib/cartopy/io/shapereader.py b/lib/cartopy/io/shapereader.py index 3c0ae5bd98..dc0d8950d9 100644 --- a/lib/cartopy/io/shapereader.py +++ b/lib/cartopy/io/shapereader.py @@ -316,8 +316,8 @@ def zip_file_contents(self, format_dict): """ for ext in ['.shp', '.dbf', '.shx', '.prj', '.cpg']: - yield Path('ne_{resolution}_{name}' - '{extension}'.format(extension=ext, **format_dict)) + yield ('ne_{resolution}_{name}' + '{extension}'.format(extension=ext, **format_dict)) def acquire_resource(self, target_path, format_dict): """ @@ -328,8 +328,7 @@ def acquire_resource(self, target_path, format_dict): from zipfile import ZipFile target_dir = Path(target_path).parent - if not target_dir.is_dir(): - target_dir.mkdir(parents=True) + target_dir.mkdir(parents=True, exist_ok=True) url = self.url(format_dict) @@ -338,8 +337,9 @@ def acquire_resource(self, target_path, format_dict): zfh = ZipFile(io.BytesIO(shapefile_online.read()), 'r') for member_path in self.zip_file_contents(format_dict): - member = zfh.getinfo(str(member_path).replace('\\', '/')) - with open(target_path.with_suffix(member_path.suffix), 'wb') as fh: + member = zfh.getinfo(member_path.replace('\\', '/')) + with open(target_path.with_suffix( + Path(member_path).suffix), 'wb') as fh: fh.write(zfh.open(member).read()) shapefile_online.close() @@ -427,10 +427,9 @@ def zip_file_contents(self, format_dict): """ for ext in ['.shp', '.dbf', '.shx']: - yield (Path('GSHHS_shp' - / '{scale}'.format(**format_dict) - / 'GSHHS_{scale}_L{level}{extension}'.format( - extension=ext, **format_dict))) + p = Path('GSHHS_shp') / '{scale}' + p /= 'GSHHS_{scale}_L{level}{extension}' + yield str(p).format(extension=ext, **format_dict) def acquire_all_resources(self, format_dict): from zipfile import ZipFile @@ -477,13 +476,12 @@ def acquire_all_resources(self, format_dict): modified_format_dict.update({'scale': scale, 'level': level}) target_path = self.target_path(modified_format_dict) target_dir = target_path.parent - if not target_dir.is_dir(): - target_dir.mkdir(parents=True) + target_dir.mkdir(parents=True, exist_ok=True) for member_path in self.zip_file_contents(modified_format_dict): - member = zfh.getinfo(str(member_path).replace('\\', '/')) - with open(target_path.with_suffix(member_path.suffix), - 'wb') as fh: + member = zfh.getinfo(member_path.replace('\\', '/')) + with open(target_path.with_suffix( + Path(member_path).suffix), 'wb') as fh: fh.write(zfh.open(member).read()) zfh.close() diff --git a/lib/cartopy/io/srtm.py b/lib/cartopy/io/srtm.py index 928e13761d..8f136c40b2 100644 --- a/lib/cartopy/io/srtm.py +++ b/lib/cartopy/io/srtm.py @@ -362,8 +362,7 @@ def acquire_resource(self, target_path, format_dict): from zipfile import ZipFile target_dir = Path(target_path).parent - if not target_dir.is_dir(): - target_dir.make_dir() + target_dir.mkdir(parents=True, exist_ok=True) url = self.url(format_dict) diff --git a/lib/cartopy/mpl/geoaxes.py b/lib/cartopy/mpl/geoaxes.py index 78006b646f..ed9d82995c 100644 --- a/lib/cartopy/mpl/geoaxes.py +++ b/lib/cartopy/mpl/geoaxes.py @@ -1065,10 +1065,9 @@ def background_img(self, name='ne_shaded', resolution='low', extent=None, # read in the user's background image directory: if len(_USER_BG_IMGS) == 0: self.read_user_background_images() - import os - bgdir = Path(os.getenv('CARTOPY_USER_BACKGROUNDS', - (config["repo_data_dir"] / 'raster' - / 'natural_earth'))) + bgdir = Path(os.getenv( + 'CARTOPY_USER_BACKGROUNDS', + config["repo_data_dir"] / 'raster' / 'natural_earth')) # now get the filename we want to use: try: fname = _USER_BG_IMGS[name][resolution] @@ -1164,9 +1163,9 @@ def read_user_background_images(self, verify=True): lib/cartopy/data/raster/natural_earth/images.json """ - bgdir = Path(os.getenv('CARTOPY_USER_BACKGROUNDS', - (config["repo_data_dir"] / 'raster' - / 'natural_earth'))) + bgdir = Path(os.getenv( + 'CARTOPY_USER_BACKGROUNDS', + config["repo_data_dir"] / 'raster' / 'natural_earth')) json_file = bgdir / 'images.json' with open(json_file) as js_obj: diff --git a/lib/cartopy/tests/io/test_downloaders.py b/lib/cartopy/tests/io/test_downloaders.py index 29188b33f9..c9f9793940 100644 --- a/lib/cartopy/tests/io/test_downloaders.py +++ b/lib/cartopy/tests/io/test_downloaders.py @@ -31,7 +31,7 @@ def test_Downloader_data(): Path('/wibble') / 'foo' / 'bar' / 'example' / 'shape.shp') assert (di.pre_downloaded_path(replacement_dict) == - Path('/project') / 'foobar' / 'example' / 'sample.shp') + Path('/project/foobar/example/sample.shp')) @contextlib.contextmanager diff --git a/lib/cartopy/tests/test_coding_standards.py b/lib/cartopy/tests/test_coding_standards.py index 7e792b83d7..3eb22da3e6 100644 --- a/lib/cartopy/tests/test_coding_standards.py +++ b/lib/cartopy/tests/test_coding_standards.py @@ -32,7 +32,7 @@ # Guess cartopy repo directory of cartopy - realpath is used to mitigate # against Python finding the cartopy package via a symlink. -CARTOPY_DIR = Path(cartopy.__file__).resolve() +CARTOPY_DIR = Path(cartopy.__file__).parent.resolve() REPO_DIR = Path(os.getenv('CARTOPY_GIT_DIR', CARTOPY_DIR.parent.parent)) diff --git a/lib/cartopy/tests/test_img_nest.py b/lib/cartopy/tests/test_img_nest.py index 389f7cc7f6..685c2cb5ce 100644 --- a/lib/cartopy/tests/test_img_nest.py +++ b/lib/cartopy/tests/test_img_nest.py @@ -283,8 +283,7 @@ def wmts_data(): fname_template = str(_TEST_DATA_DIR / 'z_{}' / 'x_{}_y_{}.png') - if not _TEST_DATA_DIR.is_dir(): - _TEST_DATA_DIR.mkdir(parents=True) + _TEST_DATA_DIR.mkdir(parents=True, exist_ok=True) data_version_fname = _TEST_DATA_DIR / 'version.txt' @@ -308,8 +307,7 @@ def wmts_data(): x, y, z = tile fname = Path(fname_template.format(z, x, y)) if not fname.exists(): - if not fname.parent.is_dir(): - fname.parent.mkdir(parents=True) + fname.parent.mkdir(parents=True, exist_ok=True) img, extent, _ = aerial.get_image(tile) nx, ny = 256, 256