Skip to content

Commit

Permalink
format all code files using black (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaS11 authored Dec 13, 2023
1 parent 84f76ab commit c7656c8
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 93 deletions.
1 change: 0 additions & 1 deletion icepyx/core/APIformatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class Parameters:
"""

def __init__(self, partype, values=None, reqtype=None):

assert partype in [
"CMR",
"required",
Expand Down
71 changes: 40 additions & 31 deletions icepyx/core/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

import earthaccess


class AuthenticationError(Exception):
'''
"""
Raised when an error is encountered while authenticating Earthdata credentials
'''
"""

pass


class EarthdataAuthMixin():
class EarthdataAuthMixin:
"""
This mixin class generates the needed authentication sessions and tokens, including for NASA Earthdata cloud access.
Authentication is completed using the [earthaccess library](https://nsidc.github.io/earthaccess/).
Expand All @@ -21,26 +23,27 @@ class EarthdataAuthMixin():
3. Storing credentials in a .netrc file (not recommended for security reasons)
More details on using these methods is available in the [earthaccess documentation](https://nsidc.github.io/earthaccess/tutorials/restricted-datasets/#auth).
This class can be inherited by any other class that requires authentication. For
example, the `Query` class inherits this one, and so a Query object has the
This class can be inherited by any other class that requires authentication. For
example, the `Query` class inherits this one, and so a Query object has the
`.session` property. The method `earthdata_login()` is included for backwards compatibility.
The class can be created without any initialization parameters, and the properties will
be populated when they are called. It can alternately be initialized with an
earthaccess.auth.Auth object, which will then be used to create a session or
be populated when they are called. It can alternately be initialized with an
earthaccess.auth.Auth object, which will then be used to create a session or
s3login_credentials as they are called.
Parameters
----------
auth : earthaccess.auth.Auth, default None
Optional parameter to initialize an object with existing credentials.
Examples
--------
>>> a = EarthdataAuthMixin()
>>> a.session # doctest: +SKIP
>>> a.s3login_credentials # doctest: +SKIP
"""

def __init__(self, auth=None):
self._auth = copy.deepcopy(auth)
# initializatin of session and s3 creds is not allowed because those are generated
Expand All @@ -58,58 +61,62 @@ def __str__(self):

@property
def auth(self):
'''
Authentication object returned from earthaccess.login() which stores user authentication.
'''
"""
Authentication object returned from earthaccess.login() which stores user authentication.
"""
# Only login the first time .auth is accessed
if self._auth is None:
auth = earthaccess.login()
# check for a valid auth response
if auth.authenticated is False:
raise AuthenticationError('Earthdata authentication failed. Check output for error message')
raise AuthenticationError(
"Earthdata authentication failed. Check output for error message"
)
else:
self._auth = auth

return self._auth

@property
def session(self):
'''
"""
Earthaccess session object for connecting to Earthdata resources.
'''
"""
# Only generate a session the first time .session is accessed
if self._session is None:
self._session = self.auth.get_session()
return self._session

@property
def s3login_credentials(self):
'''
"""
A dictionary which stores login credentials for AWS s3 access. This property is accessed
if using AWS cloud data.
Because s3 tokens are only good for one hour, this function will automatically check if an
hour has elapsed since the last token use and generate a new token if necessary.
'''
"""

def set_s3_creds():
''' Store s3login creds from `auth`and reset the last updated timestamp'''
"""Store s3login creds from `auth`and reset the last updated timestamp"""
self._s3login_credentials = self.auth.get_s3_credentials(daac="NSIDC")
self._s3_initial_ts = datetime.datetime.now()

# Only generate s3login_credentials the first time credentials are accessed, or if an hour
# has passed since the last login
# has passed since the last login
if self._s3login_credentials is None:
set_s3_creds()
elif (datetime.datetime.now() - self._s3_initial_ts) >= datetime.timedelta(hours=1):
elif (datetime.datetime.now() - self._s3_initial_ts) >= datetime.timedelta(
hours=1
):
set_s3_creds()
return self._s3login_credentials

def earthdata_login(self, uid=None, email=None, s3token=None, **kwargs) -> None:
"""
Authenticate with NASA Earthdata to enable data ordering and download.
Credential storage details are described in the EathdataAuthMixin class section.
**Note:** This method is maintained for backward compatibility. It is no longer required to explicitly run `.earthdata_login()`. Authentication will be performed by the module as needed when `.session` or `.s3login_credentials` are accessed.
Parameters
Expand All @@ -134,12 +141,14 @@ def earthdata_login(self, uid=None, email=None, s3token=None, **kwargs) -> None:
"""
warnings.warn(
"It is no longer required to explicitly run the `.earthdata_login()` method. Authentication will be performed by the module as needed.",
DeprecationWarning, stacklevel=2
)

"It is no longer required to explicitly run the `.earthdata_login()` method. Authentication will be performed by the module as needed.",
DeprecationWarning,
stacklevel=2,
)

if uid != None or email != None or s3token != None:
warnings.warn(
"The user id (uid) and/or email keyword arguments are no longer required.",
DeprecationWarning, stacklevel=2
DeprecationWarning,
stacklevel=2,
)
3 changes: 1 addition & 2 deletions icepyx/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class DeprecationError(Exception):
"""
Class raised for use of functionality that is no longer supported by icepyx.
"""

pass


Expand All @@ -27,5 +28,3 @@ def __init__(

def __str__(self):
return f"{self.msgtxt}: {self.errmsg}"


5 changes: 3 additions & 2 deletions icepyx/core/icesat2data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@


class Icesat2Data:
def __init__(self,):

def __init__(
self,
):
warnings.filterwarnings("always")
warnings.warn(
"DEPRECATED. Please use icepyx.Query to create a download data object (all other functionality is the same)",
Expand Down
7 changes: 3 additions & 4 deletions icepyx/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ class Query(GenQuery, EarthdataAuthMixin):
files : string, default None
A placeholder for future development. Not used for any purposes yet.
auth : earthaccess.auth.Auth, default None
An earthaccess authentication object. Available as an argument so an existing
earthaccess.auth.Auth object can be used for authentication. If not given, a new auth
object will be created whenever authentication is needed.
An earthaccess authentication object. Available as an argument so an existing
earthaccess.auth.Auth object can be used for authentication. If not given, a new auth
object will be created whenever authentication is needed.
Returns
-------
Expand Down Expand Up @@ -411,7 +411,6 @@ def __init__(
auth=None,
**kwargs,
):

# Check necessary combination of input has been specified
if (
(product is None or spatial_extent is None)
Expand Down
3 changes: 0 additions & 3 deletions icepyx/core/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def geodataframe(extent_type, spatial_extent, file=False, xdateline=None):
# DevGoal: the crs setting and management needs to be improved

elif extent_type == "polygon" and file == False:

# if spatial_extent is already a Polygon
if isinstance(spatial_extent, Polygon):
spatial_extent_geom = spatial_extent
Expand Down Expand Up @@ -248,7 +247,6 @@ def validate_polygon_pairs(spatial_extent):
if (spatial_extent[0][0] != spatial_extent[-1][0]) or (
spatial_extent[0][1] != spatial_extent[-1][1]
):

# Throw a warning
warnings.warn(
"WARNING: Polygon's first and last point's coordinates differ,"
Expand Down Expand Up @@ -436,7 +434,6 @@ def __init__(self, spatial_extent, **kwarg):

# Check if spatial_extent is a list of coordinates (bounding box or polygon)
if isinstance(spatial_extent, (list, np.ndarray)):

# bounding box
if len(spatial_extent) == 4 and all(
isinstance(i, scalar_types) for i in spatial_extent
Expand Down
8 changes: 0 additions & 8 deletions icepyx/core/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def convert_string_to_date(date):


def check_valid_date_range(start, end):

"""
Helper function for checking if a date range is valid.
Expand Down Expand Up @@ -89,7 +88,6 @@ def check_valid_date_range(start, end):


def validate_times(start_time, end_time):

"""
Validates the start and end times passed into __init__ and returns them as datetime.time objects.
Expand Down Expand Up @@ -145,7 +143,6 @@ def validate_times(start_time, end_time):


def validate_date_range_datestr(date_range, start_time=None, end_time=None):

"""
Validates a date range provided in the form of a list of strings.
Expand Down Expand Up @@ -190,7 +187,6 @@ def validate_date_range_datestr(date_range, start_time=None, end_time=None):


def validate_date_range_datetime(date_range, start_time=None, end_time=None):

"""
Validates a date range provided in the form of a list of datetimes.
Expand Down Expand Up @@ -230,7 +226,6 @@ def validate_date_range_datetime(date_range, start_time=None, end_time=None):


def validate_date_range_date(date_range, start_time=None, end_time=None):

"""
Validates a date range provided in the form of a list of datetime.date objects.
Expand Down Expand Up @@ -268,7 +263,6 @@ def validate_date_range_date(date_range, start_time=None, end_time=None):


def validate_date_range_dict(date_range, start_time=None, end_time=None):

"""
Validates a date range provided in the form of a dict with the following keys:
Expand Down Expand Up @@ -330,7 +324,6 @@ def validate_date_range_dict(date_range, start_time=None, end_time=None):

# if is string date
elif isinstance(_start_date, str):

_start_date = convert_string_to_date(_start_date)
_start_date = dt.datetime.combine(_start_date, start_time)

Expand Down Expand Up @@ -411,7 +404,6 @@ def __init__(self, date_range, start_time=None, end_time=None):
"""

if len(date_range) == 2:

# date range is provided as dict of strings, dates, or datetimes
if isinstance(date_range, dict):
self._start, self._end = validate_date_range_dict(
Expand Down
10 changes: 6 additions & 4 deletions icepyx/core/validate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ def tracks(track):

return track_list


def check_s3bucket(path):
"""
Check if the given path is an s3 path. Raise a warning if the data being referenced is not
in the NSIDC bucket
"""
split_path = path.split('/')
if split_path[0] == 's3:' and split_path[2] != 'nsidc-cumulus-prod-protected':
split_path = path.split("/")
if split_path[0] == "s3:" and split_path[2] != "nsidc-cumulus-prod-protected":
warnings.warn(
's3 data being read from outside the NSIDC data bucket. Icepyx can '
'read this data, but available data lists may not be accurate.', stacklevel=2
"s3 data being read from outside the NSIDC data bucket. Icepyx can "
"read this data, but available data lists may not be accurate.",
stacklevel=2,
)
return path
Loading

0 comments on commit c7656c8

Please sign in to comment.