Skip to content

Commit

Permalink
make cf-units optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Nov 12, 2024
1 parent cc630ca commit 17b248f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
48 changes: 37 additions & 11 deletions compliance_checker/cfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from functools import lru_cache, partial

from importlib_resources import files
from pyudunits2 import UnitSystem, UnresolvableUnitException

ut_system = UnitSystem.from_udunits2_xml()

_UNITLESS_DB = None
_SEA_NAMES = None
Expand Down Expand Up @@ -2032,30 +2029,59 @@ def guess_feature_type(nc, variable):
return "reduced-grid"


class _CCUnit:
def __init__(self, units):
import cf_units

Check warning on line 2034 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2034

Added line #L2034 was not covered by tests

self.u = cf_units.Unit(units)
self.is_time_reference = self.u.is_time_reference()

Check warning on line 2037 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2036-L2037

Added lines #L2036 - L2037 were not covered by tests

def __eq__(self, other):
return self.u == other.u

Check warning on line 2040 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2040

Added line #L2040 was not covered by tests

def is_convertible_to(self, other):
if isinstance(other, str):
ret = self.u.is_convertible(other)

Check warning on line 2044 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2044

Added line #L2044 was not covered by tests
else:
ret = self.u.is_convertible(other.u)
return ret

Check warning on line 2047 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2046-L2047

Added lines #L2046 - L2047 were not covered by tests

def is_dimensionless(self):
return self.u.is_dimensionless()

Check warning on line 2050 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2050

Added line #L2050 was not covered by tests

def expanded(self):
return self.u.definition

Check warning on line 2053 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2053

Added line #L2053 was not covered by tests


def _units(units: str):
"""PLACEHOLDER."""
# FIXME:
# cf_units will create:
# cf_units.Unit(None)
# Unit('unknown')
# that will contain all the methods we use here.
# FIXME: Try to make cf_units optional
try:
return _CCUnit(units)
except ImportError:
from pyudunits2 import UnitSystem, UnresolvableUnitException

Check warning on line 2062 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2059-L2062

Added lines #L2059 - L2062 were not covered by tests

ut_system = UnitSystem.from_udunits2_xml()

Check warning on line 2064 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2064

Added line #L2064 was not covered by tests

# FIXME: cf_units.Unit(None) -> Unit('unknown')
if units is None:
units = ""

Check warning on line 2068 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2068

Added line #L2068 was not covered by tests
# FIXME: Syntax Error when HH:MM:SS is present in time reference.
if "T00:00:00" in units:
units = units.replace("T00:00:00", "")

Check warning on line 2071 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2071

Added line #L2071 was not covered by tests

# FIXME: cf_units raised only ValueError
try:
u = ut_system.unit(units)
except (SyntaxError, UnresolvableUnitException) as err:
raise ValueError from err

Check warning on line 2077 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2074-L2077

Added lines #L2074 - L2077 were not covered by tests
# FIXME: cf_units defined .is_time_reference for time reference units.
u.is_time_reference = False
u.is_long_time_interval = False
try:

Check warning on line 2080 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2079-L2080

Added lines #L2079 - L2080 were not covered by tests
if hasattr(u._definition, "shift_from"):
u.is_time_reference = True
if u._definition.unit.content in ("months", "years"):
u.is_long_time_interval = True
except KeyError:

Check warning on line 2083 in compliance_checker/cfutil.py

View check run for this annotation

Codecov / codecov/patch

compliance_checker/cfutil.py#L2082-L2083

Added lines #L2082 - L2083 were not covered by tests
# FIXME: hasattr should return None in that case.
# pyudunits2/_expr_graph.py:27, in Node.__getattr__(self, name)
# 25 def __getattr__(self, name):
# 26 # Allow the dictionary to raise KeyError if the key doesn't exist.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ requires = [
[project]
name = "compliance-checker"
description = "Checks Datasets and SOS endpoints for standards compliance"
readme = "README.md"
readme = { file = "README.md", content-type = "text/markdown" }
license = { text = "Apache-2.0" }
maintainers = [
{ name = "Dave Foster", email = "[email protected]" },
Expand Down Expand Up @@ -40,6 +40,7 @@ dynamic = [
"dependencies",
"version",
]
optional-dependencies.extras = [ "cf-units>=2" ]
urls.documentation = "https://ioos.github.io/compliance-checker"
urls.homepage = "https://compliance.ioos.us/index.html"
urls.repository = "https://github.com/ioos/compliance-checker"
Expand Down Expand Up @@ -81,7 +82,6 @@ compliance_checker = [
dependencies = { file = [
"requirements.txt",
] }
readme = { file = "README.md", content-type = "text/markdown" }

[tool.setuptools_scm]
write_to = "compliance_checker/_version.py"
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cf-units>=2
cftime>=1.1.0
importlib-metadata
importlib-resources
Expand Down

0 comments on commit 17b248f

Please sign in to comment.