Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument to generate ee credentials on-demand. #202

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions xee/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
from __future__ import annotations

import concurrent.futures
import copy
import functools
import importlib
import itertools
import logging
import math
import os
import sys
from typing import Any, Dict, Iterable, List, Literal, Optional, Sequence, Tuple, Union
from typing import Any, Callable, Dict, Iterable, List, Literal, Optional, Sequence, Tuple, Union
from urllib import parse
import warnings

Expand Down Expand Up @@ -805,7 +806,8 @@ def _ee_init_check(self):
'Attempting to initialize using application default credentials.'
)

ee.Initialize(**(self.store.ee_init_kwargs or {}))
ee_init_kwargs = _parse_ee_init_kwargs(self.store.ee_init_kwargs)
ee.Initialize(**ee_init_kwargs)

def __getitem__(self, key: indexing.ExplicitIndexer) -> np.typing.ArrayLike:
return indexing.explicit_indexing_adapter(
Expand Down Expand Up @@ -1165,3 +1167,34 @@ def open_dataset(
)

return ds


def _parse_ee_init_kwargs(
ee_init_kwargs: Optional[Dict[str, Any]],
) -> Dict[str, Any]:
"""Parses Earth Engine Initialize kwargs.

Generate credentials if credentials_function is specified.

Args:
ee_init_kwargs: A dictionary of keyword arguments to pass to Earth Engine
Initialize, or None.

Returns:
A dictionary of keyword arguments to pass to Earth Engine Initialize.
"""
ee_init_kwargs = copy.copy(ee_init_kwargs) or {}
if (
'credentials' in ee_init_kwargs
and 'credentials_function' in ee_init_kwargs
):
raise ValueError(
'Cannot specify both credentials and credentials_function.'
)
if 'credentials_function' in ee_init_kwargs:
credentials_function: Callable[[], Any] = ee_init_kwargs.pop(
'credentials_function'
)
ee_init_kwargs['credentials'] = credentials_function()

return ee_init_kwargs
44 changes: 44 additions & 0 deletions xee/ext_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,49 @@ def test_exceeding_byte_limit__raises_error(self):
ext._check_request_limit(chunks, dtype_size, xee.REQUEST_BYTE_LIMIT)


class ParseEEInitKwargsTest(absltest.TestCase):

def test_parse_ee_init_kwargs__empty(self):
self.assertDictEqual(ext._parse_ee_init_kwargs(None), {})

def test_parse_ee_init_kwargs__credentials(self):
self.assertDictEqual(
ext._parse_ee_init_kwargs(
{
'credentials': 'foo',
'other': 'bar',
}
),
{
'credentials': 'foo',
'other': 'bar',
},
)

def test_parse_ee_init_kwargs__credentials_function(self):
self.assertDictEqual(
ext._parse_ee_init_kwargs(
{
'credentials_function': lambda: 'foo',
'other': 'bar',
}
),
{
'credentials': 'foo',
'other': 'bar',
},
)

def test_parse_ee_init_kwargs__credentials_and_credentials_function(self):
with self.assertRaises(ValueError):
ext._parse_ee_init_kwargs(
{
'credentials': 'foo',
'credentials_function': lambda: 'foo',
'other': 'bar',
}
)


if __name__ == '__main__':
absltest.main()
Loading