Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT: Use pathlib throughout the codebase #2116

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/make_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.

import os
import inspect
from pathlib import Path
import textwrap
import numpy as np
import cartopy.crs as ccrs
Expand Down Expand Up @@ -130,8 +130,7 @@ def create_instance(prj_cls, instance_args):


if __name__ == '__main__':
fname = os.path.join(os.path.dirname(__file__), 'source',
'reference', 'projections.rst')
fname = Path(__file__).parent / 'source' / 'reference' / 'projections.rst'
table = open(fname, 'w')

header = """
Expand Down
7 changes: 3 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
from datetime import datetime
import os
from pathlib import Path
import sys

import cartopy
import matplotlib
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, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# add these directories to sys.path here.
sys.path.insert(0, str(Path(__file__).parent.resolve()))

# -- General configuration -----------------------------------------------------

Expand Down
9 changes: 2 additions & 7 deletions docs/source/matplotlib/advanced_plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Contour plots
.. plot::
:include-source:

import os
import matplotlib.pyplot as plt
from scipy.io import netcdf

Expand All @@ -21,9 +20,7 @@ Contour plots


# get the path of the file. It can be found in the repo data directory.
fname = os.path.join(config["repo_data_dir"],
'netcdf', 'HadISST1_SST_update.nc'
)
fname = config["repo_data_dir"] / 'netcdf' / 'HadISST1_SST_update.nc'

dataset = netcdf.netcdf_file(fname, maskandscale=True, mmap=False)
sst = dataset.variables['sst'][0, :, :]
Expand Down Expand Up @@ -56,9 +53,7 @@ Images
fig = plt.figure(figsize=(8, 12))

# get the path of the file. It can be found in the repo data directory.
fname = os.path.join(config["repo_data_dir"],
'raster', 'sample', 'Miriam.A2012270.2050.2km.jpg'
)
fname = config["repo_data_dir"] / 'raster' / 'sample' / 'Miriam.A2012270.2050.2km.jpg'
img_extent = (-120.67660000000001, -106.32104523100001, 13.2301484511245, 30.766899999999502)
img = plt.imread(fname)

Expand Down
17 changes: 8 additions & 9 deletions lib/cartopy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
# This file is part of Cartopy and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
import os
from pathlib import Path

from ._version import version as __version__ # noqa: F401
import tempfile

__document_these__ = ['config']

# Configuration
import os.path


# for the writable data directory (i.e. the one where new data goes), follow
# the XDG guidelines found at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
_writable_dir = os.path.join(os.path.expanduser('~'), '.local', 'share')
_data_dir = os.path.join(os.environ.get("XDG_DATA_HOME", _writable_dir),
'cartopy')
_cache_dir = os.path.join(tempfile.gettempdir(), 'cartopy_cache_dir')
_writable_dir = Path.home() / '.local' / 'share'
_data_dir = Path(os.environ.get("XDG_DATA_HOME", _writable_dir)) / 'cartopy'
_cache_dir = Path(tempfile.gettempdir()) / 'cartopy_cache_dir'

config = {'pre_existing_data_dir': os.environ.get('CARTOPY_DATA_DIR', ''),
config = {'pre_existing_data_dir': Path(os.environ.get('CARTOPY_DATA_DIR',
'')),
'data_dir': _data_dir,
'cache_dir': _cache_dir,
'repo_data_dir': os.path.join(os.path.dirname(__file__), 'data'),
'repo_data_dir': Path(__file__).parent / 'data',
'downloaders': {},
}
"""
Expand Down
8 changes: 4 additions & 4 deletions lib/cartopy/img_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ def regrid(array, source_x_coords, source_y_coords, source_proj, target_proj,

# Stack our original xyz array, this will also wrap coords when necessary
xyz = source_proj.transform_points(source_proj,
source_x_coords.flatten(),
source_y_coords.flatten())
source_x_coords.flatten(),
source_y_coords.flatten())
# Transform the target points into the source projection
target_xyz = source_proj.transform_points(target_proj,
target_x_points.flatten(),
target_y_points.flatten())
target_x_points.flatten(),
target_y_points.flatten())

if _is_pykdtree:
kdtree = pykdtree.kdtree.KDTree(xyz)
Expand Down
22 changes: 11 additions & 11 deletions lib/cartopy/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

import collections
import os
from pathlib import Path
import string
from urllib.request import urlopen
import warnings
Expand Down Expand Up @@ -150,8 +150,8 @@ def target_path(self, format_dict):
expected as a minimum in their ``FORMAT_KEYS`` class attribute.
"""
return self._formatter.format(self.target_path_template,
**format_dict)
return Path(self._formatter.format(self.target_path_template,
**format_dict))

def pre_downloaded_path(self, format_dict):
"""
Expand All @@ -167,8 +167,9 @@ def pre_downloaded_path(self, format_dict):
expected as a minimum in their ``FORMAT_KEYS`` class attribute.
"""
return self._formatter.format(self.pre_downloaded_path_template,
**format_dict)
p = self._formatter.format(self.pre_downloaded_path_template,
**format_dict)
return None if p == '' else Path(p)

def path(self, format_dict):
"""
Expand All @@ -193,10 +194,9 @@ def path(self, format_dict):
"""
pre_downloaded_path = self.pre_downloaded_path(format_dict)
target_path = self.target_path(format_dict)
if (pre_downloaded_path is not None and
os.path.exists(pre_downloaded_path)):
if pre_downloaded_path is not None and pre_downloaded_path.exists():
result_path = pre_downloaded_path
elif os.path.exists(target_path):
elif target_path.exists():
result_path = target_path
else:
# we need to download the file
Expand All @@ -217,9 +217,9 @@ def acquire_resource(self, target_path, format_dict):
expected as a minimum in their ``FORMAT_KEYS`` class attribute.
"""
target_dir = os.path.dirname(target_path)
if not os.path.isdir(target_dir):
os.makedirs(target_dir)
target_path = Path(target_path)
target_dir = target_path.parent
target_dir.mkdir(parents=True, exist_ok=True)

