Skip to content

Commit

Permalink
Replace get_environment_variable with get_env_var
Browse files Browse the repository at this point in the history
  • Loading branch information
naschmitz committed Oct 10, 2024
1 parent 7349872 commit 99de732
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
8 changes: 3 additions & 5 deletions geemap/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ipywidgets as widgets
from IPython.display import display, HTML
from typing import Optional
from .common import get_environment_variable, temp_file_path
from .common import get_env_var, temp_file_path
from .geemap import Map, ee_initialize

try:
Expand Down Expand Up @@ -57,16 +57,14 @@ def __init__(
# Initialization

if project is None:
project = get_environment_variable(
"EE_PROJECT_ID"
) or get_environment_variable("GOOGLE_PROJECT_ID")
project = get_env_var("EE_PROJECT_ID") or get_env_var("GOOGLE_PROJECT_ID")
if project is None:
raise ValueError(
"Please provide a valid project ID via the 'project' parameter."
)

if google_api_key is None:
google_api_key = get_environment_variable("GOOGLE_API_KEY")
google_api_key = get_env_var("GOOGLE_API_KEY")
if google_api_key is None:
raise ValueError(
"Please provide a valid Google API key via the 'google_api_key' parameter."
Expand Down
8 changes: 4 additions & 4 deletions geemap/coreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
pass


def get_environment_variable(key: str) -> Optional[str]:
def get_env_var(key: str) -> Optional[str]:
"""Retrieves an environment variable or Colab secret for the given key.
Colab secrets have precedence over environment variables.
Expand Down Expand Up @@ -83,7 +83,7 @@ def ee_initialize(
kwargs["http_transport"] = httplib2.Http()

if project is None:
kwargs["project"] = get_environment_variable("EE_PROJECT_ID")
kwargs["project"] = get_env_var("EE_PROJECT_ID")
else:
kwargs["project"] = project

Expand All @@ -98,7 +98,7 @@ def ee_initialize(
auth_args["auth_mode"] = auth_mode

if ee.data._credentials is None:
ee_token = get_environment_variable(token_name)
ee_token = get_env_var(token_name)
if service_account:
try:
credential_file_path = os.path.expanduser(
Expand Down Expand Up @@ -340,7 +340,7 @@ def get_google_maps_api_key(key: str = "GOOGLE_MAPS_API_KEY") -> Optional[str]:
Returns:
str: The API key, or None if it could not be found.
"""
if api_key := get_environment_variable(key):
if api_key := get_env_var(key):
return api_key
return os.environ.get(key, None)

Expand Down
12 changes: 6 additions & 6 deletions geemap/maplibregl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,8 +1292,8 @@ def to_html(
html = html.replace(div_before, div_after)

if replace_key or (os.getenv("MAPTILER_REPLACE_KEY") is not None):
key_before = get_environment_variable("MAPTILER_KEY")
key_after = get_environment_variable("MAPTILER_KEY_PUBLIC")
key_before = get_env_var("MAPTILER_KEY")
key_after = get_env_var("MAPTILER_KEY_PUBLIC")
if key_after is not None:
html = html.replace(key_before, key_after)

Expand Down Expand Up @@ -2217,7 +2217,7 @@ def _get_3d_terrain_style(
"""

if api_key is None:
api_key = get_environment_variable(token)
api_key = get_env_var(token)

if api_key is None:
print("An API key is required to use the 3D terrain feature.")
Expand Down Expand Up @@ -2748,7 +2748,7 @@ def add_3d_buildings(
None
"""

MAPTILER_KEY = get_environment_variable("MAPTILER_KEY")
MAPTILER_KEY = get_env_var("MAPTILER_KEY")
source = {
"url": f"https://api.maptiler.com/tiles/v3/tiles.json?key={MAPTILER_KEY}",
"type": "vector",
Expand Down Expand Up @@ -2913,7 +2913,7 @@ def construct_maptiler_style(style: str, api_key: Optional[str] = None) -> str:
"""

if api_key is None:
api_key = get_environment_variable("MAPTILER_KEY")
api_key = get_env_var("MAPTILER_KEY")

url = f"https://api.maptiler.com/maps/{style}/style.json?key={api_key}"

Expand Down Expand Up @@ -2965,7 +2965,7 @@ def maptiler_3d_style(
"""

if api_key is None:
api_key = get_environment_variable(token)
api_key = get_env_var(token)

if api_key is None:
print("An API key is required to use the 3D terrain feature.")
Expand Down
20 changes: 10 additions & 10 deletions tests/test_coreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ class TestCoreUtils(unittest.TestCase):

def test_get_environment_invalid_key(self):
"""Verifies None is returned if keys are invalid."""
self.assertIsNone(coreutils.get_environment_variable(None))
self.assertIsNone(coreutils.get_environment_variable(""))
self.assertIsNone(coreutils.get_env_var(None))
self.assertIsNone(coreutils.get_env_var(""))

@mock.patch.dict(os.environ, {"key": "value"})
def test_get_environment_variable_unknown_key(self):
def test_get_env_var_unknown_key(self):
"""Verifies None is returned if the environment variable could not be found."""
self.assertIsNone(coreutils.get_environment_variable("unknown-key"))
self.assertIsNone(coreutils.get_env_var("unknown-key"))

@mock.patch.dict(os.environ, {"key": "value"})
def test_get_environment_variable_from_env(self):
def test_get_env_var_from_env(self):
"""Verifies environment variables are read from environment variables."""
self.assertEqual(coreutils.get_environment_variable("key"), "value")
self.assertEqual(coreutils.get_env_var("key"), "value")

@mock.patch.dict("sys.modules", {"google.colab": mock.Mock()})
def test_get_environment_variable_from_colab(self):
def test_get_env_var_from_colab(self):
"""Verifies environment variables are read from Colab secrets."""
mock_colab = sys.modules["google.colab"]
mock_colab.userdata.get.return_value = "colab-value"

self.assertEqual(coreutils.get_environment_variable("key"), "colab-value")
self.assertEqual(coreutils.get_env_var("key"), "colab-value")
mock_colab.userdata.get.assert_called_once_with("key")

@mock.patch.dict(os.environ, {"key": "environ-value"})
@mock.patch.dict("sys.modules", {"google.colab": mock.Mock()})
def test_get_environment_variable_colab_fails_fallback_to_env(self):
def test_get_env_var_colab_fails_fallback_to_env(self):
"""Verifies environment variables are read if a Colab secret read fails."""
mock_colab = sys.modules["google.colab"]
mock_colab.userdata.SecretNotFoundError = FakeSecretNotFoundError
mock_colab.userdata.NotebookAccessError = FakeNotebookAccessError
mock_colab.userdata.get.side_effect = FakeNotebookAccessError()

self.assertEqual(coreutils.get_environment_variable("key"), "environ-value")
self.assertEqual(coreutils.get_env_var("key"), "environ-value")

0 comments on commit 99de732

Please sign in to comment.