updating ba, bastd

This commit is contained in:
Ayush Saini 2022-06-09 01:26:46 +05:30
parent c2ddced5cf
commit 3c43b5661b
169 changed files with 13214 additions and 4232 deletions

View file

@ -4,9 +4,10 @@
from __future__ import annotations
import datetime
import os
import time
import weakref
import datetime
import functools
from enum import Enum
from typing import TYPE_CHECKING, cast, TypeVar, Generic
@ -23,7 +24,7 @@ except ModuleNotFoundError:
if TYPE_CHECKING:
import asyncio
from efro.call import Call as Call # 'as Call' so we re-export.
from typing import Any, Callable, Optional, NoReturn
from typing import Any, Callable, NoReturn
T = TypeVar('T')
TVAL = TypeVar('TVAL')
@ -176,15 +177,15 @@ class DirtyBit:
retry_interval: float = 5.0,
use_lock: bool = False,
auto_dirty_seconds: float = None,
min_update_interval: Optional[float] = None):
min_update_interval: float | None = None):
curtime = time.time()
self._retry_interval = retry_interval
self._auto_dirty_seconds = auto_dirty_seconds
self._min_update_interval = min_update_interval
self._dirty = dirty
self._next_update_time: Optional[float] = (curtime if dirty else None)
self._last_update_time: Optional[float] = None
self._next_auto_dirty_time: Optional[float] = (
self._next_update_time: float | None = (curtime if dirty else None)
self._last_update_time: float | None = None
self._next_auto_dirty_time: float | None = (
(curtime + self._auto_dirty_seconds) if
(not dirty and self._auto_dirty_seconds is not None) else None)
self._use_lock = use_lock
@ -475,7 +476,7 @@ def asserttype(obj: Any, typ: type[T]) -> T:
return obj
def asserttype_o(obj: Any, typ: type[T]) -> Optional[T]:
def asserttype_o(obj: Any, typ: type[T]) -> T | None:
"""Return an object typed as a given optional type.
Assert is used to check its actual type, so only use this when
@ -498,7 +499,7 @@ def checktype(obj: Any, typ: type[T]) -> T:
return obj
def checktype_o(obj: Any, typ: type[T]) -> Optional[T]:
def checktype_o(obj: Any, typ: type[T]) -> T | None:
"""Return an object typed as a given optional type.
Always checks the type at runtime with isinstance and throws a TypeError
@ -523,7 +524,7 @@ def warntype(obj: Any, typ: type[T]) -> T:
return obj # type: ignore
def warntype_o(obj: Any, typ: type[T]) -> Optional[T]:
def warntype_o(obj: Any, typ: type[T]) -> T | None:
"""Return an object typed as a given type.
Always checks the type at runtime and simply logs a warning if it is
@ -537,7 +538,7 @@ def warntype_o(obj: Any, typ: type[T]) -> Optional[T]:
return obj # type: ignore
def assert_non_optional(obj: Optional[T]) -> T:
def assert_non_optional(obj: T | None) -> T:
"""Return an object with Optional typing removed.
Assert is used to check its actual type, so only use this when
@ -547,7 +548,7 @@ def assert_non_optional(obj: Optional[T]) -> T:
return obj
def check_non_optional(obj: Optional[T]) -> T:
def check_non_optional(obj: T | None) -> T:
"""Return an object with Optional typing removed.
Always checks the actual type and throws a TypeError on failure.
@ -652,7 +653,6 @@ def unchanging_hostname() -> str:
network conditions. (A Mac will tend to go from Foo to Foo.local,
Foo.lan etc. throughout its various adventures)
"""
import os
import platform
import subprocess
@ -663,3 +663,28 @@ def unchanging_hostname() -> str:
check=True,
capture_output=True).stdout.decode().strip().replace(' ', '-')
return os.uname().nodename
def set_canonical_module(module_globals: dict[str, Any],
names: list[str]) -> None:
"""Override any __module__ attrs on passed classes/etc.
This allows classes to present themselves using clean paths such as
mymodule.MyClass instead of possibly ugly internal ones such as
mymodule._internal._stuff.MyClass.
"""
modulename = module_globals.get('__name__')
if not isinstance(modulename, str):
raise RuntimeError('Unable to get module name.')
for name in names:
obj = module_globals[name]
existing = getattr(obj, '__module__', None)
try:
if existing is not None and existing != modulename:
obj.__module__ = modulename
except Exception:
import logging
logging.warning(
'set_canonical_module: unable to change __module__'
" from '%s' to '%s' on %s object at '%s'.", existing,
modulename, type(obj), name)