Skip to content

Commit

Permalink
Issue #49: Replace pythesint with metvocab tool (#84)
Browse files Browse the repository at this point in the history
* Add metvocab as a dependency
* Replace CF standard lookup
* Replace vocabulary lookup
* Remove pythesint
  • Loading branch information
vkbo authored Sep 15, 2021
1 parent d41a82c commit 07ecb54
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
63 changes: 46 additions & 17 deletions dmci/tools/check_mmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"""

import logging
import pythesint as pti

from metvocab import CFStandard, MMDVocab

from lxml import etree
from urllib.parse import urlparse
Expand All @@ -29,9 +30,31 @@
class CheckMMD():

def __init__(self):

self._status_pass = []
self._status_fail = []
self._status_ok = True

self._cf_standard = CFStandard()
self._cf_standard.init_vocab()
self._status_ok &= self._cf_standard.is_initialised

self._access_constraing = MMDVocab("mmd", "https://vocab.met.no/mmd/Access_Constraint")
self._access_constraing.init_vocab()
self._status_ok &= self._access_constraing.is_initialised

self._activity_type = MMDVocab("mmd", "https://vocab.met.no/mmd/Activity_Type")
self._activity_type.init_vocab()
self._status_ok &= self._activity_type.is_initialised

self._operational_status = MMDVocab("mmd", "https://vocab.met.no/mmd/Operational_Status")
self._operational_status.init_vocab()
self._status_ok &= self._operational_status.is_initialised

self._use_constraint = MMDVocab("mmd", "https://vocab.met.no/mmd/Use_Constraint")
self._use_constraint.init_vocab()
self._status_ok &= self._use_constraint.is_initialised

return

def clear(self):
Expand Down Expand Up @@ -172,19 +195,22 @@ def check_cf(self, xmldoc):
cf_list = [elem.text for elem in cf_elements[0]]
if len(cf_list) > 1:
err.append("Only one CF name should be provided, got %d." % len(cf_list))
ok = False
ok &= False

# Check CF names even if more than one provided
for cf_name in cf_list:
try:
pti.get_cf_standard_name(cf_name)
except IndexError:
err.append("Keyword '%s' is not a CF standard name." % cf_name)
ok = False
cf_ok = self._cf_standard.check_standard_name(cf_name)
if not cf_ok:
err.append("Keyword '%s' is not a CF standard name." % cf_name)
ok &= False
except Exception:
err.append("Internal Error: CF standard name lookup failed.")
ok &= False

elif n_cf > 1:
err.append("More than one CF entry found. Only one is allowed.")
ok = False
ok &= False

if n_cf > 0:
self._log_result("Climate and Forecast Standard Names Check", ok, err)
Expand Down Expand Up @@ -218,10 +244,10 @@ def check_vocabulary(self, xmldoc):
List of errors
"""
vocabularies = {
"access_constraint": pti.get_mmd_access_constraints,
"activity_type": pti.get_mmd_activity_type,
"operational_status": pti.get_mmd_operstatus,
"use_constraint": pti.get_mmd_use_constraint_type,
"access_constraint": self._access_constraing.check_concept_value,
"activity_type": self._activity_type.check_concept_value,
"operational_status": self._operational_status.check_concept_value,
"use_constraint": self._use_constraint.check_concept_value,
}
ok = True
err = []
Expand All @@ -237,12 +263,15 @@ def check_vocabulary(self, xmldoc):
for rep in elems_found:
num += 1
try:
f_name(rep.text)
except IndexError:
err.append("Incorrect vocabulary '%s' for element '%s'." % (
rep.text, element_name
))
ok = False
v_ok = f_name(rep.text)
if not v_ok:
err.append("Incorrect vocabulary '%s' for element '%s'." % (
rep.text, element_name
))
ok &= False
except Exception:
err.append("Internal Error: '%s' vocabulary lookup failed." % element_name)
ok &= False

if num > 0:
self._log_result("Controlled Vocabularies Check", ok, err)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pythesint @ git+/~https://github.com/metno/py-thesaurus-interface@f43f9ec70131fcbb46f129ea566a0ca241b3ba67
metvocab @ git+/~https://github.com/metno/met-vocab-tools@c6d51ad3370b65ae1ebd805cd14b2a9e69d8922f
requests>=2.22
pyyaml>=5.1
flask>=1.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ include_package_data = True
packages = find:
scripts = dmci_start_api.py
install_requires =
pythesint @ git+/~https://github.com/metno/py-thesaurus-interface@f43f9ec70131fcbb46f129ea566a0ca241b3ba67
metvocab @ git+/~https://github.com/metno/met-vocab-tools@c6d51ad3370b65ae1ebd805cd14b2a9e69d8922f
requests>=2.22
pyyaml>=5.1
flask>=1.0
Expand Down
30 changes: 28 additions & 2 deletions tests/test_mmd_tools/test_mmd_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import pytest

from lxml import etree
from metvocab import CFStandard, MMDVocab

from tools import causeOSError

from dmci.tools import CheckMMD

Expand Down Expand Up @@ -249,7 +252,7 @@ def testMMDTools_CheckURLs():


@pytest.mark.tools
def testMMDTools_CheckCF():
def testMMDTools_CheckCF(monkeypatch):
"""Test the check_cf function."""
chkMMD = CheckMMD()

Expand Down Expand Up @@ -301,11 +304,25 @@ def testMMDTools_CheckCF():
assert err == ["More than one CF entry found. Only one is allowed."]
assert n == 2

# Check Exception
with monkeypatch.context() as mp:
mp.setattr(CFStandard, "check_standard_name", causeOSError)
ok, err, n = chkMMD.check_cf(etree.ElementTree(etree.XML(
"<root>"
" <keywords vocabulary='Climate and Forecast Standard Names'>"
" <keyword>sea_surface_temperature</keyword>"
" </keywords>"
"</root>"
)))
assert ok is False
assert err == ["Internal Error: CF standard name lookup failed."]
assert n == 1

# END Test testMMDTools_CheckCF


@pytest.mark.tools
def testMMDTools_CheckVocabulary():
def testMMDTools_CheckVocabulary(monkeypatch):
"""Test the check_vocabulary function."""
chkMMD = CheckMMD()
ok, err = chkMMD.check_vocabulary(etree.ElementTree(etree.XML(
Expand All @@ -320,6 +337,15 @@ def testMMDTools_CheckVocabulary():
assert ok is False
assert err == ["Incorrect vocabulary 'OOperational' for element 'operational_status'."]

# Check Exception
with monkeypatch.context() as mp:
mp.setattr(MMDVocab, "check_concept_value", causeOSError)
ok, err = chkMMD.check_vocabulary(etree.ElementTree(etree.XML(
"<root><operational_status>Operational</operational_status></root>"
)))
assert ok is False
assert err == ["Internal Error: 'operational_status' vocabulary lookup failed."]

# END Test testMMDTools_CheckVocabulary


Expand Down

0 comments on commit 07ecb54

Please sign in to comment.