Add Black
This commit is contained in:
1 parent
0490167a12
commit
da05dfe708
16 files changed
+401
-272
No files matched your search
@@ -9,21 +9,29 @@ import random
|
||||
import string
|
||||
from functools import wraps
|
||||
from types import MappingProxyType
|
||||
from typing import (Any, Optional, TypeVar, Callable, KeysView, Union, # noqa
|
||||
Iterable, List, Dict, Iterator, Coroutine, MutableSet)
|
||||
from typing import (
|
||||
Any,
|
||||
Optional,
|
||||
TypeVar,
|
||||
Callable,
|
||||
KeysView,
|
||||
Union, # noqa
|
||||
Iterable,
|
||||
Coroutine,
|
||||
)
|
||||
|
||||
import slugify as unicode_slug
|
||||
|
||||
from .dt import as_local, utcnow
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
T = TypeVar('T')
|
||||
U = TypeVar('U')
|
||||
ENUM_T = TypeVar('ENUM_T', bound=enum.Enum)
|
||||
T = TypeVar("T")
|
||||
U = TypeVar("U")
|
||||
ENUM_T = TypeVar("ENUM_T", bound=enum.Enum)
|
||||
# pylint: enable=invalid-name
|
||||
|
||||
RE_SANITIZE_FILENAME = re.compile(r'(~|\.\.|/|\\)')
|
||||
RE_SANITIZE_PATH = re.compile(r'(~|\.(\.)+)')
|
||||
RE_SANITIZE_FILENAME = re.compile(r"(~|\.\.|/|\\)")
|
||||
RE_SANITIZE_PATH = re.compile(r"(~|\.(\.)+)")
|
||||
|
||||
|
||||
def sanitize_filename(filename: str) -> str:
|
||||
@@ -38,23 +46,24 @@ def sanitize_path(path: str) -> str:
|
||||
|
||||
def slugify(text: str) -> str:
|
||||
"""Slugify a given text."""
|
||||
return unicode_slug.slugify(text, separator='_') # type: ignore
|
||||
return unicode_slug.slugify(text, separator="_") # type: ignore
|
||||
|
||||
|
||||
def repr_helper(inp: Any) -> str:
|
||||
"""Help creating a more readable string representation of objects."""
|
||||
if isinstance(inp, (dict, MappingProxyType)):
|
||||
return ", ".join(
|
||||
repr_helper(key)+"="+repr_helper(item) for key, item
|
||||
in inp.items())
|
||||
repr_helper(key) + "=" + repr_helper(item) for key, item in inp.items()
|
||||
)
|
||||
if isinstance(inp, datetime):
|
||||
return as_local(inp).isoformat()
|
||||
|
||||
return str(inp)
|
||||
|
||||
|
||||
def convert(value: Optional[T], to_type: Callable[[T], U],
|
||||
default: Optional[U] = None) -> Optional[U]:
|
||||
def convert(
|
||||
value: Optional[T], to_type: Callable[[T], U], default: Optional[U] = None
|
||||
) -> Optional[U]:
|
||||
"""Convert value to to_type, returns default if fails."""
|
||||
try:
|
||||
return default if value is None else to_type(value)
|
||||
@@ -63,8 +72,9 @@ def convert(value: Optional[T], to_type: Callable[[T], U],
|
||||
return default
|
||||
|
||||
|
||||
def ensure_unique_string(preferred_string: str, current_strings:
|
||||
Union[Iterable[str], KeysView[str]]) -> str:
|
||||
def ensure_unique_string(
|
||||
preferred_string: str, current_strings: Union[Iterable[str], KeysView[str]]
|
||||
) -> str:
|
||||
"""Return a string that is not present in current_strings.
|
||||
|
||||
If preferred string exists will append _2, _3, ..
|
||||
@@ -88,14 +98,14 @@ def get_local_ip() -> str:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
# Use Google Public DNS server to determine own IP
|
||||
sock.connect(('8.8.8.8', 80))
|
||||
sock.connect(("8.8.8.8", 80))
|
||||
|
||||
return sock.getsockname()[0] # type: ignore
|
||||
except socket.error:
|
||||
try:
|
||||
return socket.gethostbyname(socket.gethostname())
|
||||
except socket.gaierror:
|
||||
return '127.0.0.1'
|
||||
return "127.0.0.1"
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
@@ -106,7 +116,7 @@ def get_random_string(length: int = 10) -> str:
|
||||
generator = random.SystemRandom()
|
||||
source_chars = string.ascii_letters + string.digits
|
||||
|
||||
return ''.join(generator.choice(source_chars) for _ in range(length))
|
||||
return "".join(generator.choice(source_chars) for _ in range(length))
|
||||
|
||||
|
||||
class OrderedEnum(enum.Enum):
|
||||
@@ -158,8 +168,9 @@ class Throttle:
|
||||
Adds a datetime attribute `last_call` to the method.
|
||||
"""
|
||||
|
||||
def __init__(self, min_time: timedelta,
|
||||
limit_no_throttle: Optional[timedelta] = None) -> None:
|
||||
def __init__(
|
||||
self, min_time: timedelta, limit_no_throttle: Optional[timedelta] = None
|
||||
) -> None:
|
||||
"""Initialize the throttle."""
|
||||
self.min_time = min_time
|
||||
self.limit_no_throttle = limit_no_throttle
|
||||
@@ -168,10 +179,13 @@ class Throttle:
|
||||
"""Caller for the throttle."""
|
||||
# Make sure we return a coroutine if the method is async.
|
||||
if asyncio.iscoroutinefunction(method):
|
||||
|
||||
async def throttled_value() -> None:
|
||||
"""Stand-in function for when real func is being throttled."""
|
||||
return None
|
||||
|
||||
else:
|
||||
|
||||
def throttled_value() -> None: # type: ignore
|
||||
"""Stand-in function for when real func is being throttled."""
|
||||
return None
|
||||
@@ -189,8 +203,10 @@ class Throttle:
|
||||
# All methods have the classname in their qualname separated by a '.'
|
||||
# Functions have a '.' in their qualname if defined inline, but will
|
||||
# be prefixed by '.<locals>.' so we strip that out.
|
||||
is_func = (not hasattr(method, '__self__') and
|
||||
'.' not in method.__qualname__.split('.<locals>.')[-1])
|
||||
is_func = (
|
||||
not hasattr(method, "__self__")
|
||||
and "." not in method.__qualname__.split(".<locals>.")[-1]
|
||||
)
|
||||
|
||||
@wraps(method)
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Union[Callable, Coroutine]:
|
||||
@@ -199,14 +215,14 @@ class Throttle:
|
||||
If we cannot acquire the lock, it is running so return None.
|
||||
"""
|
||||
# pylint: disable=protected-access
|
||||
if hasattr(method, '__self__'):
|
||||
host = getattr(method, '__self__')
|
||||
if hasattr(method, "__self__"):
|
||||
host = getattr(method, "__self__")
|
||||
elif is_func:
|
||||
host = wrapper
|
||||
else:
|
||||
host = args[0] if args else wrapper
|
||||
|
||||
if not hasattr(host, '_throttle'):
|
||||
if not hasattr(host, "_throttle"):
|
||||
host._throttle = {}
|
||||
|
||||
if id(self) not in host._throttle:
|
||||
@@ -217,7 +233,7 @@ class Throttle:
|
||||
return throttled_value()
|
||||
|
||||
# Check if method is never called or no_throttle is given
|
||||
force = kwargs.pop('no_throttle', False) or not throttle[1]
|
||||
force = kwargs.pop("no_throttle", False) or not throttle[1]
|
||||
|
||||
try:
|
||||
if force or utcnow() - throttle[1] > self.min_time:
|
||||
|
||||
Reference in new issue
Block a user