url = self.url(format_dict)

Expand Down
44 changes: 15 additions & 29 deletions lib/cartopy/io/img_nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@


import collections
import glob
import os.path
from pathlib import Path

import numpy as np
from PIL import Image
Expand Down Expand Up @@ -113,32 +112,19 @@ def world_files(fname):
'/path/to/img/with_no_extension.w'
"""
froot, fext = os.path.splitext(fname)
path = Path(fname)
fext = path.suffix[1::].lower()
# If there was no extension to the filename.
if froot == fname:
result = [f'{fname}.w', f'{fname}.W']
if len(fext) < 3:
result = [path.with_suffix('.w'), path.with_suffix('.W')]
else:
fext = fext[1::].lower()
if len(fext) < 3:
result = [f'{fname}.w', f'{fname}.W']
else:
fext_types = [f'{fext}w', f'{fext[0]}{fext[-1]}w']
fext_types.extend([ext.upper() for ext in fext_types])
result = [f'{froot}.{ext}' for ext in fext_types]

def _convert_basename(name):
dirname, basename = os.path.split(name)
base, ext = os.path.splitext(basename)
if base == base.upper():
result = base.lower() + ext
else:
result = base.upper() + ext
if dirname:
result = os.path.join(dirname, result)
return result
fext_types = [f'.{fext}w', f'.{fext[0]}{fext[-1]}w']
fext_types.extend([ext.upper() for ext in fext_types])
result = [path.with_suffix(ext) for ext in fext_types]

result += [_convert_basename(r) for r in result]
return result
result += [p.with_name(p.stem.swapcase() + p.suffix) for p in result]
# return string paths rather than Path objects
return [str(r) for r in result]

def __array__(self):
return np.array(Image.open(self.filename))
Expand Down Expand Up @@ -228,14 +214,14 @@ def scan_dir_for_imgs(self, directory, glob_pattern='*.tif',
Does not recursively search sub-directories.
"""
imgs = glob.glob(os.path.join(directory, glob_pattern))
imgs = Path(directory).glob(glob_pattern)

for img in imgs:
dirname, fname = os.path.split(img)
dirname, fname = img.parent, img.name
worlds = img_class.world_files(fname)
for fworld in worlds:
fworld = os.path.join(dirname, fworld)
if os.path.exists(fworld):
fworld = dirname / fworld
if fworld.exists():
break
else:
raise ValueError(
Expand Down
27 changes: 10 additions & 17 deletions lib/cartopy/io/img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from abc import ABCMeta, abstractmethod
import concurrent.futures
import io
import os
from pathlib import Path
import warnings

from PIL import Image
Expand Down Expand Up @@ -60,11 +60,11 @@ def __init__(self, desired_tile_form='RGB',
self._default_cache = False
if cache is True:
self._default_cache = True
self.cache_path = cartopy.config["cache_dir"]
self.cache_path = Path(cartopy.config["cache_dir"])
elif cache is False:
self.cache_path = None
else:
self.cache_path = cache
self.cache_path = Path(cache)
self.cache = set({})
self._load_cache()

Expand Down Expand Up @@ -101,22 +101,19 @@ def fetch_tile(tile):
@property
def _cache_dir(self):
"""Return the name of the cache directory"""
return os.path.join(
self.cache_path,
self.__class__.__name__
)
return self.cache_path / self.__class__.__name__

def _load_cache(self):
"""Load the cache"""
if self.cache_path is not None:
cache_dir = self._cache_dir
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
if not cache_dir.exists():
cache_dir.mkdir(parents=True)
if self._default_cache:
warnings.warn(
'Cartopy created the following directory to cache '
f'GoogleWTS tiles: {cache_dir}')
self.cache = self.cache.union(set(os.listdir(cache_dir)))
self.cache = self.cache.union(set(cache_dir.iterdir()))

def _find_images(self, target_domain, target_z, start_tile=(0, 0, 0)):
"""Target domain is a shapely polygon in native coordinates."""
Expand Down Expand Up @@ -209,15 +206,11 @@ def get_image(self, tile):

if self.cache_path is not None:
filename = "_".join([str(i) for i in tile]) + ".npy"
cached_file = os.path.join(
self._cache_dir,
filename
)
cached_file = self._cache_dir / filename
else:
filename = None
cached_file = None

if filename in self.cache:
if cached_file in self.cache:
img = np.load(cached_file, allow_pickle=False)
else:
url = self._image_url(tile)
Expand All @@ -236,7 +229,7 @@ def get_image(self, tile):
img = img.convert(self.desired_tile_form)
if self.cache_path is not None:
np.save(cached_file, img, allow_pickle=False)
self.cache.add(filename)
self.cache.add(cached_file)

return img, self.tileextent(tile), 'lower'

Expand Down
Loading