Skip to content

Commit

Permalink
MNT: Update pathlib replacements
Browse files Browse the repository at this point in the history
Address code review comments and simplify some path handling.
Also change back to returning string from zip_file_contents.
  • Loading branch information
greglucas committed Feb 26, 2023
1 parent aaf6b58 commit f1c9bd2
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 42 deletions.
3 changes: 1 addition & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/cartopy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions lib/cartopy/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 1 addition & 7 deletions lib/cartopy/io/img_nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
28 changes: 13 additions & 15 deletions lib/cartopy/io/shapereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions lib/cartopy/io/srtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 6 additions & 7 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/cartopy/tests/io/test_downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/cartopy/tests/test_coding_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 2 additions & 4 deletions lib/cartopy/tests/test_img_nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down

0 comments on commit f1c9bd2

Please sign in to comment.