Skip to content
Open
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
2 changes: 0 additions & 2 deletions benchmarks/benchmarks/sperf/combine_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# See LICENSE in the root of the repository for full licensing details.
"""Region combine benchmarks for the SPerf scheme of the UK Met Office's NG-VAT project."""

import os.path

from dask import array as da
import numpy as np

Expand Down
4 changes: 2 additions & 2 deletions docs/gallery_code/meteorology/plot_COP_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

""" # noqa: D205, D212, D400

import os.path
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -40,7 +40,7 @@ def cop_metadata_callback(cube, field, filename):
"""Add an "Experiment" coordinate which comes from the filename."""
# Extract the experiment name (such as A1B or E1) from the filename (in
# this case it is just the start of the file name, before the first ".").
fname = os.path.basename(filename) # filename without path.
fname = Path(filename).name # filename without path.
experiment_label = fname.split(".")[0]

# Create a coordinate with the experiment label in it...
Expand Down
6 changes: 3 additions & 3 deletions docs/src/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ def autolog(message):

# 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.
# documentation root, use pathlib.Path().absolute() to make it absolute, like shown here.

# custom sphinx extensions
sys.path.append(os.path.abspath("sphinxext"))
sys.path.append(str((Path("sphinxext").absolute())))

# add some sample files from the developers guide..
sys.path.append(os.path.abspath(os.path.join("developers_guide")))
sys.path.append(str(Path("developers_guide").absolute()))

# why isn't the iris path added to it is discoverable too? We dont need to,
# the sphinext to generate the api rst knows where the source is. If it
Expand Down
2 changes: 2 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ This document explains the changes made to Iris for this release
except for mo_pack.
(:issue:`6832`, :pull:`6976`)

#. `@SgtVarmint` _ migrated codebase from os.path to pathlib.Path where possible
(:issue:`4523`, :pull:`7087`)
.. comment
Whatsnew author names (@github name) in alphabetical order. Note that,
core dev names are automatically included by the common_links.inc:
Expand Down
10 changes: 5 additions & 5 deletions lib/iris/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

Filenames can contain `~` or `~user` abbreviations, and/or
Unix shell-style wildcards (e.g. `*` and `?`). See the
standard library function :func:`os.path.expanduser` and
standard library function :func:`pathlib.Path(path).expanduser()` and
module :mod:`fnmatch` for more details.

.. warning::
Expand Down Expand Up @@ -101,7 +101,7 @@ def callback(cube, field, filename):
import contextlib
import glob
import importlib
import os.path
from pathlib import Path
import threading
from typing import Callable, Literal

Expand Down Expand Up @@ -340,16 +340,16 @@ def sample_data_path(*path_to_join):
appropriate for general file access.

"""
target = os.path.join(*path_to_join)
if os.path.isabs(target):
target = Path(*path_to_join)
if target.is_absolute():
raise ValueError(
"Absolute paths, such as {!r}, are not supported.\n"
"NB. This function is only for locating files in the "
"iris sample data collection. It is not needed or "
"appropriate for general file access.".format(target)
)
if iris_sample_data is not None:
target = os.path.join(iris_sample_data.path, target)
target = str(Path(iris_sample_data.path) / target)
else:
raise ImportError(
"Please install the 'iris-sample-data' package to access sample data."
Expand Down
19 changes: 10 additions & 9 deletions lib/iris/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import configparser
import contextlib
import logging
import os.path
import os
from pathlib import Path
import warnings

import iris.warnings
Expand Down Expand Up @@ -135,7 +136,7 @@ def get_dir_option(section, option, default=None):
path = default
if config.has_option(section, option):
c_path = config.get(section, option)
if os.path.isdir(c_path):
if Path(c_path).is_dir():
path = c_path
else:
msg = (
Expand All @@ -150,14 +151,14 @@ def get_dir_option(section, option, default=None):


# Figure out the full path to the "iris" package.
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
ROOT_PATH = Path(__file__).parent.absolute()

# The full path to the configuration directory of the active Iris instance.
CONFIG_PATH = os.path.join(ROOT_PATH, "etc")
CONFIG_PATH = ROOT_PATH / "etc"

# Load the optional "site.cfg" file if it exists.
config = configparser.ConfigParser()
config.read([os.path.join(CONFIG_PATH, "site.cfg")])
config.read([CONFIG_PATH / "site.cfg"])

##################
# Resource options
Expand All @@ -167,19 +168,19 @@ def get_dir_option(section, option, default=None):
TEST_DATA_DIR = get_dir_option(
_RESOURCE_SECTION,
"test_data_dir",
default=os.path.join(os.path.dirname(__file__), "test_data"),
default=str(Path(__file__) / "test_data"),
)

# Override the data repository if the appropriate environment variable
# has been set.
override = os.environ.get("OVERRIDE_TEST_DATA_REPOSITORY")
if override:
TEST_DATA_DIR = None
if os.path.isdir(os.path.expanduser(override)):
TEST_DATA_DIR = os.path.abspath(override)
if Path(override).expanduser().is_dir():
TEST_DATA_DIR = str(Path(override).absolute())

PALETTE_PATH = get_dir_option(
_RESOURCE_SECTION, "palette_path", os.path.join(CONFIG_PATH, "palette")
_RESOURCE_SECTION, "palette_path", CONFIG_PATH / "palette"
)

# Runtime options
Expand Down
6 changes: 3 additions & 3 deletions lib/iris/fileformats/abf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import calendar
import datetime
import glob
import os.path
from pathlib import Path

import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self, filename):
field = ABFField("AVHRRBUVI01.1985feba.abl")

"""
basename = os.path.basename(filename)
basename = Path(filename).name
if len(basename) != 24:
raise ValueError(
"ABFField expects a filename of 24 characters: {}".format(basename)
Expand All @@ -100,7 +100,7 @@ def __getattr__(self, key):

def _read(self):
"""Read the field from the given filename."""
basename = os.path.basename(self._filename)
basename = Path(self._filename).name
self.version = int(basename[9:11])
self.year = int(basename[12:16])
self.month = basename[16:19]
Expand Down
7 changes: 5 additions & 2 deletions lib/iris/fileformats/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from abc import ABCMeta, abstractmethod
from collections.abc import Iterable, MutableMapping
import os
from pathlib import Path
import re
from typing import ClassVar, Optional
import warnings
Expand Down Expand Up @@ -1365,7 +1365,10 @@ def __init__(self, file_source, warn=False, monotonic=False):
self._own_file = False
if isinstance(file_source, str):
# Create from filepath : open it + own it (=close when we die).
self._filename = os.path.expanduser(file_source)
self._filename = Path(file_source).expanduser()
if file_source.startswith("https:"):
self._filename = file_source

self._dataset = _thread_safe_nc.DatasetWrapper(self._filename, mode="r")
self._own_file = True
else:
Expand Down
7 changes: 4 additions & 3 deletions lib/iris/fileformats/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
from pathlib import Path
import subprocess

import iris
Expand Down Expand Up @@ -40,16 +41,16 @@ def _dot_path():
path = _DOT_EXECUTABLE_PATH
else:
path = iris.config.get_option("System", "dot_path", default="dot")
if not os.path.exists(path):
if not os.path.isabs(path):
if not Path(path).exists():
if not Path(path).is_absolute():
try:
# Check PATH
subprocess.check_output([path, "-V"], stderr=subprocess.STDOUT)
except (OSError, subprocess.CalledProcessError):
path = None
else:
path = None
_DOT_EXECUTABLE_PATH = path
_DOT_EXECUTABLE_PATH = str(path)
_DOT_CHECKED = True
return path

Expand Down
8 changes: 4 additions & 4 deletions lib/iris/fileformats/netcdf/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import collections
from itertools import repeat, zip_longest
import os
import os.path
from pathlib import Path
import re
import string
import typing
Expand Down Expand Up @@ -418,13 +418,13 @@ def __init__(self, filename, netcdf_format, compute=True):
else:
# Given a filepath string/path : create a dataset from that
try:
self.filepath = os.path.abspath(filename)
self.filepath = Path(filename)
self._dataset = _thread_safe_nc.DatasetWrapper(
self.filepath, mode="w", format=netcdf_format
)
except RuntimeError:
dir_name = os.path.dirname(self.filepath)
if not os.path.isdir(dir_name):
dir_name = self.filepath.parent
if not dir_name.is_dir():
msg = "No such file or directory: {}".format(dir_name)
raise IOError(msg)
if not os.access(dir_name, os.R_OK | os.W_OK):
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/fileformats/um/_fast_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""

from contextlib import contextmanager
import os.path
from pathlib import Path
import threading

import numpy as np
Expand Down Expand Up @@ -120,7 +120,7 @@ def _select_raw_fields_loader(fname):
from iris.fileformats.um import um_to_pp

with open(fname, "rb") as fh:
spec = FORMAT_AGENT.get_spec(os.path.basename(fname), fh)
spec = FORMAT_AGENT.get_spec(Path(fname).name, fh)
if spec.name.startswith(_FF_SPEC_NAME):
loader = um_to_pp
elif spec.name.startswith(_PP_SPEC_NAME):
Expand Down
6 changes: 3 additions & 3 deletions lib/iris/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import collections
from collections import OrderedDict
import glob
import os.path
import pathlib
import re

Expand Down Expand Up @@ -168,7 +167,8 @@ def expand_filespecs(file_specs, files_expected=True):
"""
# Remove any hostname component - currently unused
filenames = [
os.path.abspath(os.path.expanduser(fn.removeprefix("//"))) for fn in file_specs
str(pathlib.Path(fn.removeprefix("//")).expanduser().absolute())
for fn in file_specs
]

if files_expected:
Expand Down Expand Up @@ -214,7 +214,7 @@ def load_files(filenames, callback, constraints=None):
handler_map = collections.defaultdict(list)
for fn in all_file_paths:
with open(fn, "rb") as fh:
handling_format_spec = FORMAT_AGENT.get_spec(os.path.basename(fn), fh)
handling_format_spec = FORMAT_AGENT.get_spec(pathlib.Path(fn).name, fh)
handler_map[handling_format_spec].append(fn)

# Call each iris format handler with the appropriate filenames
Expand Down
4 changes: 2 additions & 2 deletions lib/iris/io/format_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

from collections.abc import Callable
import functools
import os
from pathlib import Path
import struct


Expand Down Expand Up @@ -348,7 +348,7 @@ class FileExtension(FileElement):

def get_element(self, basename, file_handle):
# noqa D102
return os.path.splitext(basename)[1]
return Path(basename).suffix


class LeadingLine(FileElement):
Expand Down
8 changes: 4 additions & 4 deletions lib/iris/palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from functools import wraps
import os
import os.path
from pathlib import Path
import re

import cf_units
Expand Down Expand Up @@ -248,15 +248,15 @@ def _load_palette():
# Identify any target .txt color map palette files.
filenames.extend(
[
os.path.join(root, filename)
str(Path(root) / filename)
for filename in files
if os.path.splitext(filename)[1] == ".txt"
if Path(filename).suffix == ".txt"
]
)

for filename in filenames:
# Default color map name based on the file base-name (case-SENSITIVE).
cmap_name = os.path.splitext(os.path.basename(filename))[0]
cmap_name = Path(filename).stem
cmap_scheme = None
cmap_keywords = []
cmap_std_names = []
Expand Down
Loading
Loading