Skip to content

Commit b9a91e9

Browse files
author
Xee authors
committed
Add argument to generate ee credentials on-demand.
PiperOrigin-RevId: 713500577
1 parent 180132b commit b9a91e9

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

xee/ext.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
from __future__ import annotations
2020

2121
import concurrent.futures
22+
import copy
2223
import functools
2324
import importlib
2425
import itertools
2526
import logging
2627
import math
2728
import os
2829
import sys
29-
from typing import Any, Dict, Iterable, List, Literal, Optional, Sequence, Tuple, Union
30+
from typing import Any, Callable, Dict, Iterable, List, Literal, Optional, Sequence, Tuple, Union
3031
from urllib import parse
3132
import warnings
3233

@@ -805,7 +806,8 @@ def _ee_init_check(self):
805806
'Attempting to initialize using application default credentials.'
806807
)
807808

808-
ee.Initialize(**(self.store.ee_init_kwargs or {}))
809+
ee_init_kwargs = _parse_ee_init_kwargs(self.store.ee_init_kwargs)
810+
ee.Initialize(**ee_init_kwargs)
809811

810812
def __getitem__(self, key: indexing.ExplicitIndexer) -> np.typing.ArrayLike:
811813
return indexing.explicit_indexing_adapter(
@@ -1165,3 +1167,34 @@ def open_dataset(
11651167
)
11661168

11671169
return ds
1170+
1171+
1172+
def _parse_ee_init_kwargs(
1173+
ee_init_kwargs: Optional[Dict[str, Any]],
1174+
) -> Dict[str, Any]:
1175+
"""Parses Earth Engine Initialize kwargs.
1176+
1177+
Generate credentials if credentials_function is specified.
1178+
1179+
Args:
1180+
ee_init_kwargs: A dictionary of keyword arguments to pass to Earth Engine
1181+
Initialize, or None.
1182+
1183+
Returns:
1184+
A dictionary of keyword arguments to pass to Earth Engine Initialize.
1185+
"""
1186+
ee_init_kwargs = copy.copy(ee_init_kwargs) or {}
1187+
if (
1188+
'credentials' in ee_init_kwargs
1189+
and 'credentials_function' in ee_init_kwargs
1190+
):
1191+
raise ValueError(
1192+
'Cannot specify both credentials and credentials_function.'
1193+
)
1194+
if 'credentials_function' in ee_init_kwargs:
1195+
credentials_function: Callable[[], Any] = ee_init_kwargs.pop(
1196+
'credentials_function'
1197+
)
1198+
ee_init_kwargs['credentials'] = credentials_function()
1199+
1200+
return ee_init_kwargs

xee/ext_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,49 @@ def test_exceeding_byte_limit__raises_error(self):
9797
ext._check_request_limit(chunks, dtype_size, xee.REQUEST_BYTE_LIMIT)
9898

9999

100+
class ParseEEInitKwargsTest(absltest.TestCase):
101+
102+
def test_parse_ee_init_kwargs__empty(self):
103+
self.assertDictEqual(ext._parse_ee_init_kwargs(None), {})
104+
105+
def test_parse_ee_init_kwargs__credentials(self):
106+
self.assertDictEqual(
107+
ext._parse_ee_init_kwargs(
108+
{
109+
'credentials': 'foo',
110+
'other': 'bar',
111+
}
112+
),
113+
{
114+
'credentials': 'foo',
115+
'other': 'bar',
116+
},
117+
)
118+
119+
def test_parse_ee_init_kwargs__credentials_function(self):
120+
self.assertDictEqual(
121+
ext._parse_ee_init_kwargs(
122+
{
123+
'credentials_function': lambda: 'foo',
124+
'other': 'bar',
125+
}
126+
),
127+
{
128+
'credentials': 'foo',
129+
'other': 'bar',
130+
},
131+
)
132+
133+
def test_parse_ee_init_kwargs__credentials_and_credentials_function(self):
134+
with self.assertRaises(ValueError):
135+
ext._parse_ee_init_kwargs(
136+
{
137+
'credentials': 'foo',
138+
'credentials_function': lambda: 'foo',
139+
'other': 'bar',
140+
}
141+
)
142+
143+
100144
if __name__ == '__main__':
101145
absltest.main()

0 commit comments

Comments
 (0)