diff --git a/compliance_checker/base.py b/compliance_checker/base.py index 64c2685c..c2674e85 100644 --- a/compliance_checker/base.py +++ b/compliance_checker/base.py @@ -191,10 +191,9 @@ def __del__(self): are cleared before the next checker uses it. Some caches were inadvertently mutated by other functions. """ - # 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() + + cfutil.get_geophysical_variables.cache_clear() + cfutil.get_time_variables.cache_clear() class BaseNCCheck: diff --git a/compliance_checker/cf/util.py b/compliance_checker/cf/util.py index 61e44f75..6f100653 100644 --- a/compliance_checker/cf/util.py +++ b/compliance_checker/cf/util.py @@ -5,11 +5,7 @@ import requests from cf_units import Unit -try: - from importlib.resources import files -except ImportError: - from importlib_resources import files - +from importlib_resources import files from lxml import etree from netCDF4 import Dataset diff --git a/compliance_checker/cfutil.py b/compliance_checker/cfutil.py index 2f16ac22..245c6fd3 100644 --- a/compliance_checker/cfutil.py +++ b/compliance_checker/cfutil.py @@ -9,10 +9,7 @@ from functools import lru_cache, partial from cf_units import Unit -try: - from importlib.resources import files -except ImportError: - from importlib_resources import files +from importlib_resources import files _UNITLESS_DB = None _SEA_NAMES = None diff --git a/compliance_checker/suite.py b/compliance_checker/suite.py index ec3631d0..d261a1cc 100644 --- a/compliance_checker/suite.py +++ b/compliance_checker/suite.py @@ -13,20 +13,17 @@ 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 +import importlib_metadata import requests from lxml import etree as ET from netCDF4 import Dataset from owslib.sos import SensorObservationService from owslib.swe.sensor.sml import SensorML -if sys.version_info >= (3, 10): - import importlib.metadata as impmd -else: - import importlib_metadata as impmd +from packaging.version import parse from compliance_checker import __version__, tempnc from compliance_checker.base import BaseCheck, GenericFile, Result, fix_return_value @@ -74,10 +71,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 = impmd.entry_points(group='compliance_checker.generators') + gens = importlib_metadata.entry_points( + groups="compliance_checker.generators", + ) cls.suite_generators = [x.load() for x in gens] return cls.suite_generators @@ -140,9 +138,9 @@ def load_all_available_checkers(cls): Helper method to retrieve all sub checker classes derived from various base classes. """ - checkers = impmd.entry_points(group='compliance_checker.suites') - cls._load_checkers(checkers) - + cls._load_checkers( + importlib_metadata.entry_points(group="compliance_checker.suites"), + ) @classmethod def _load_checkers(cls, checkers): @@ -153,7 +151,6 @@ def _load_checkers(cls, checkers): for c in checkers: try: - # check_obj = c.resolve() check_obj = c.load() if hasattr(check_obj, "_cc_spec") and hasattr( check_obj, @@ -193,8 +190,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(version.parse(v) for v in version_nums)) - # if the version can't be parsed, sort according to character collation + latest_version = str(max(parse(v) for v in version_nums)) + # if the version can't be parsed, do it according to character collation except ValueError: latest_version = max(version_nums) cls.checkers[spec] = cls.checkers[spec + ":latest"] = cls.checkers[ @@ -771,14 +768,9 @@ def generate_dataset(self, cdl_path): :param str cdl_path: Absolute path to cdl file that is used to generate netCDF file """ - # 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" + if isinstance(cdl_path, str): + cdl_path = Path(cdl_path) + ds_str = cdl_path.with_suffix(".nc") # generate netCDF-4 file iostat = subprocess.run( diff --git a/compliance_checker/tests/conftest.py b/compliance_checker/tests/conftest.py index 178ab73a..2c662c16 100644 --- a/compliance_checker/tests/conftest.py +++ b/compliance_checker/tests/conftest.py @@ -3,10 +3,7 @@ from itertools import chain import pytest -try: - from importlib.resources import files -except ImportError: - from importlib_resources import files +from importlib_resources import files from netCDF4 import Dataset from compliance_checker.cf import util diff --git a/compliance_checker/tests/resources.py b/compliance_checker/tests/resources.py index e61fa290..7bbca9d6 100644 --- a/compliance_checker/tests/resources.py +++ b/compliance_checker/tests/resources.py @@ -1,9 +1,6 @@ import subprocess -try: - from importlib.resources import files -except ImportError: - from importlib_resources import files +from importlib_resources import files def get_filename(path): diff --git a/compliance_checker/tests/test_cli.py b/compliance_checker/tests/test_cli.py index cb3c21a7..c86cd897 100644 --- a/compliance_checker/tests/test_cli.py +++ b/compliance_checker/tests/test_cli.py @@ -91,8 +91,7 @@ def checker_1(): def checker_2(): return Namespace(_cc_spec="checker_2", _cc_spec_version="2.2") - mock_checkers = [Namespace(load=checker_1), - Namespace(load=checker_2)] + mock_checkers = [Namespace(load=checker_1), Namespace(load=checker_2)] with pytest.warns(DeprecationWarning): CheckSuite._load_checkers(mock_checkers) diff --git a/compliance_checker/tests/test_suite.py b/compliance_checker/tests/test_suite.py index 7faa2eea..8c8987df 100644 --- a/compliance_checker/tests/test_suite.py +++ b/compliance_checker/tests/test_suite.py @@ -3,11 +3,7 @@ from pathlib import Path import numpy as np - -try: - from importlib.resources import files -except ImportError: - from importlib_resources import files +from importlib_resources import files from compliance_checker.acdd import ACDDBaseCheck from compliance_checker.base import BaseCheck, GenericFile, Result @@ -87,7 +83,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 == str(static_files["netCDF4"].with_suffix(".nc")) + assert ds_name == static_files["netCDF4"].with_suffix(".nc") # check if netCDF4 file was created assert os.path.isfile(static_files["netCDF4"].with_suffix(".nc"))