Skip to content

Commit

Permalink
minor type hint corrections in aws_utils.py and utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenzie-grimes-noaa committed Jul 28, 2023
1 parent a710136 commit 5cf40c7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
23 changes: 12 additions & 11 deletions python/idsse_common/idsse/common/aws_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import os
from datetime import datetime, timedelta, timezone
from typing import Sequence, Tuple
from typing import List, Tuple, Optional, Sequence

from .path_builder import PathBuilder
from .utils import TimeDelta, datetime_gen, exec_cmd
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_path(self, issue: datetime, valid: datetime) -> str:
lead = TimeDelta(valid-issue)
return self.path_builder.build_path(issue=issue, valid=valid, lead=lead)

def aws_ls(self, path: str, prepend_path: bool = True) -> Sequence[str]:
def aws_ls(self, path: str, prepend_path: bool = True) -> List[str]:
"""Execute an 'ls' on the AWS s3 bucket specified by path
Args:
Expand Down Expand Up @@ -85,13 +85,13 @@ def aws_cp(self, path: str, dest: str) -> bool:
commands = ['aws', 's3', '--no-sign-request', 'cp', path, dest]
exec_cmd(commands)
return True
except:
except Exception: # pytest: disable=broad-exception-caught
return False
finally:
pass


def check_for(self, issue: datetime, valid: datetime) -> Tuple[datetime, str]:

def check_for(self, issue: datetime, valid: datetime) -> Optional[Tuple[datetime, str]]:
"""Checks if an object passed issue/valid exists
Args:
Expand All @@ -112,9 +112,9 @@ def check_for(self, issue: datetime, valid: datetime) -> Tuple[datetime, str]:
return None

def get_issues(self,
num_issues: int = 1,
issue_start: datetime = None,
issue_end: datetime = datetime.now(timezone.utc)
num_issues: Optional[int] = 1,
issue_start: Optional[datetime] = None,
issue_end: datetime = datetime.utcnow()
) -> Sequence[datetime]:
"""Determine the available issue date/times
Expand Down Expand Up @@ -148,8 +148,8 @@ def get_issues(self,

def get_valids(self,
issue: datetime,
valid_start: datetime = None,
valid_end: datetime = None) -> Sequence[Tuple[datetime, str]]:
valid_start: Optional[datetime] = None,
valid_end: Optional[datetime] = None) -> Optional[Sequence[Tuple[datetime, str]]]:
"""Get all objects consistent with the passed issue date/time and filter by valid range
Args:
Expand All @@ -164,7 +164,8 @@ def get_valids(self,
object's location) and the object's location (path)
"""
if valid_start and valid_start == valid_end:
return [self.check_for(issue, valid_start)]
valids_and_filenames = self.check_for(issue, valid_start)
return [valids_and_filenames] if valids_and_filenames is not None else None

dir_path = self.path_builder.build_dir(issue=issue)

Expand Down
2 changes: 1 addition & 1 deletion python/idsse_common/idsse/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self,
filepaths = glob.glob(config, recursive=recursive)
if len(filepaths) == 0:
raise FileNotFoundError
elif len(filepaths) == 1:
if len(filepaths) == 1:
self._from_filepath(filepaths[0], keys)
else:
self._from_filepaths(filepaths, keys)
Expand Down
8 changes: 4 additions & 4 deletions python/idsse_common/idsse/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import logging
from datetime import datetime, timedelta
from subprocess import Popen, PIPE, TimeoutExpired
from typing import Sequence
from typing import Sequence, Optional, Generator, Any

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,7 +70,7 @@ def __delitem__(self, key):
del self.__dict__[key]


def exec_cmd(commands: Sequence[str], timeout: int = None) -> Sequence[str]:
def exec_cmd(commands: Sequence[str], timeout: Optional[int] = None) -> Sequence[str]:
"""Execute the passed commands via a Popen call
Args:
Expand Down Expand Up @@ -145,8 +145,8 @@ def dict_copy_with(old_dict: dict, **kwargs) -> dict:

def datetime_gen(dt_: datetime,
time_delta: timedelta,
end_dt: datetime = None,
max_num: int = 100) -> datetime:
end_dt: Optional[datetime] = None,
max_num: int = 100) -> Generator[datetime, Any, None]:
"""Create a date/time sequence generator, given a starting date/time and a time stride
Args:
Expand Down
6 changes: 3 additions & 3 deletions python/idsse_common/test/test_aws_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_aws_cp_retries_with_s3_command_line(aws_utils: AwsUtils, monkeypatch: M
mock_exec_cmd_failure)

copy_success = aws_utils.aws_cp('s3:/some/path', 's3:/new/path')
assert copy_success == True
assert copy_success
assert mock_exec_cmd_failure.call_count == 2


Expand All @@ -118,7 +118,7 @@ def test_aws_cp_fails(aws_utils: AwsUtils, monkeypatch: MonkeyPatch):
mock_exec_cmd_failure)

copy_success = aws_utils.aws_cp('s3:/some/path', 's3:/new/path')
assert copy_success == False
assert not copy_success
mock_exec_cmd_failure.call_count == 2


Expand Down Expand Up @@ -146,7 +146,7 @@ def test_get_issues(aws_utils: AwsUtils, mock_exec_cmd):
def test_get_issues_returns_latest_issue_from_today_if_no_args_passed(aws_utils: AwsUtils, mock_exec_cmd):
result = aws_utils.get_issues()
assert len(result) == 1
assert result[0].date() == datetime.now().date()
assert result[0].date() == datetime.utcnow().date()


def test_get_valids_all(aws_utils: AwsUtils, mock_exec_cmd):
Expand Down

0 comments on commit 5cf40c7

Please sign in to comment.