Skip to content

Commit

Permalink
Add feature to check_metadata_archive_compliance to raise error
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Nov 3, 2023
1 parent 55518c6 commit 6c769e6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
9 changes: 6 additions & 3 deletions disdrodb/data_transfer/scripts/download_disdrodb_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------.
####################################################################
## Wrapper to download disdrodb archives by command lines ##
####################################################################
"""Wrapper to download stations from the DISDRODB Decentralized Data Archive."""

import sys

import click

from disdrodb.data_transfer.download_data import click_download_option

sys.tracebacklimit = 0 # avoid full traceback error if occur


@click.command()
@click_download_option
Expand Down
15 changes: 12 additions & 3 deletions disdrodb/metadata/check_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,17 @@ def check_archive_metadata_reader(base_dir: str = None) -> bool:
return is_valid


def check_archive_metadata_compliance(base_dir: str = None):
def check_archive_metadata_compliance(base_dir: str = None, raise_error=False):
"""Check the archive metadata compliance.
Parameters
----------
base_dir : str (optional)
Base directory of DISDRODB. Format: <...>/DISDRODB
If None (the default), the disdrodb config variable 'dir' is used.
raise_error: bool (optional)
Whether to raise an error and interrupt the archive check if a
metadata is not compliant. The default is False.
Returns
-------
Expand All @@ -512,6 +515,7 @@ def check_archive_metadata_compliance(base_dir: str = None):
data_source = _infer_data_source_from_path(fpath)
campaign_name = _infer_campaign_name_from_path(fpath)
station_name = os.path.basename(fpath).replace(".yml", "")
# Check compliance
try:
check_metadata_compliance(
base_dir=base_dir,
Expand All @@ -521,8 +525,13 @@ def check_archive_metadata_compliance(base_dir: str = None):
)
except Exception as e:
is_valid = False
print(f"Error for {data_source} {campaign_name} {station_name}.")
print(f"The error is: {e}.")
msg = f"Error for {data_source} {campaign_name} {station_name}."
msg = msg + f"The error is: {e}."
if raise_error:
raise ValueError(msg)
else:
print(msg)

return is_valid


Expand Down
8 changes: 6 additions & 2 deletions disdrodb/metadata/scripts/check_metadata_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------.
"""Wrapper to check DISDRODB Metadata Archive Compliance from terminal."""
import sys

import click

sys.tracebacklimit = 0 # avoid full traceback error if occur


@click.command()
@click.option("--base_dir", type=str, show_default=True, default=None, help="DISDRODB root directory")
def disdrodb_check_metadata_archive(base_dir=None):
def check_metadata_archive(base_dir=None):
from disdrodb.metadata.check_metadata import check_archive_metadata_compliance

check_archive_metadata_compliance(base_dir=base_dir)
check_archive_metadata_compliance(base_dir=base_dir, raise_error=True)
10 changes: 9 additions & 1 deletion disdrodb/tests/test_metadata/test_check_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ def test_check_archive_metadata_reader(tmp_path):


def test_check_archive_metadata_compliance(tmp_path):
# from pathlib import Path
# tmp_path = Path("/tmp/")

base_dir = os.path.join(tmp_path, "DISDRODB")

# We check only the failure, the success are tested in the above tests.
Expand All @@ -283,9 +286,14 @@ def test_check_archive_metadata_compliance(tmp_path):
campaign_name = "campaign_name"
yaml_dict = {"reader": ""}
create_fake_metadata_file(tmp_path, yaml_file_name, yaml_dict, data_source, campaign_name)
result = check_archive_metadata_compliance(base_dir)
# Test does not raise error !
result = check_archive_metadata_compliance(base_dir, raise_error=False)
assert result is False

# Test it raise error
with pytest.raises(ValueError):
result = check_archive_metadata_compliance(base_dir, raise_error=True)


def test_check_archive_metadata_geolocation(tmp_path):
# We check only the failure, the success are tested in the above test.
Expand Down

0 comments on commit 6c769e6

Please sign in to comment.