aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/__init__.py123
-rw-r--r--src/utils/consts.py2
-rw-r--r--src/utils/http.py11
-rw-r--r--src/utils/irc.py46
-rw-r--r--src/utils/parse.py57
5 files changed, 124 insertions, 115 deletions
diff --git a/src/utils/__init__.py b/src/utils/__init__.py
index 87671108..52de64ba 100644
--- a/src/utils/__init__.py
+++ b/src/utils/__init__.py
@@ -1,6 +1,5 @@
-import decimal, io, re
-from src import ModuleManager
-from . import irc, http
+import decimal, io, re, typing
+from src.utils import consts, irc, http, parse
TIME_SECOND = 1
TIME_MINUTE = TIME_SECOND*60
@@ -8,7 +7,7 @@ TIME_HOUR = TIME_MINUTE*60
TIME_DAY = TIME_HOUR*24
TIME_WEEK = TIME_DAY*7
-def time_unit(seconds):
+def time_unit(seconds: int) -> typing.Tuple[int, str]:
since = None
unit = None
if seconds >= TIME_WEEK:
@@ -29,7 +28,7 @@ def time_unit(seconds):
since = int(since)
if since > 1:
unit = "%ss" % unit # pluralise the unit
- return [since, unit]
+ return (since, unit)
REGEX_PRETTYTIME = re.compile("\d+[wdhms]", re.I)
@@ -38,7 +37,7 @@ SECONDS_HOURS = SECONDS_MINUTES*60
SECONDS_DAYS = SECONDS_HOURS*24
SECONDS_WEEKS = SECONDS_DAYS*7
-def from_pretty_time(pretty_time):
+def from_pretty_time(pretty_time: str) -> typing.Optional[int]:
seconds = 0
for match in re.findall(REGEX_PRETTYTIME, pretty_time):
number, unit = int(match[:-1]), match[-1].lower()
@@ -54,12 +53,14 @@ def from_pretty_time(pretty_time):
if seconds > 0:
return seconds
+UNIT_MINIMUM = 6
UNIT_SECOND = 5
UNIT_MINUTE = 4
UNIT_HOUR = 3
UNIT_DAY = 2
UNIT_WEEK = 1
-def to_pretty_time(total_seconds, minimum_unit=UNIT_SECOND, max_units=6):
+def to_pretty_time(total_seconds: int, minimum_unit: int=UNIT_SECOND,
+ max_units: int=UNIT_MINIMUM) -> str:
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
@@ -84,7 +85,7 @@ def to_pretty_time(total_seconds, minimum_unit=UNIT_SECOND, max_units=6):
units += 1
return out
-def parse_number(s):
+def parse_number(s: str) -> str:
try:
decimal.Decimal(s)
return s
@@ -110,28 +111,18 @@ def parse_number(s):
IS_TRUE = ["true", "yes", "on", "y"]
IS_FALSE = ["false", "no", "off", "n"]
-def bool_or_none(s):
+def bool_or_none(s: str) -> typing.Optional[bool]:
s = s.lower()
if s in IS_TRUE:
return True
elif s in IS_FALSE:
return False
-def int_or_none(s):
+def int_or_none(s: str) -> typing.Optional[int]:
stripped_s = s.lstrip("0")
if stripped_s.isdigit():
return int(stripped_s)
-def get_closest_setting(event, setting, default=None):
- server = event["server"]
- if "channel" in event:
- closest = event["channel"]
- elif "target" in event and "is_channel" in event and event["is_channel"]:
- closest = event["target"]
- else:
- closest = event["user"]
- return closest.get_setting(setting, server.get_setting(setting, default))
-
-def prevent_highlight(nickname):
+def prevent_highlight(nickname: str) -> str:
return nickname[0]+"\u200c"+nickname[1:]
class EventError(Exception):
@@ -139,86 +130,40 @@ class EventError(Exception):
class EventsResultsError(EventError):
def __init__(self):
EventError.__init__(self, "Failed to load results")
+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)
-def _set_get_append(obj, setting, item):
+def _set_get_append(obj: typing.Any, setting: str, item: typing.Any):
if not hasattr(obj, setting):
setattr(obj, setting, [])
getattr(obj, setting).append(item)
-def hook(event, **kwargs):
+def hook(event: str, **kwargs):
def _hook_func(func):
- _set_get_append(func, ModuleManager.BITBOT_HOOKS_MAGIC,
+ _set_get_append(func, consts.BITBOT_HOOKS_MAGIC,
{"event": event, "kwargs": kwargs})
return func
return _hook_func
-def export(setting, value):
+def export(setting: str, value: typing.Any):
def _export_func(module):
- _set_get_append(module, ModuleManager.BITBOT_EXPORTS_MAGIC,
+ _set_get_append(module, consts.BITBOT_EXPORTS_MAGIC,
{"setting": setting, "value": value})
return module
return _export_func
-COMMENT_TYPES = ["#", "//"]
-def get_hashflags(filename):
- hashflags = {}
- with io.open(filename, mode="r", encoding="utf8") as f:
- for line in f:
- line = line.strip("\n")
- found = False
- for comment_type in COMMENT_TYPES:
- if line.startswith(comment_type):
- line = line.replace(comment_type, "", 1).lstrip()
- found = True
- break
-
- if not found:
- break
- elif line.startswith("--"):
- hashflag, sep, value = line[2:].partition(" ")
- hashflags[hashflag] = value if sep else None
- return hashflags.items()
-
-class Docstring(object):
- def __init__(self, description, items, var_items):
- self.description = description
- self.items = items
- self.var_items = var_items
-
-def parse_docstring(s):
- description = ""
- last_item = None
- items = {}
- var_items = {}
- if s:
- for line in s.split("\n"):
- line = line.strip()
-
- if line:
- if line[0] == ":":
- key, _, value = line[1:].partition(": ")
- last_item = key
-
- if key in var_items:
- var_items[key].append(value)
- elif key in items:
- var_items[key] = [items.pop(key), value]
- else:
- items[key] = value
- else:
- if last_item:
- items[last_item] += " %s" % line
- else:
- if description:
- description += " "
- description += line
- return Docstring(description, items, var_items)
-
-def top_10(items, convert_key=lambda x: x, value_format=lambda x: x):
- top_10 = sorted(items.keys())
- top_10 = sorted(top_10, key=items.get, reverse=True)[:10]
+TOP_10_CALLABLE = typing.Callable[[typing.Any], typing.Any]
+def top_10(items: typing.List[typing.Any],
+ 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])))
+ 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
+ return top_10_items
diff --git a/src/utils/consts.py b/src/utils/consts.py
new file mode 100644
index 00000000..d2816509
--- /dev/null
+++ b/src/utils/consts.py
@@ -0,0 +1,2 @@
+BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
+BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
diff --git a/src/utils/http.py b/src/utils/http.py
index ddf88b2b..b949e9ff 100644
--- a/src/utils/http.py
+++ b/src/utils/http.py
@@ -1,4 +1,4 @@
-import re, signal, traceback, urllib.error, urllib.parse
+import re, signal, traceback, typing, urllib.error, urllib.parse
import json as _json
import bs4, requests
@@ -18,9 +18,10 @@ class HTTPParsingException(HTTPException):
def throw_timeout():
raise HTTPTimeoutException()
-def get_url(url, method="GET", get_params={}, post_data=None, headers={},
- json_data=None, code=False, json=False, soup=False, parser="lxml",
- fallback_encoding="utf8"):
+def get_url(url: str, method: str="GET", get_params: dict={},
+ post_data: typing.Any=None, headers: dict={},
+ json_data: typing.Any=None, code: bool=False, json: bool=False,
+ soup: bool=False, parser: str="lxml", fallback_encoding: str="utf8"):
if not urllib.parse.urlparse(url).scheme:
url = "http://%s" % url
@@ -66,6 +67,6 @@ def get_url(url, method="GET", get_params={}, post_data=None, headers={},
else:
return data
-def strip_html(s):
+def strip_html(s: str) -> str:
return bs4.BeautifulSoup(s, "lxml").get_text()
diff --git a/src/utils/irc.py b/src/utils/irc.py
index 792de7f3..3e7f8b76 100644
--- a/src/utils/irc.py
+++ b/src/utils/irc.py
@@ -1,4 +1,4 @@
-import string, re
+import string, re, typing
ASCII_UPPER = string.ascii_uppercase
ASCII_LOWER = string.ascii_lowercase
@@ -7,32 +7,36 @@ STRICT_RFC1459_LOWER = ASCII_LOWER+r'|{}'
RFC1459_UPPER = STRICT_RFC1459_UPPER+"^"
RFC1459_LOWER = STRICT_RFC1459_LOWER+"~"
-def remove_colon(s):
+def remove_colon(s: str) -> str:
if s.startswith(":"):
s = s[1:]
return s
+MULTI_REPLACE_ITERABLE = typing.Iterable[str]
# case mapping lowercase/uppcase logic
-def _multi_replace(s, chars1, chars2):
+def _multi_replace(s: str,
+ chars1: typing.Iterable[str],
+ chars2: typing.Iterable[str]) -> str:
for char1, char2 in zip(chars1, chars2):
s = s.replace(char1, char2)
return s
-def lower(server, s):
- if server.case_mapping == "ascii":
+def lower(case_mapping: str, s: str) -> str:
+ if case_mapping == "ascii":
return _multi_replace(s, ASCII_UPPER, ASCII_LOWER)
- elif server.case_mapping == "rfc1459":
+ elif case_mapping == "rfc1459":
return _multi_replace(s, RFC1459_UPPER, RFC1459_LOWER)
- elif server.case_mapping == "strict-rfc1459":
+ elif case_mapping == "strict-rfc1459":
return _multi_replace(s, STRICT_RFC1459_UPPER, STRICT_RFC1459_LOWER)
else:
- raise ValueError("unknown casemapping '%s'" % server.case_mapping)
+ raise ValueError("unknown casemapping '%s'" % case_mapping)
# compare a string while respecting case mapping
-def equals(server, s1, s2):
- return lower(server, s1) == lower(server, s2)
+def equals(case_mapping: str, s1: str, s2: str) -> bool:
+ return lower(case_mapping, s1) == lower(case_mapping, s2)
class IRCHostmask(object):
- def __init__(self, nickname, username, hostname, hostmask):
+ def __init__(self, nickname: str, username: str, hostname: str,
+ hostmask: str):
self.nickname = nickname
self.username = username
self.hostname = hostname
@@ -42,24 +46,24 @@ class IRCHostmask(object):
def __str__(self):
return self.hostmask
-def seperate_hostmask(hostmask):
+def seperate_hostmask(hostmask: str) -> IRCHostmask:
hostmask = remove_colon(hostmask)
nickname, _, username = hostmask.partition("!")
username, _, hostname = username.partition("@")
return IRCHostmask(nickname, username, hostname, hostmask)
-
class IRCLine(object):
- def __init__(self, tags, prefix, command, args, arbitrary, last, server):
+ def __init__(self, tags: dict, prefix: str, command: str,
+ args: typing.List[str], arbitrary: typing.Optional[str],
+ last: str):
self.tags = tags
self.prefix = prefix
self.command = command
self.args = args
self.arbitrary = arbitrary
self.last = last
- self.server = server
-def parse_line(server, line):
+def parse_line(line: str) -> IRCLine:
tags = {}
prefix = None
command = None
@@ -81,7 +85,7 @@ def parse_line(server, line):
args = line.split(" ")
last = arbitrary or args[-1]
- return IRCLine(tags, prefix, command, args, arbitrary, last, server)
+ return IRCLine(tags, prefix, command, args, arbitrary, last)
COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GREEN = 0, 1, 2, 3
COLOR_RED, COLOR_BROWN, COLOR_PURPLE, COLOR_ORANGE = 4, 5, 6, 7
@@ -94,20 +98,20 @@ FONT_BOLD, FONT_ITALIC, FONT_UNDERLINE, FONT_INVERT = ("\x02", "\x1D",
FONT_COLOR, FONT_RESET = "\x03", "\x0F"
REGEX_COLOR = re.compile("%s\d\d(?:,\d\d)?" % FONT_COLOR)
-def color(s, foreground, background=None):
+def color(s: str, foreground: str, background: str=None) -> str:
foreground = str(foreground).zfill(2)
if background:
background = str(background).zfill(2)
return "%s%s%s%s%s" % (FONT_COLOR, foreground,
"" if not background else ",%s" % background, s, FONT_COLOR)
-def bold(s):
+def bold(s: str) -> str:
return "%s%s%s" % (FONT_BOLD, s, FONT_BOLD)
-def underline(s):
+def underline(s: str) -> str:
return "%s%s%s" % (FONT_UNDERLINE, s, FONT_UNDERLINE)
-def strip_font(s):
+def strip_font(s: str) -> str:
s = s.replace(FONT_BOLD, "")
s = s.replace(FONT_ITALIC, "")
s = REGEX_COLOR.sub("", s)
diff --git a/src/utils/parse.py b/src/utils/parse.py
new file mode 100644
index 00000000..03a585c2
--- /dev/null
+++ b/src/utils/parse.py
@@ -0,0 +1,57 @@
+import io, typing
+
+COMMENT_TYPES = ["#", "//"]
+def hashflags(filename: str) -> typing.List[typing.Tuple[str, str]]:
+ hashflags = {}
+ with io.open(filename, mode="r", encoding="utf8") as f:
+ for line in f:
+ line = line.strip("\n")
+ found = False
+ for comment_type in COMMENT_TYPES:
+ if line.startswith(comment_type):
+ line = line.replace(comment_type, "", 1).lstrip()
+ found = True
+ break
+
+ if not found:
+ break
+ elif line.startswith("--"):
+ hashflag, sep, value = line[2:].partition(" ")
+ hashflags[hashflag] = value if sep else None
+ return list(hashflags.items())
+
+class Docstring(object):
+ def __init__(self, description: str, items: dict, var_items: dict):
+ self.description = description
+ self.items = items
+ self.var_items = var_items
+
+def docstring(s: str) -> Docstring:
+ description = ""
+ last_item = None
+ items = {}
+ var_items = {}
+ if s:
+ for line in s.split("\n"):
+ line = line.strip()
+
+ if line:
+ if line[0] == ":":
+ key, _, value = line[1:].partition(": ")
+ last_item = key
+
+ if key in var_items:
+ var_items[key].append(value)
+ elif key in items:
+ var_items[key] = [items.pop(key), value]
+ else:
+ items[key] = value
+ else:
+ if last_item:
+ items[last_item] += " %s" % line
+ else:
+ if description:
+ description += " "
+ description += line
+ return Docstring(description, items, var_items)
+