Skip to content

Commit

Permalink
removed last of pkg_resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBarker-NOAA committed Apr 12, 2024
1 parent 7c7fa9d commit afd0d4e
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 22 deletions.
7 changes: 4 additions & 3 deletions compliance_checker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ def __del__(self):
are cleared before the next checker uses it. Some caches were
inadvertently mutated by other functions.
"""

cfutil.get_geophysical_variables.cache_clear()
cfutil.get_time_variables.cache_clear()
# odd errors -- module getting deleted before this object?
if cfutil is not None:
cfutil.get_geophysical_variables.cache_clear()
cfutil.get_time_variables.cache_clear()


class BaseNCCheck:
Expand Down
6 changes: 5 additions & 1 deletion compliance_checker/cf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import requests
from cf_units import Unit
from importlib_resources import files
try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

Check warning on line 11 in compliance_checker/cf/util.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cf/util.py#L10-L11

Added lines #L10 - L11 were not covered by tests

from lxml import etree
from netCDF4 import Dataset

Expand Down
5 changes: 4 additions & 1 deletion compliance_checker/cfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from functools import lru_cache, partial

from cf_units import Unit
from importlib_resources import files
try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

Check warning on line 15 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L14-L15

Added lines #L14 - L15 were not covered by tests

_UNITLESS_DB = None
_SEA_NAMES = None
Expand Down
36 changes: 24 additions & 12 deletions compliance_checker/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import warnings
from collections import defaultdict
from datetime import datetime, timezone
from packaging import version
from operator import itemgetter
from pathlib import Path
from urllib.parse import urlparse
Expand All @@ -22,8 +23,10 @@
from netCDF4 import Dataset
from owslib.sos import SensorObservationService
from owslib.swe.sensor.sml import SensorML
from packaging.version import parse
from pkg_resources import working_set
if sys.version_info >= (3, 9):
import importlib.metadata as impmd
else:
import importlib_metadata as impmd

Check warning on line 29 in compliance_checker/suite.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/suite.py#L29

Added line #L29 was not covered by tests

from compliance_checker import __version__, tempnc
from compliance_checker.base import BaseCheck, GenericFile, Result, fix_return_value
Expand Down Expand Up @@ -71,10 +74,11 @@ def _get_generator_plugins(cls):
Return a list of classes from external plugins that are used to
generate checker classes
"""

# NOTE: updated to not use pkg_resources, but
# not tested -- it is ever used?
if not hasattr(cls, "suite_generators"):
gens = working_set.iter_entry_points("compliance_checker.generators")
cls.suite_generators = [x.resolve() for x in gens]
gens = impmd.entry_points(group='compliance_checker.generators')
cls.suite_generators = [x.load() for x in gens]

Check warning on line 81 in compliance_checker/suite.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/suite.py#L80-L81

Added lines #L80 - L81 were not covered by tests

return cls.suite_generators

Expand Down Expand Up @@ -136,7 +140,9 @@ def load_all_available_checkers(cls):
Helper method to retrieve all sub checker classes derived from various
base classes.
"""
cls._load_checkers(working_set.iter_entry_points("compliance_checker.suites"))
checkers = impmd.entry_points(group='compliance_checker.suites')
cls._load_checkers(checkers)


@classmethod
def _load_checkers(cls, checkers):
Expand All @@ -147,7 +153,8 @@ def _load_checkers(cls, checkers):

for c in checkers:
try:
check_obj = c.resolve()
# check_obj = c.resolve()
check_obj = c.load()
if hasattr(check_obj, "_cc_spec") and hasattr(
check_obj,
"_cc_spec_version",
Expand Down Expand Up @@ -186,8 +193,8 @@ def _load_checkers(cls, checkers):
for spec, versions in itertools.groupby(ver_checkers, itemgetter(0)):
version_nums = [v[-1] for v in versions]
try:
latest_version = str(max(parse(v) for v in version_nums))
# if the version can't be parsed, do it according to character collation
latest_version = str(max(version.parse(v) for v in version_nums))
# if the version can't be parsed, sort according to character collation
except ValueError:
latest_version = max(version_nums)
cls.checkers[spec] = cls.checkers[spec + ":latest"] = cls.checkers[
Expand Down Expand Up @@ -764,9 +771,14 @@ def generate_dataset(self, cdl_path):
:param str cdl_path: Absolute path to cdl file that is used to generate netCDF file
"""
if isinstance(cdl_path, str):
cdl_path = Path(cdl_path)
ds_str = cdl_path.with_suffix(".nc")
# better to update the following code with Path object -- some day
cdl_path = os.fspath(cdl_path)
if (
".cdl" in cdl_path
): # it's possible the filename doesn't have the .cdl extension
ds_str = cdl_path.replace(".cdl", ".nc")
else:
ds_str = cdl_path + ".nc"

Check warning on line 781 in compliance_checker/suite.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/suite.py#L781

Added line #L781 was not covered by tests

# generate netCDF-4 file
iostat = subprocess.run(
Expand Down
5 changes: 4 additions & 1 deletion compliance_checker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from itertools import chain

import pytest
from importlib_resources import files
try:
from importlib.resources import files
except ImportError:
from importlib_resources import files
from netCDF4 import Dataset

from compliance_checker.cf import util
Expand Down
5 changes: 4 additions & 1 deletion compliance_checker/tests/resources.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import subprocess

from importlib_resources import files
try:
from importlib.resources import files
except ImportError:
from importlib_resources import files


def get_filename(path):
Expand Down
3 changes: 2 additions & 1 deletion compliance_checker/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def checker_1():
def checker_2():
return Namespace(_cc_spec="checker_2", _cc_spec_version="2.2")

mock_checkers = [Namespace(resolve=checker_1), Namespace(resolve=checker_2)]
mock_checkers = [Namespace(load=checker_1),
Namespace(load=checker_2)]
with pytest.warns(DeprecationWarning):
CheckSuite._load_checkers(mock_checkers)

Expand Down
8 changes: 6 additions & 2 deletions compliance_checker/tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from pathlib import Path

import numpy as np
from importlib_resources import files

try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

from compliance_checker.acdd import ACDDBaseCheck
from compliance_checker.base import BaseCheck, GenericFile, Result
Expand Down Expand Up @@ -83,7 +87,7 @@ def test_generate_dataset_netCDF4(self):
# create netCDF4 file
ds_name = self.cs.generate_dataset(static_files["netCDF4"])
# check if correct name is return
assert ds_name == static_files["netCDF4"].with_suffix(".nc")
assert ds_name == str(static_files["netCDF4"].with_suffix(".nc"))
# check if netCDF4 file was created
assert os.path.isfile(static_files["netCDF4"].with_suffix(".nc"))

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cf-units>=2
cftime>=1.1.0
importlib-resources # drop this when dropping Python 3.8
importlib-metadata # drop this when dropping Python 3.8
isodate>=0.6.1
jinja2>=2.7.3
lxml>=3.2.1
Expand Down

0 comments on commit afd0d4e

Please sign in to comment.