From 1ef237d1c21aa4040ff0e9f8b9ee56bf45d65bfb Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 28 Oct 2024 19:54:01 +0100 Subject: [PATCH 1/6] added testcase for prompt --- tests/test_download.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_download.py b/tests/test_download.py index 240e7c25..ace13316 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -20,6 +20,7 @@ import xarray as xr import glob import numpy as np +from unittest.mock import patch def get_cds_url_key(): @@ -46,6 +47,22 @@ def test_cds_credentials(): cds_credentials() + +@patch("getpass.getpass") +def test_cds_credentials_prompt(getpass): + # backup credentials and remove credentials envvars and file + cds_url, cds_apikey = get_cds_url_key() + with pytest.raises(ValueError): + cds_remove_credentials_raise() + + # provide apikey to cds_credentials() prompt + getpass.return_value = cds_apikey + cds_credentials() + + # restore credentials file/envvars + set_cds_credentials_ifnot_none(cds_url, cds_apikey) + + @pytest.mark.requiressecrets @pytest.mark.unittest def test_cds_credentials_onlykey_envvars(): From 84ee30880ff6992acbbac3914c40e65bf00a51bc Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 28 Oct 2024 20:00:10 +0100 Subject: [PATCH 2/6] fixed cds_credentials --- dfm_tools/download.py | 6 ++++-- tests/test_download.py | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dfm_tools/download.py b/dfm_tools/download.py index 78294c1f..dc89fa03 100644 --- a/dfm_tools/download.py +++ b/dfm_tools/download.py @@ -96,11 +96,12 @@ def cds_credentials(): """ get cdsapikey from environment variables or file or query via getpass if necessary """ - # TODO: put this in a PR at https://github.com/ecmwf/cdsapi + cds_url_default = "https://cds.climate.copernicus.eu/api" + # TODO: put this in a PR at https://github.com/ecmwf/cdsapi if "CDSAPI_KEY" in os.environ.keys(): # in case of envvars, also set CDSAPI_URL envvar so it does not have to be supplied - cds_url = os.environ.get("CDSAPI_URL", "https://cds.climate.copernicus.eu/api") + cds_url = os.environ.get("CDSAPI_URL", cds_url_default) os.environ["CDSAPI_URL"] = cds_url try: @@ -115,6 +116,7 @@ def cds_credentials(): "your API-key from https://cds.climate.copernicus.eu/profile " "(first register, login and accept the terms). " "More info in https://forum.ecmwf.int/t/3743.") + cds_url = cds_url_default cds_apikey = getpass.getpass("\nEnter your ECMWF API-key (string with dashes): ") cds_set_credentials(cds_url, cds_apikey) else: diff --git a/tests/test_download.py b/tests/test_download.py index ace13316..71b3bccf 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -47,7 +47,6 @@ def test_cds_credentials(): cds_credentials() - @patch("getpass.getpass") def test_cds_credentials_prompt(getpass): # backup credentials and remove credentials envvars and file From af8cd7947841bc901ce34b1b9262be276b99985d Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 28 Oct 2024 20:03:33 +0100 Subject: [PATCH 3/6] updated whatsnew --- docs/whats-new.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/whats-new.md b/docs/whats-new.md index 0c61bd8c..3dc31d26 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -2,6 +2,9 @@ ## UNRELEASED +### Fix +- fixed CDS logging in for new users in `cds_credentials()` in [#1035](https://github.com/Deltares/dfm_tools/pull/1035) + ## 0.30.0 (2024-10-20) From d085869ff26fa0afc5643c839fd7d86d0a0689f3 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 28 Oct 2024 20:07:25 +0100 Subject: [PATCH 4/6] resolved warning --- tests/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index e1854308..b79101ca 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,7 +11,6 @@ import pandas as pd -@pytest.mark.requiressecrets @pytest.fixture def file_nc_era5_pattern(tmp_path): date_min = '2010-01-31' From d4b4abad4b09f53b2f76a2e2bc6548ea1f2b7e8b Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 28 Oct 2024 20:08:00 +0100 Subject: [PATCH 5/6] updated minimal copernicusmarine version --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d138af07..ab411bf3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,8 +44,8 @@ dependencies = [ "pydap>=3.4.0", #erddapy>=2.0.0 supports pandas>=2.0.0 "erddapy>=2.0.0", - #copernicusmarine>=1.3.3 is the latest version and the developers recommend to always use the latest version until 2.0.0 is released: https://github.com/Deltares/dfm_tools/issues/936 - "copernicusmarine>=1.3.3", + #copernicusmarine>=1.3.4 is the latest version and the developers recommend to always use the latest version until 2.0.0 is released: https://github.com/Deltares/dfm_tools/issues/936 + "copernicusmarine>=1.3.4", "copernicusmarine<2.0.0", # TODO: remove when dfm_tools is adjusted to not-yet-released 2.0 version: https://github.com/Deltares/dfm_tools/issues/933 #rws-ddlpy>=0.6.0 `ddlpy.measurements_amount()` returns all amounts "rws-ddlpy>=0.6.0", From eccc9e1a1f592ceb08fbd8c5ef2b7949304def67 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 28 Oct 2024 20:10:45 +0100 Subject: [PATCH 6/6] updated minimal copernicusmarine version --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ab411bf3..11ced5d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,8 +45,8 @@ dependencies = [ #erddapy>=2.0.0 supports pandas>=2.0.0 "erddapy>=2.0.0", #copernicusmarine>=1.3.4 is the latest version and the developers recommend to always use the latest version until 2.0.0 is released: https://github.com/Deltares/dfm_tools/issues/936 - "copernicusmarine>=1.3.4", - "copernicusmarine<2.0.0", # TODO: remove when dfm_tools is adjusted to not-yet-released 2.0 version: https://github.com/Deltares/dfm_tools/issues/933 + # TODO: remove when dfm_tools is adjusted to not-yet-released 2.0 version: https://github.com/Deltares/dfm_tools/issues/933 + "copernicusmarine>=1.3.4,<2.0.0", #rws-ddlpy>=0.6.0 `ddlpy.measurements_amount()` returns all amounts "rws-ddlpy>=0.6.0", #pooch>=1.1.0 has attribute retrieve