From 5dc84ea0cdce38a450c8915e51c58e3e07cf6342 Mon Sep 17 00:00:00 2001 From: danielfromearth Date: Fri, 10 May 2024 12:17:20 -0400 Subject: [PATCH] simplify conditionals --- earthaccess/auth.py | 8 ++++---- earthaccess/search.py | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/earthaccess/auth.py b/earthaccess/auth.py index 4f2994f9..b8995f9d 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -277,11 +277,11 @@ def _netrc(self) -> bool: raise FileNotFoundError(f"No .netrc found in {Path.home()}") from err except NetrcParseError as err: raise NetrcParseError("Unable to parse .netrc") from err - if (creds := my_netrc[self.system.edl_hostname]) is not None: - username = creds["login"] - password = creds["password"] - else: + if (creds := my_netrc[self.system.edl_hostname]) is None: return False + + username = creds["login"] + password = creds["password"] authenticated = self._get_credentials(username, password) if authenticated: logger.debug("Using .netrc file for EDL") diff --git a/earthaccess/search.py b/earthaccess/search.py index 7e34a17b..9f73af14 100644 --- a/earthaccess/search.py +++ b/earthaccess/search.py @@ -95,13 +95,15 @@ def __init__(self, auth: Optional[Auth] = None, *args: Any, **kwargs: Any) -> No """ super().__init__(*args, **kwargs) - if auth and auth.authenticated: + self.session = ( # To search, we need the new bearer tokens from NASA Earthdata - self.session = auth.get_session(bearer_token=True) + auth.get_session(bearer_token=True) + if auth and auth.authenticated + else requests.session() + ) + if auth: self.mode(auth.system.cmr_base_url) - else: - self.session = requests.sessions.Session() self._debug = False @@ -452,13 +454,15 @@ class DataGranules(GranuleQuery): def __init__(self, auth: Optional[Auth] = None, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) - if auth and auth.authenticated: + self.session = ( # To search, we need the new bearer tokens from NASA Earthdata - self.session = auth.get_session(bearer_token=True) + auth.get_session(bearer_token=True) + if auth and auth.authenticated + else requests.session() + ) + if auth: self.mode(auth.system.cmr_base_url) - else: - self.session = requests.sessions.Session() self._debug = False