Skip to content

Commit

Permalink
Add argument to generate ee credentials on-demand.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 710437577
  • Loading branch information
Xee authors committed Jan 9, 2025
1 parent 180132b commit 4f5f2c3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
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()

0 comments on commit 4f5f2c3

Please sign in to comment.