Skip to content

Commit

Permalink
Fixed linting errors
Browse files Browse the repository at this point in the history
utils.py:14 renamed/moved pytimeparse import
fields.py:142 Fixed comparison
utils.py:257 fixed name to proper snake_case
utils.py:306-307 fixed naming for error variable
  • Loading branch information
scheibling committed May 20, 2023
1 parent b93627c commit 747611d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/sshkey_tools/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
long_to_bytes,
random_keyid,
random_serial,
str_to_timedelta,
str_to_time_delta,
)

NoneType = type(None)
Expand Down Expand Up @@ -139,7 +139,7 @@ def __validate_required__(self) -> Union[bool, Exception]:
"""
Validates if the field is set when required
"""
if self.DEFAULT == self.value is None:
if self.DEFAULT and self.value is None:
return _EX.InvalidFieldDataException(
f"{self.get_name()} is a required field"
)
Expand Down Expand Up @@ -471,7 +471,7 @@ def encode(cls, value: Union[datetime, int, str]) -> bytes:
if value == "forever":
return Integer64Field.encode(MAX_INT64 - 1)

value = int(datetime.now() + str_to_timedelta(value))
value = int(datetime.now() + str_to_time_delta(value))

if isinstance(value, datetime):
value = int(value.timestamp())
Expand Down
12 changes: 7 additions & 5 deletions src/sshkey_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import hashlib as hl
import sys
import datetime
import pytimeparse2

from base64 import b64encode
from random import randint
from secrets import randbits
from typing import Dict, List, Union
from uuid import uuid4

from pytimeparse2 import parse as time_parse


NoneType = type(None)

Expand Down Expand Up @@ -254,7 +256,7 @@ def join_dicts(*dicts) -> dict:
return return_dict


def str_to_timedelta(str_delta: str) -> datetime.timedelta:
def str_to_time_delta(str_delta: str) -> datetime.timedelta:
"""Uses the package pytimeparse2 by wroberts/onegreyonewhite
to convert a string into a timedelta object.
Examples:
Expand Down Expand Up @@ -299,7 +301,7 @@ def str_to_timedelta(str_delta: str) -> datetime.timedelta:
datetime.timedelta: The time delta object
"""
try:
parsed = pytimeparse2.parse(str_delta, as_timedelta=True, raise_exception=True)
parsed = time_parse(str_delta, as_timedelta=True, raise_exception=True)
return parsed
except Exception as e:
raise ValueError(f"Could not parse time delta string {str_delta} : {e}") from e
except Exception as ex:
raise ValueError(f"Could not parse time delta string {str_delta} : {ex}") from ex

0 comments on commit 747611d

Please sign in to comment.