Skip to content

Commit

Permalink
only essential pkg_resource fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Apr 13, 2024
1 parent b957367 commit ebe9fc8
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 51 deletions.
7 changes: 3 additions & 4 deletions compliance_checker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions compliance_checker/cf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions compliance_checker/cfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 14 additions & 22 deletions compliance_checker/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 1 addition & 4 deletions compliance_checker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions compliance_checker/tests/resources.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 1 addition & 2 deletions compliance_checker/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions compliance_checker/tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))

Expand Down

0 comments on commit ebe9fc8

Please sign in to comment.