Skip to content

Commit

Permalink
Fix cds credentials (#1035)
Browse files Browse the repository at this point in the history
* fixed cds_credentials in case of prompt, including testcase

* updated whatsnew

* resolved fixture marks warning

* updated minimal copernicusmarine version
  • Loading branch information
veenstrajelmer authored Oct 28, 2024
1 parent 8abfb65 commit 39431a6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
6 changes: 4 additions & 2 deletions dfm_tools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ 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<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
#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
# 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
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 16 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import xarray as xr
import glob
import numpy as np
from unittest.mock import patch


def get_cds_url_key():
Expand All @@ -46,6 +47,21 @@ 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():
Expand Down

0 comments on commit 39431a6

Please sign in to comment.