Skip to content

Commit

Permalink
Add password function and safe decorator (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyc60 authored Jul 28, 2023
1 parent 60a4c14 commit 038f298
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sw-utils"
version = "0.3.17"
version = "0.3.18"
description = "StakeWise Python utils"
authors = ["StakeWise Labs <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
7 changes: 7 additions & 0 deletions sw_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
ValidatorStatus,
get_consensus_client,
)
from .decorators import (
custom_before_log,
retry_aiohttp_errors,
retry_ipfs_exception,
safe,
)
from .event_scanner import EventProcessor, EventScanner
from .execution import get_execution_client
from .ipfs import (
Expand All @@ -17,6 +23,7 @@
WebStorageClient,
)
from .middlewares import construct_async_sign_and_send_raw_middleware
from .password import generate_password
from .signing import (
DepositData,
DepositMessage,
Expand Down
26 changes: 26 additions & 0 deletions sw_utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import logging
import typing
from functools import wraps
from typing import Callable

import aiohttp
from tenacity import (
Expand All @@ -20,6 +22,30 @@
from tenacity import RetryCallState


def safe(func: Callable) -> Callable:
if asyncio.iscoroutinefunction(func):

@wraps(func)
async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except BaseException as e:
default_logger.exception(e)
return None

else:

@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseException as e:
default_logger.exception(e)
return None

return wrapper


def custom_before_log(logger, log_level):
def custom_log_it(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
Expand Down
22 changes: 22 additions & 0 deletions sw_utils/password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import secrets
import string

SPECIAL_CHARS = '!@#$%^&*()_'


def generate_password(length=20) -> str:
alphabet = string.ascii_letters + string.digits + SPECIAL_CHARS
lower_set = set(string.ascii_lowercase)
upper_set = set(string.ascii_uppercase)
digits_set = set(string.digits)
special_set = set(SPECIAL_CHARS)
while True:
password = [secrets.choice(alphabet) for _ in range(length)]
password_set = set(password)
if (
upper_set.intersection(password_set)
and lower_set.intersection(password_set)
and special_set.intersection(password_set)
and digits_set.intersection(password_set)
):
return ''.join(password)

0 comments on commit 038f298

Please sign in to comment.