2019-11-15 13:59:09 +00:00
|
|
|
import contextlib, decimal, enum, io, ipaddress, multiprocessing
|
2019-09-17 12:39:23 +00:00
|
|
|
import queue, re, signal, threading, typing
|
2019-11-15 13:59:09 +00:00
|
|
|
from . import cli, consts, datetime, decorators, irc, http, parse, security
|
2018-10-03 12:22:37 +00:00
|
|
|
|
2019-11-15 13:41:03 +00:00
|
|
|
from .decorators import export, hook, kwarg
|
2019-11-15 13:39:24 +00:00
|
|
|
from .settings import (BoolSetting, FunctionSetting, IntRangeSetting,
|
|
|
|
IntSetting, OptionsSetting, sensitive_format, SensitiveSetting, Setting)
|
|
|
|
|
2019-02-22 11:23:36 +00:00
|
|
|
class Direction(enum.Enum):
|
2019-03-10 13:14:15 +00:00
|
|
|
Send = 0
|
|
|
|
Recv = 1
|
2019-02-22 11:23:36 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def prevent_highlight(nickname: str) -> str:
|
2018-10-03 12:22:37 +00:00
|
|
|
return nickname[0]+"\u200c"+nickname[1:]
|
|
|
|
|
2018-10-16 13:47:01 +00:00
|
|
|
class EventError(Exception):
|
|
|
|
pass
|
2018-10-20 19:51:29 +00:00
|
|
|
class EventsResultsError(EventError):
|
|
|
|
def __init__(self):
|
|
|
|
EventError.__init__(self, "Failed to load results")
|
2018-10-30 14:58:48 +00:00
|
|
|
class EventsNotEnoughArgsError(EventError):
|
|
|
|
def __init__(self, n):
|
|
|
|
EventError.__init__(self, "Not enough arguments (minimum %d)" % n)
|
|
|
|
class EventsUsageError(EventError):
|
|
|
|
def __init__(self, usage):
|
|
|
|
EventError.__init__(self, "Not enough arguments, usage: %s" % usage)
|
|
|
|
|
2019-06-14 11:01:55 +00:00
|
|
|
class MultiCheck(object):
|
|
|
|
def __init__(self,
|
|
|
|
requests: typing.List[typing.Tuple[str, typing.List[str]]]):
|
2019-06-15 17:42:14 +00:00
|
|
|
self._requests = requests
|
|
|
|
def to_multi(self):
|
|
|
|
return self
|
|
|
|
def requests(self):
|
|
|
|
return self._requests[:]
|
2019-06-16 19:48:31 +00:00
|
|
|
def __or__(self, other: "Check"):
|
|
|
|
return MultiCheck(self._requests+[(other.request, other.args)])
|
2019-06-14 10:42:12 +00:00
|
|
|
class Check(object):
|
2019-06-14 11:12:38 +00:00
|
|
|
def __init__(self, request: str, *args: str):
|
2019-06-14 10:42:12 +00:00
|
|
|
self.request = request
|
2019-06-14 11:12:38 +00:00
|
|
|
self.args = list(args)
|
2019-06-14 11:01:55 +00:00
|
|
|
def to_multi(self):
|
|
|
|
return MultiCheck([(self.request, self.args)])
|
|
|
|
def __or__(self, other: "Check"):
|
|
|
|
return MultiCheck([(self.request, self.args),
|
|
|
|
(other.request, other.args)])
|
2019-06-14 10:42:12 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
TOP_10_CALLABLE = typing.Callable[[typing.Any], typing.Any]
|
2018-10-30 17:49:35 +00:00
|
|
|
def top_10(items: typing.Dict[typing.Any, typing.Any],
|
2018-10-30 14:58:48 +00:00
|
|
|
convert_key: TOP_10_CALLABLE=lambda x: x,
|
|
|
|
value_format: TOP_10_CALLABLE=lambda x: x):
|
|
|
|
top_10 = sorted(items.keys())
|
|
|
|
top_10 = sorted(top_10, key=items.get, reverse=True)[:10]
|
|
|
|
|
|
|
|
top_10_items = []
|
|
|
|
for key in top_10:
|
|
|
|
top_10_items.append("%s (%s)" % (convert_key(key),
|
|
|
|
value_format(items[key])))
|
|
|
|
|
|
|
|
return top_10_items
|
2018-12-08 09:00:12 +00:00
|
|
|
|
|
|
|
class CaseInsensitiveDict(dict):
|
2018-12-08 09:13:01 +00:00
|
|
|
def __init__(self, other: typing.Dict[str, typing.Any]):
|
|
|
|
dict.__init__(self, ((k.lower(), v) for k, v in other.items()))
|
|
|
|
def __getitem__(self, key: str) -> typing.Any:
|
2018-12-08 09:00:12 +00:00
|
|
|
return dict.__getitem__(self, key.lower())
|
2018-12-08 09:13:01 +00:00
|
|
|
def __setitem__(self, key: str, value: typing.Any) -> typing.Any:
|
|
|
|
return dict.__setitem__(self, key.lower(), value)
|
2019-10-25 22:32:00 +00:00
|
|
|
def __contains__(self, key: typing.Any) -> bool:
|
|
|
|
if isinstance(key, str):
|
|
|
|
return dict.__contains__(self, key.lower())
|
|
|
|
else:
|
2019-10-31 13:06:26 +00:00
|
|
|
raise TypeError("Expected string, not %r" % key)
|
2019-09-17 12:40:37 +00:00
|
|
|
def get(self, key: str, default: typing.Any=None):
|
|
|
|
return dict.get(self, key.lower(), default)
|
2019-02-06 18:11:19 +00:00
|
|
|
|
|
|
|
def is_ip(s: str) -> bool:
|
|
|
|
try:
|
|
|
|
ipaddress.ip_address(s)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
return True
|
2019-02-25 10:36:17 +00:00
|
|
|
|
|
|
|
def is_main_thread() -> bool:
|
|
|
|
return threading.current_thread() is threading.main_thread()
|
2019-06-28 22:16:05 +00:00
|
|
|
|
2019-08-30 16:17:03 +00:00
|
|
|
class DeadlineExceededException(Exception):
|
|
|
|
pass
|
|
|
|
def _raise_deadline():
|
|
|
|
raise DeadlineExceededException()
|
|
|
|
|
2019-08-30 17:13:12 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def deadline(seconds: int=10):
|
|
|
|
old_handler = signal.signal(signal.SIGALRM,
|
|
|
|
lambda _1, _2: _raise_deadline())
|
2019-08-30 17:36:48 +00:00
|
|
|
old_seconds, _ = signal.setitimer(signal.ITIMER_REAL, seconds, 0)
|
|
|
|
|
2019-08-30 16:17:03 +00:00
|
|
|
try:
|
2019-09-02 14:49:55 +00:00
|
|
|
if not old_seconds == 0.0 and seconds > old_seconds:
|
|
|
|
raise ValueError(
|
|
|
|
"Deadline timeout larger than parent deadline (%s > %s)" %
|
|
|
|
(seconds, old_seconds))
|
|
|
|
|
2019-08-30 17:13:12 +00:00
|
|
|
yield
|
2019-08-30 16:17:03 +00:00
|
|
|
finally:
|
2019-08-30 17:13:12 +00:00
|
|
|
signal.signal(signal.SIGALRM, old_handler)
|
2019-08-30 17:36:48 +00:00
|
|
|
signal.setitimer(signal.ITIMER_REAL, old_seconds, 0)
|
2019-09-17 10:56:30 +00:00
|
|
|
|
2019-10-31 13:06:26 +00:00
|
|
|
DeadlineProcessReturnType = typing.TypeVar("DeadlineProcessReturnType")
|
2019-10-25 22:32:00 +00:00
|
|
|
def deadline_process(func: typing.Callable[[], DeadlineProcessReturnType],
|
|
|
|
seconds: int=10) -> DeadlineProcessReturnType:
|
2019-10-31 13:06:26 +00:00
|
|
|
q: multiprocessing.Queue[
|
|
|
|
typing.Tuple[bool, DeadlineProcessReturnType]] = multiprocessing.Queue()
|
2019-09-17 10:56:30 +00:00
|
|
|
def _wrap(func, q):
|
|
|
|
try:
|
|
|
|
q.put([True, func()])
|
|
|
|
except Exception as e:
|
|
|
|
q.put([False, e])
|
|
|
|
|
|
|
|
p = multiprocessing.Process(target=_wrap, args=(func, q))
|
|
|
|
p.start()
|
|
|
|
|
2019-09-18 09:21:40 +00:00
|
|
|
deadlined = False
|
2019-09-17 12:39:23 +00:00
|
|
|
try:
|
|
|
|
success, out = q.get(block=True, timeout=seconds)
|
|
|
|
except queue.Empty:
|
2019-10-25 22:32:00 +00:00
|
|
|
p.kill() # type: ignore # to make mypy pass on Python 3.6
|
2019-09-18 09:21:40 +00:00
|
|
|
deadlined = True
|
2019-09-18 09:24:01 +00:00
|
|
|
finally:
|
|
|
|
q.close()
|
2019-09-18 09:21:40 +00:00
|
|
|
|
|
|
|
if deadlined:
|
2019-09-17 10:56:30 +00:00
|
|
|
_raise_deadline()
|
|
|
|
|
|
|
|
if success:
|
|
|
|
return out
|
|
|
|
else:
|
2019-10-25 22:32:00 +00:00
|
|
|
raise out # type: ignore
|