Use Black code style

This commit is contained in:
2021-02-22 19:02:13 +01:00
committed by Valentin Lorentz
parent 34ed62fd85
commit 8016e01daf
59 changed files with 4855 additions and 3033 deletions

View File

@ -3,24 +3,35 @@ import re
import secrets
from collections import namedtuple
HistoryMessage = namedtuple('HistoryMessage', ['time', 'msgid', 'target', 'text'])
HistoryMessage = namedtuple("HistoryMessage", ["time", "msgid", "target", "text"])
def to_history_message(msg):
return HistoryMessage(time=msg.tags.get('time'), msgid=msg.tags.get('msgid'), target=msg.params[0], text=msg.params[1])
return HistoryMessage(
time=msg.tags.get("time"),
msgid=msg.tags.get("msgid"),
target=msg.params[0],
text=msg.params[1],
)
# thanks jess!
IRCV3_FORMAT_STRFTIME = "%Y-%m-%dT%H:%M:%S.%f%z"
def ircv3_timestamp_to_unixtime(timestamp):
return datetime.datetime.strptime(timestamp, IRCV3_FORMAT_STRFTIME).timestamp()
def random_name(base):
return base + '-' + secrets.token_hex(8)
return base + "-" + secrets.token_hex(8)
"""
Stolen from supybot:
"""
class MultipleReplacer:
"""Return a callable that replaces all dict keys by the associated
value. More efficient than multiple .replace()."""
@ -30,24 +41,26 @@ class MultipleReplacer:
# it to a class in Python 3.
def __init__(self, dict_):
self._dict = dict_
dict_ = dict([(re.escape(key), val) for key,val in dict_.items()])
self._matcher = re.compile('|'.join(dict_.keys()))
dict_ = dict([(re.escape(key), val) for key, val in dict_.items()])
self._matcher = re.compile("|".join(dict_.keys()))
def __call__(self, s):
return self._matcher.sub(lambda m: self._dict[m.group(0)], s)
def normalizeWhitespace(s, removeNewline=True):
r"""Normalizes the whitespace in a string; \s+ becomes one space."""
if not s:
return str(s) # not the same reference
starts_with_space = (s[0] in ' \n\t\r')
ends_with_space = (s[-1] in ' \n\t\r')
return str(s) # not the same reference
starts_with_space = s[0] in " \n\t\r"
ends_with_space = s[-1] in " \n\t\r"
if removeNewline:
newline_re = re.compile('[\r\n]+')
s = ' '.join(filter(bool, newline_re.split(s)))
s = ' '.join(filter(bool, s.split('\t')))
s = ' '.join(filter(bool, s.split(' ')))
newline_re = re.compile("[\r\n]+")
s = " ".join(filter(bool, newline_re.split(s)))
s = " ".join(filter(bool, s.split("\t")))
s = " ".join(filter(bool, s.split(" ")))
if starts_with_space:
s = ' ' + s
s = " " + s
if ends_with_space:
s += ' '
s += " "
return s