Voice et bot modif
This commit is contained in:
parent
189d56026b
commit
7333a22bcd
10774 changed files with 634644 additions and 933308 deletions
|
|
@ -31,46 +31,23 @@ else:
|
|||
colorama.init()
|
||||
|
||||
|
||||
def envwrap(prefix, types=None, is_method=False):
|
||||
def envwrap(name, app="", types=None, is_method=False):
|
||||
"""
|
||||
Override parameter defaults via `os.environ[prefix + param_name]`.
|
||||
Maps UPPER_CASE env vars map to lower_case param names.
|
||||
camelCase isn't supported (because Windows ignores case).
|
||||
|
||||
Precedence (highest first):
|
||||
|
||||
- call (`foo(a=3)`)
|
||||
- environ (`FOO_A=2`)
|
||||
- signature (`def foo(a=1)`)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
prefix : str
|
||||
Env var prefix, e.g. "FOO_"
|
||||
types : dict, optional
|
||||
Fallback mappings `{'param_name': type, ...}` if types cannot be
|
||||
inferred from function signature.
|
||||
Consider using `types=collections.defaultdict(lambda: ast.literal_eval)`.
|
||||
is_method : bool, optional
|
||||
Whether to use `functools.partialmethod`. If (default: False) use `functools.partial`.
|
||||
|
||||
Examples
|
||||
--------
|
||||
```
|
||||
$ cat foo.py
|
||||
from tqdm.utils import envwrap
|
||||
@envwrap("FOO_")
|
||||
def test(a=1, b=2, c=3):
|
||||
print(f"received: a={a}, b={b}, c={c}")
|
||||
|
||||
$ FOO_A=42 FOO_C=1337 python -c 'import foo; foo.test(c=99)'
|
||||
received: a=42, b=2, c=99
|
||||
```
|
||||
Basic (env-only) version of [envwrap](https://github.com/tqdm/envwrap).
|
||||
Install `envwrap` for config file support.
|
||||
"""
|
||||
if types is None:
|
||||
types = {}
|
||||
i = len(prefix)
|
||||
env_overrides = {k[i:].lower(): v for k, v in os.environ.items() if k.startswith(prefix)}
|
||||
if name[-1] == "_":
|
||||
name = name[:-1]
|
||||
warn("Trailing underscore in `name` is automatic", DeprecationWarning, stacklevel=2)
|
||||
prefixes = (name, f"{name}_{app}") if app else (name,)
|
||||
env_overrides = {}
|
||||
for prefix in prefixes:
|
||||
prefix = prefix.upper() + "_"
|
||||
i = len(prefix)
|
||||
env_overrides.update(
|
||||
(k[i:].lower(), v) for k, v in os.environ.items() if k.startswith(prefix))
|
||||
part = partialmethod if is_method else partial
|
||||
|
||||
def wrap(func):
|
||||
|
|
@ -99,7 +76,13 @@ def envwrap(prefix, types=None, is_method=False):
|
|||
return wrap
|
||||
|
||||
|
||||
class FormatReplace(object):
|
||||
try:
|
||||
from envwrap import envwrap # noqa: F401, F811
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
class FormatReplace:
|
||||
"""
|
||||
>>> a = FormatReplace('something')
|
||||
>>> f"{a:5d}"
|
||||
|
|
@ -114,7 +97,7 @@ class FormatReplace(object):
|
|||
return self.replace
|
||||
|
||||
|
||||
class Comparable(object):
|
||||
class Comparable:
|
||||
"""Assumes child has self._comparable attr/@property"""
|
||||
def __lt__(self, other):
|
||||
return self._comparable < other._comparable
|
||||
|
|
@ -135,7 +118,7 @@ class Comparable(object):
|
|||
return not self < other
|
||||
|
||||
|
||||
class ObjectWrapper(object):
|
||||
class ObjectWrapper:
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._wrapped, name)
|
||||
|
||||
|
|
@ -210,7 +193,7 @@ class DisableOnWriteError(ObjectWrapper):
|
|||
pass
|
||||
return inner
|
||||
|
||||
def __init__(self, wrapped, tqdm_instance):
|
||||
def __init__(self, wrapped, tqdm_instance): # noqa: B042
|
||||
super().__init__(wrapped)
|
||||
if hasattr(wrapped, 'write'):
|
||||
self.wrapper_setattr(
|
||||
|
|
@ -251,7 +234,7 @@ class CallbackIOWrapper(ObjectWrapper):
|
|||
|
||||
def _is_utf(encoding):
|
||||
try:
|
||||
u'\u2588\u2589'.encode(encoding)
|
||||
'\u2588\u2589'.encode(encoding)
|
||||
except UnicodeEncodeError:
|
||||
return False
|
||||
except Exception:
|
||||
|
|
@ -284,69 +267,16 @@ def _screen_shape_wrapper(): # pragma: no cover
|
|||
Return a function which returns console dimensions (width, height).
|
||||
Supported: linux, osx, windows, cygwin.
|
||||
"""
|
||||
_screen_shape = None
|
||||
if IS_WIN:
|
||||
_screen_shape = _screen_shape_windows
|
||||
if _screen_shape is None:
|
||||
_screen_shape = _screen_shape_tput
|
||||
if IS_NIX:
|
||||
_screen_shape = _screen_shape_linux
|
||||
return _screen_shape
|
||||
from os import get_terminal_size
|
||||
|
||||
|
||||
def _screen_shape_windows(fp): # pragma: no cover
|
||||
try:
|
||||
import struct
|
||||
from ctypes import create_string_buffer, windll
|
||||
from sys import stdin, stdout
|
||||
|
||||
io_handle = -12 # assume stderr
|
||||
if fp == stdin:
|
||||
io_handle = -10
|
||||
elif fp == stdout:
|
||||
io_handle = -11
|
||||
|
||||
h = windll.kernel32.GetStdHandle(io_handle)
|
||||
csbi = create_string_buffer(22)
|
||||
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
|
||||
if res:
|
||||
(_bufx, _bufy, _curx, _cury, _wattr, left, top, right, bottom,
|
||||
_maxx, _maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
|
||||
return right - left, bottom - top # +1
|
||||
except Exception: # nosec
|
||||
pass
|
||||
return None, None
|
||||
|
||||
|
||||
def _screen_shape_tput(*_): # pragma: no cover
|
||||
"""cygwin xterm (windows)"""
|
||||
try:
|
||||
import shlex
|
||||
from subprocess import check_call # nosec
|
||||
return [int(check_call(shlex.split('tput ' + i))) - 1
|
||||
for i in ('cols', 'lines')]
|
||||
except Exception: # nosec
|
||||
pass
|
||||
return None, None
|
||||
|
||||
|
||||
def _screen_shape_linux(fp): # pragma: no cover
|
||||
|
||||
try:
|
||||
from array import array
|
||||
from fcntl import ioctl
|
||||
from termios import TIOCGWINSZ
|
||||
except ImportError:
|
||||
return None, None
|
||||
else:
|
||||
def inner(fp):
|
||||
try:
|
||||
rows, cols = array('h', ioctl(fp, TIOCGWINSZ, '\0' * 8))[:2]
|
||||
return cols, rows
|
||||
cols, lines = get_terminal_size(getattr(fp, 'fileno', lambda: None)())
|
||||
return cols - 1, lines - 1
|
||||
except Exception:
|
||||
try:
|
||||
return [int(os.environ[i]) - 1 for i in ("COLUMNS", "LINES")]
|
||||
except (KeyError, ValueError):
|
||||
return None, None
|
||||
return None, None
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
def _environ_cols_wrapper(): # pragma: no cover
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue