Skip to content

Commit

Permalink
MNT: Convert remaining uses of os.path to pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
dopplershift committed Apr 30, 2021
1 parent 7870bf2 commit dc0af88
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
18 changes: 10 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from datetime import datetime
import os
from pathlib import Path
import re
import sys

Expand All @@ -19,9 +20,10 @@

# 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.abspath('.'))
sys.path.insert(0, os.path.abspath(os.path.join('..', '..')))
# documentation root, use resolve() to make it absolute, like shown here.
cwd = Path.cwd().resolve()
sys.path.insert(0, str(cwd))
sys.path.insert(0, str(cwd.parent.parent))

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

Expand Down Expand Up @@ -49,11 +51,11 @@
'reference_url': {
'metpy': None,
},
'examples_dirs': [os.path.join('..', 'examples'), os.path.join('..', 'tutorials')],
'examples_dirs': [str(cwd.parent / 'examples'), str(cwd.parent / 'tutorials')],
'gallery_dirs': ['examples', 'tutorials'],
'filename_pattern': r'\.py',
'backreferences_dir': os.path.join('api', 'generated'),
'default_thumb_file': os.path.join('_static', 'metpy_150x150_white_bg.png'),
'backreferences_dir': str(Path('api') / 'generated'),
'default_thumb_file': str(Path('_static') / 'metpy_150x150_white_bg.png'),
'abort_on_example_error': True
}

Expand Down Expand Up @@ -208,12 +210,12 @@

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
html_logo = os.path.join('_static', 'metpy_horizontal.png')
html_logo = str(Path('_static') / 'metpy_horizontal.png')

# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
html_favicon = os.path.join('_static', 'metpy_32x32.ico')
html_favicon = str(Path('_static') / 'metpy_32x32.ico')

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
11 changes: 4 additions & 7 deletions docs/override_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
#
# Verifies that any modules present in the _templates/overrides directory have all of their
# their exported functions included in the doc file.
import glob
import importlib
import os
from pathlib import Path
import sys


modules_to_skip = ['metpy.xarray']


failed = False
for full_path in glob.glob('_templates/overrides/metpy.*.rst'):

filename = os.path.basename(full_path)
module = filename.split('.rst')[0]
for full_path in (Path('_templates') / 'overrides').glob('metpy.*.rst'):

module = full_path.with_suffx('').name
if module in modules_to_skip:
continue

Expand All @@ -37,7 +34,7 @@
if missing_functions:
failed = True
print('ERROR - The following functions are missing from the override file ' +
filename + ': ' + ', '.join(missing_functions), file=sys.stderr)
str(full_path.name) + ': ' + ', '.join(missing_functions), file=sys.stderr)

# Report status
if failed:
Expand Down
17 changes: 8 additions & 9 deletions src/metpy/plots/ctables.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ def plot_color_gradients(cmap_category, cmap_list, nrows):
"""

import ast
import glob
import logging
import os.path
from pathlib import Path

import matplotlib.colors as mcolors

Expand Down Expand Up @@ -152,15 +151,15 @@ def scan_dir(self, path):
The path to the directory with the color tables
"""
for fname in glob.glob(os.path.join(path, '*' + TABLE_EXT)):
if os.path.isfile(fname):
with open(fname) as fobj:
for entry in Path(path).glob('*' + TABLE_EXT):
if entry.is_file():
with entry.open() as fobj:
try:
self.add_colortable(fobj, os.path.splitext(os.path.basename(fname))[0])
log.debug('Added colortable from file: %s', fname)
self.add_colortable(fobj, entry.with_suffix('').name)
log.debug('Added colortable from file: %s', entry)
except RuntimeError:
# If we get a file we can't handle, assume we weren't meant to.
log.info('Skipping unparsable file: %s', fname)
log.info('Skipping unparsable file: %s', entry)

def add_colortable(self, fobj, name):
r"""Add a color table from a file to the registry.
Expand Down Expand Up @@ -274,7 +273,7 @@ def get_colortable(self, name):

registry = ColortableRegistry()
registry.scan_resource('metpy.plots', 'colortable_files')
registry.scan_dir(os.path.curdir)
registry.scan_dir(Path.cwd())

with exporter:
colortables = registry
14 changes: 7 additions & 7 deletions tests/plots/test_ctables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""Tests for the `ctables` module."""

from io import StringIO
import os.path
from pathlib import Path
import tempfile

import numpy as np
Expand All @@ -31,22 +31,22 @@ def test_scan_dir(registry):
kwargs = {'mode': 'w', 'dir': '.', 'suffix': '.tbl', 'delete': False, 'buffering': 1}
with tempfile.NamedTemporaryFile(**kwargs) as fobj:
fobj.write('"red"\n"lime"\n"blue"\n')
fname = fobj.name
good_file = Path(fobj.name)

# Unrelated table file that *should not* impact the scan
with tempfile.NamedTemporaryFile(**kwargs) as fobj:
fobj.write('PADK 704540 ADAK NAS\n')
bad_file = fobj.name
bad_file = Path(fobj.name)

# Needs to be outside with so it's closed on windows
registry.scan_dir(os.path.dirname(fname))
name = os.path.splitext(os.path.basename(fname))[0]
registry.scan_dir(good_file.parent)
name = good_file.with_suffix('').name

assert name in registry
assert registry[name] == [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)]
finally:
os.remove(fname)
os.remove(bad_file)
good_file.unlink()
bad_file.unlink()


def test_read_file(registry):
Expand Down
6 changes: 3 additions & 3 deletions tools/nexrad_msgs/parse_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ def write_file(fname, info):


if __name__ == '__main__':
import os.path
from pathlib import Path

for num in [18, 3]:
fname = 'msg{:d}.spec'.format(num)
print('Processing {}...'.format(fname)) # noqa: T001
print(f'Processing {fname}...') # noqa: T001
info = processors[num](fname)
fname = os.path.splitext(fname)[0] + '.py'
fname = Path(fname).with_suffix('.py')
write_file(fname, info)

0 comments on commit dc0af88

Please sign in to comment.