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

removed last of pkg_resources. #1060

Merged
merged 5 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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()
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved


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
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved

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, 10):
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
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved

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 @@
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')
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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')
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved
cls._load_checkers(checkers)


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

for c in checkers:
try:
check_obj = c.resolve()
# check_obj = c.resolve()
check_obj = c.load()
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(check_obj, "_cc_spec") and hasattr(
check_obj,
"_cc_spec_version",
Expand Down Expand Up @@ -186,8 +193,8 @@
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 @@

: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