Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfromearth committed Feb 15, 2024
1 parent e918577 commit 2365c5c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 30 deletions.
52 changes: 37 additions & 15 deletions earthaccess/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ def search_datasets(
)
return []
if earthaccess.__auth__.authenticated:
query = DataCollections(auth=earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs)
query = DataCollections(
auth=earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment,
).parameters(**kwargs)
else:
query = DataCollections(
earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs)
earthdata_environment=earthaccess.__auth__.earthdata_environment
).parameters(**kwargs)
datasets_found = query.hits()
print(f"Datasets found: {datasets_found}")
if count > 0:
Expand Down Expand Up @@ -116,19 +119,24 @@ def search_data(
```
"""
if earthaccess.__auth__.authenticated:
query = DataGranules(earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs)
query = DataGranules(
earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment,
).parameters(**kwargs)
else:
query = DataGranules(
earthdata_environment=earthaccess.__auth__.earthdata_environment).parameters(**kwargs)
earthdata_environment=earthaccess.__auth__.earthdata_environment
).parameters(**kwargs)
granules_found = query.hits()
print(f"Granules found: {granules_found}")
if count > 0:
return query.get(count)
return query.get_all()


def login(strategy: str = "all", persist: bool = False, earthdata_environment=Env.PROD) -> Auth:
def login(
strategy: str = "all", persist: bool = False, earthdata_environment=Env.PROD
) -> Auth:
"""Authenticate with Earthdata login (https://urs.earthdata.nasa.gov/).
Parameters:
Expand All @@ -148,15 +156,23 @@ def login(strategy: str = "all", persist: bool = False, earthdata_environment=En
if strategy == "all":
for strategy in ["environment", "netrc", "interactive"]:
try:
earthaccess.__auth__.login(strategy=strategy, persist=persist, earthdata_environment=earthdata_environment)
earthaccess.__auth__.login(
strategy=strategy,
persist=persist,
earthdata_environment=earthdata_environment,
)
except Exception:
pass

if earthaccess.__auth__.authenticated:
earthaccess.__store__ = Store(earthaccess.__auth__)
break
else:
earthaccess.__auth__.login(strategy=strategy, persist=persist, earthdata_environment=earthdata_environment)
earthaccess.__auth__.login(
strategy=strategy,
persist=persist,
earthdata_environment=earthdata_environment,
)
if earthaccess.__auth__.authenticated:
earthaccess.__store__ = Store(earthaccess.__auth__)

Expand Down Expand Up @@ -251,11 +267,14 @@ def collection_query() -> Type[CollectionQuery]:
a query builder instance for data collections.
"""
if earthaccess.__auth__.authenticated:
query_builder = DataCollections(earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment)
query_builder = DataCollections(
earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment,
)
else:
query_builder = DataCollections(
earthdata_environment=earthaccess.__auth__.earthdata_environment)
earthdata_environment=earthaccess.__auth__.earthdata_environment
)
return query_builder


Expand All @@ -266,11 +285,14 @@ def granule_query() -> Type[GranuleQuery]:
a query builder instance for data granules.
"""
if earthaccess.__auth__.authenticated:
query_builder = DataGranules(earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment)
query_builder = DataGranules(
earthaccess.__auth__,
earthdata_environment=earthaccess.__auth__.earthdata_environment,
)
else:
query_builder = DataGranules(
earthdata_environment=earthaccess.__auth__.earthdata_environment)
earthdata_environment=earthaccess.__auth__.earthdata_environment
)
return query_builder


Expand Down
47 changes: 36 additions & 11 deletions earthaccess/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class Env(Enum):
"""
Host URL options, for different Earthdata domains.
"""

PROD = "urs.earthdata.nasa.gov"
UAT = "uat.urs.earthdata.nasa.gov"
SIT = "sit.urs.earthdata.nasa.gov"


class SessionWithHeaderRedirection(requests.Session):
"""
Requests removes auth headers if the redirect happens outside the
Expand Down Expand Up @@ -82,8 +84,12 @@ def __init__(self) -> None:
self.tokens: List = []
self._set_earthdata_environment(Env.PROD)

def login(self, strategy: str = "netrc", persist: bool = False,
earthdata_environment: Optional[Env] = None) -> Any:
def login(
self,
strategy: str = "netrc",
persist: bool = False,
earthdata_environment: Optional[Env] = None,
) -> Any:
"""Authenticate with Earthdata login.
Parameters:
Expand Down Expand Up @@ -119,13 +125,21 @@ def _set_earthdata_environment(self, earthdata_environment: Env) -> None:
self.earthdata_environment = earthdata_environment

# Maybe all these predefined URLs should be in a constants.py file
self.EDL_GET_TOKENS_URL = f"https://{self.earthdata_environment.value}/api/users/tokens"
self.EDL_GET_TOKENS_URL = (
f"https://{self.earthdata_environment.value}/api/users/tokens"
)
self.EDL_GET_PROFILE = f"https://{self.earthdata_environment.value}/api/users/<USERNAME>?client_id=ntD0YGC_SM3Bjs-Tnxd7bg"
self.EDL_GENERATE_TOKENS_URL = f"https://{self.earthdata_environment.value}/api/users/token"
self.EDL_REVOKE_TOKEN = f"https://{self.earthdata_environment.value}/api/users/revoke_token"
self.EDL_GENERATE_TOKENS_URL = (
f"https://{self.earthdata_environment.value}/api/users/token"
)
self.EDL_REVOKE_TOKEN = (
f"https://{self.earthdata_environment.value}/api/users/revoke_token"
)

self._eula_url = f"https://{self.earthdata_environment.value}/users/earthaccess/unaccepted_eulas"
self._apps_url = f"https://{self.earthdata_environment.value}/application_search"
self._apps_url = (
f"https://{self.earthdata_environment.value}/application_search"
)

def refresh_tokens(self) -> bool:
"""Refresh CMR tokens.
Expand Down Expand Up @@ -196,7 +210,9 @@ def get_s3_credentials(
A Python dictionary with the temporary AWS S3 credentials.
"""
if self.authenticated:
session = SessionWithHeaderRedirection(self.username, self.password, self.earthdata_environment)
session = SessionWithHeaderRedirection(
self.username, self.password, self.earthdata_environment
)
if endpoint is None:
auth_url = self._get_cloud_auth_url(
daac_shortname=daac, provider=provider
Expand Down Expand Up @@ -346,7 +362,9 @@ def _get_credentials(
return self.authenticated

def _get_user_tokens(self, username: str, password: str) -> Any:
session = SessionWithHeaderRedirection(username, password, self.earthdata_environment)
session = SessionWithHeaderRedirection(
username, password, self.earthdata_environment
)
auth_resp = session.get(
self.EDL_GET_TOKENS_URL,
headers={
Expand All @@ -357,7 +375,9 @@ def _get_user_tokens(self, username: str, password: str) -> Any:
return auth_resp

def _generate_user_token(self, username: str, password: str) -> Any:
session = SessionWithHeaderRedirection(username, password, self.earthdata_environment)
session = SessionWithHeaderRedirection(
username, password, self.earthdata_environment
)
auth_resp = session.post(
self.EDL_GENERATE_TOKENS_URL,
headers={
Expand All @@ -369,7 +389,9 @@ def _generate_user_token(self, username: str, password: str) -> Any:

def _revoke_user_token(self, token: str) -> bool:
if self.authenticated:
session = SessionWithHeaderRedirection(self.username, self.password, self.earthdata_environment)
session = SessionWithHeaderRedirection(
self.username, self.password, self.earthdata_environment
)
auth_resp = session.post(
self.EDL_REVOKE_TOKEN,
params={"token": token},
Expand All @@ -392,7 +414,10 @@ def _persist_user_credentials(self, username: str, password: str) -> bool:
print(e)
return False
my_netrc = Netrc(str(netrc_path))
my_netrc[self.earthdata_environment.value] = {"login": username, "password": password}
my_netrc[self.earthdata_environment.value] = {
"login": username,
"password": password,
}
my_netrc.save()
return True

Expand Down
24 changes: 20 additions & 4 deletions earthaccess/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ class DataCollections(CollectionQuery):
"umm_json",
]

def __init__(self, auth: Optional[Auth] = None, earthdata_environment: Optional[Env] = None, *args: Any, **kwargs: Any) -> None:
def __init__(
self,
auth: Optional[Auth] = None,
earthdata_environment: Optional[Env] = None,
*args: Any,
**kwargs: Any,
) -> None:
"""Builds an instance of DataCollections to query CMR
Parameters:
Expand All @@ -53,7 +59,9 @@ def __init__(self, auth: Optional[Auth] = None, earthdata_environment: Optional[
self.mode(CMR_SIT)

print(f"[in DataCollections] Earthdata environment: {earthdata_environment}")
print(f"[in DataCollections] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}")
print(
f"[in DataCollections] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}"
)

if auth is not None and auth.authenticated:
# To search, we need the new bearer tokens from NASA Earthdata
Expand Down Expand Up @@ -378,7 +386,13 @@ class DataGranules(GranuleQuery):
"umm_json",
]

def __init__(self, auth: Any = None, earthdata_environment: Optional[Env] = None, *args: Any, **kwargs: Any) -> None:
def __init__(
self,
auth: Any = None,
earthdata_environment: Optional[Env] = None,
*args: Any,
**kwargs: Any,
) -> None:
"""Base class for Granule and Collection CMR queries."""
super().__init__(*args, **kwargs)
self.session = session()
Expand All @@ -393,7 +407,9 @@ def __init__(self, auth: Any = None, earthdata_environment: Optional[Env] = None
self.mode(CMR_SIT)

print(f"[in DataGranules] Earthdata environment: {earthdata_environment}")
print(f"[in DataGranules] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}")
print(
f"[in DataGranules] earthdata_environment == Env.PROD -----> {earthdata_environment == Env.PROD}"
)

if auth is not None and auth.authenticated:
# To search, we need the new bearer tokens from NASA Earthdata
Expand Down

0 comments on commit 2365c5c

Please sign in to comment.