From 873a3227373833d6e2fefd2a5abcee455a999cef Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Fri, 9 Feb 2018 16:27:10 -0600 Subject: [PATCH 1/3] Fix issues arising from multiple PR merges --- cloudbot/__init__.py | 4 +++- cloudbot/bot.py | 2 +- cloudbot/client.py | 2 +- cloudbot/clients/irc.py | 5 +++-- cloudbot/util/web.py | 32 ++++++++++++++++---------------- plugins/core/chan_log.py | 15 +++++++++------ plugins/core/check_conn.py | 9 ++++++++- plugins/cryptocurrency.py | 8 ++++++-- 8 files changed, 47 insertions(+), 30 deletions(-) diff --git a/cloudbot/__init__.py b/cloudbot/__init__.py index b02f5c002..842648341 100644 --- a/cloudbot/__init__.py +++ b/cloudbot/__init__.py @@ -79,8 +79,10 @@ def _setup(): dict_config["handlers"]["console"]["level"] = "DEBUG" dict_config["loggers"]["asyncio"] = { "level": "DEBUG", - "handlers": ["console", "file"] + "handlers": ["console"] } + if file_log: + dict_config["loggers"]["asyncio"]["handlers"].append("file") if logging_config.get("file_debug", False): dict_config["handlers"]["debug_file"] = { diff --git a/cloudbot/bot.py b/cloudbot/bot.py index b0e44925c..486479028 100644 --- a/cloudbot/bot.py +++ b/cloudbot/bot.py @@ -239,7 +239,7 @@ def _init_routine(self): self.observer.start() # Connect to servers - yield from asyncio.gather(*[conn.try_connect() for conn in self.connections.values()], loop=self.loop) + yield from asyncio.gather(*[conn.connect() for conn in self.connections.values()], loop=self.loop) # Activate web interface. if self.config.get("web", {}).get("enabled", False) and web_installed: diff --git a/cloudbot/client.py b/cloudbot/client.py index 9b6aaea36..6388d6b2f 100644 --- a/cloudbot/client.py +++ b/cloudbot/client.py @@ -101,7 +101,7 @@ def connect(self, timeout=None): """ raise NotImplementedError - def quit(self, reason=None): + def quit(self, reason=None, set_inactive=True): """ Gracefully disconnects from the server with reason , close() should be called shortly after. """ diff --git a/cloudbot/clients/irc.py b/cloudbot/clients/irc.py index a8743c56f..ac8f729d7 100644 --- a/cloudbot/clients/irc.py +++ b/cloudbot/clients/irc.py @@ -170,8 +170,9 @@ def _connect(self, timeout=None): # TODO stop connecting if a connect hook fails? yield from asyncio.gather(*tasks) - def quit(self, reason=None): - self._active = False + def quit(self, reason=None, set_inactive=True): + if set_inactive: + self._active = False if self.connected: if reason: diff --git a/cloudbot/util/web.py b/cloudbot/util/web.py index 011b71b5e..23908c474 100644 --- a/cloudbot/util/web.py +++ b/cloudbot/util/web.py @@ -87,12 +87,12 @@ def try_shorten(self, url, custom=None, key=None): return url def expand(self, url): + r = requests.get(url, allow_redirects=False) try: - r = requests.get(url, allow_redirects=False) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) if 'location' in r.headers: return r.headers['location'] @@ -132,12 +132,12 @@ def _decorate(impl): class Isgd(Shortener): def shorten(self, url, custom=None, key=None): p = {'url': url, 'shorturl': custom, 'format': 'json'} + r = requests.get('http://is.gd/create.php', params=p) try: - r = requests.get('http://is.gd/create.php', params=p) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) j = r.json() @@ -148,12 +148,12 @@ def shorten(self, url, custom=None, key=None): def expand(self, url): p = {'shorturl': url, 'format': 'json'} + r = requests.get('http://is.gd/forward.php', params=p) try: - r = requests.get('http://is.gd/forward.php', params=p) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) j = r.json() @@ -169,12 +169,12 @@ def shorten(self, url, custom=None, key=None): h = {'content-type': 'application/json'} k = {'key': key} p = {'longUrl': url} + r = requests.post('https://www.googleapis.com/urlshortener/v1/url', params=k, data=json.dumps(p), headers=h) try: - r = requests.post('https://www.googleapis.com/urlshortener/v1/url', params=k, data=json.dumps(p), headers=h) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) j = r.json() @@ -185,12 +185,12 @@ def shorten(self, url, custom=None, key=None): def expand(self, url): p = {'shortUrl': url} + r = requests.get('https://www.googleapis.com/urlshortener/v1/url', params=p) try: - r = requests.get('https://www.googleapis.com/urlshortener/v1/url', params=p) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) j = r.json() @@ -204,12 +204,12 @@ def expand(self, url): class Gitio(Shortener): def shorten(self, url, custom=None, key=None): p = {'url': url, 'code': custom} + r = requests.post('http://git.io', data=p) try: - r = requests.post('http://git.io', data=p) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) if r.status_code == requests.codes.created: s = r.headers['location'] @@ -224,12 +224,12 @@ def shorten(self, url, custom=None, key=None): @_pastebin('hastebin') class Hastebin(Pastebin): def paste(self, data, ext): + r = requests.post(HASTEBIN_SERVER + '/documents', data=data) try: - r = requests.post(HASTEBIN_SERVER + '/documents', data=data) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) else: j = r.json() @@ -246,11 +246,11 @@ def paste(self, data, ext): 'text': data, 'expire': '1d' } + r = requests.post(SNOONET_PASTE + '/paste/new', data=params) try: - r = requests.post(SNOONET_PASTE + '/paste/new', data=params) r.raise_for_status() except RequestException as e: r = e.response - raise ServiceError(r.status_code, r) + raise ServiceError(r.reason, r) else: return '{}'.format(r.url) diff --git a/plugins/core/chan_log.py b/plugins/core/chan_log.py index c271c4e05..ecd0b66e0 100644 --- a/plugins/core/chan_log.py +++ b/plugins/core/chan_log.py @@ -24,14 +24,16 @@ def on_hook_end(error, launched_hook, launched_event, admin_log): lines = traceback.format_exception(*error) last_line = lines[-1] messages.append(last_line.strip()) - except Exception as e: - messages.append("Error occurred while formatting error {}: {}".format(type(e), e)) + except Exception: + msg = traceback.format_exc()[-1] + messages.append("Error occurred while formatting error {}".format(msg)) else: try: url = web.paste('\n'.join(lines)) messages.append("Traceback: " + url) - except Exception as e: - messages.append("Error occurred while gathering traceback {}: {}".format(type(e), e)) + except Exception: + msg = traceback.format_exc()[-1] + messages.append("Error occurred while gathering traceback {}".format(msg)) try: lines = ["{} = {}".format(k, v) for k, v in _dump_attrs(launched_event)] @@ -56,8 +58,9 @@ def on_hook_end(error, launched_hook, launched_event, admin_log): url = web.paste('\n'.join(lines)) messages.append("Event: " + url) - except Exception as e: - messages.append("Error occurred while gathering error data {}: {}".format(type(e), e)) + except Exception: + msg = traceback.format_exc()[-1] + messages.append("Error occurred while gathering error data {}".format(msg)) for message in messages: admin_log(message, should_broadcast) diff --git a/plugins/core/check_conn.py b/plugins/core/check_conn.py index 650c562ef..44d267b6c 100644 --- a/plugins/core/check_conn.py +++ b/plugins/core/check_conn.py @@ -10,8 +10,11 @@ def do_reconnect(conn, auto=True): if conn.connected: conn.quit("Reconnecting...") yield from asyncio.sleep(5) + did_quit = True + else: + did_quit = False - if auto: + if auto and not did_quit: coro = conn.auto_reconnect() else: coro = conn.connect(30) @@ -19,6 +22,9 @@ def do_reconnect(conn, auto=True): try: yield from coro except asyncio.TimeoutError: + if auto: + raise + return "Connection timed out" return "Reconnected to '{}'".format(conn.name) @@ -87,6 +93,7 @@ def on_connect(conn): conn.memory["last_activity"] = now conn.memory["lag"] = 0 conn.memory["needs_reconnect"] = False + conn.memory['last_ping_rpl'] = now @hook.command("lagcheck", autohelp=False, permissions=["botcontrol"]) diff --git a/plugins/cryptocurrency.py b/plugins/cryptocurrency.py index ceead68c8..80257db1d 100644 --- a/plugins/cryptocurrency.py +++ b/plugins/cryptocurrency.py @@ -9,6 +9,7 @@ License: GPL v3 """ +import asyncio from collections import defaultdict from datetime import datetime, timedelta from operator import itemgetter @@ -19,7 +20,9 @@ from yarl import URL from cloudbot import hook +from cloudbot.event import CommandEvent from cloudbot.util import colors, web +from cloudbot.util.func_utils import call_with_args CURRENCY_SYMBOLS = { 'USD': '$', @@ -153,8 +156,9 @@ def __init__(self, name, *cmds): def alias_wrapper(alias): - def func(text, reply): - return crypto_command(" ".join((alias.name, text)), reply) + def func(text, event): + event.text = alias.name + " " + text + return call_with_args(crypto_command, event) func.__doc__ = """- Returns the current {} value""".format(alias.name) func.__name__ = alias.name + "_alias" From 37a4a33032b6b1816c834a6a5c9fd0f389f048ab Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Fri, 9 Feb 2018 18:26:32 -0600 Subject: [PATCH 2/3] Fix tests --- tests/core_tests/test_plugin_hooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core_tests/test_plugin_hooks.py b/tests/core_tests/test_plugin_hooks.py index 372b75b0b..3528b53a5 100644 --- a/tests/core_tests/test_plugin_hooks.py +++ b/tests/core_tests/test_plugin_hooks.py @@ -120,7 +120,7 @@ def test_hook_args(hook): if hook.type in ("irc_raw", "perm_check", "periodic", "on_start", "on_stop", "event", "on_connect"): event = Event(bot=bot) elif hook.type == "command": - event = CommandEvent(bot=bot, hook=hook, text="", triggered_command="") + event = CommandEvent(bot=bot, hook=hook, text="", triggered_command="", cmd_prefix='.') elif hook.type == "regex": event = RegexEvent(bot=bot, hook=hook, match=None) elif hook.type.startswith("on_cap"): From 84ea6ff28d651dd1350c4e717e26384409511625 Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Fri, 9 Feb 2018 19:18:05 -0600 Subject: [PATCH 3/3] Fix tests --- cloudbot/util/func_utils.py | 2 +- plugins/cryptocurrency.py | 2 ++ plugins/stock.py | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cloudbot/util/func_utils.py b/cloudbot/util/func_utils.py index 63ee63601..09b4cebd3 100644 --- a/cloudbot/util/func_utils.py +++ b/cloudbot/util/func_utils.py @@ -3,7 +3,7 @@ class ParameterError(Exception): def __init__(self, name, valid_args): - self.__init__(name, list(valid_args)) + super().__init__(name, list(valid_args)) def __str__(self): return "'{}' is not a valid parameter, valid parameters are: {}".format(self.args[0], self.args[1]) diff --git a/plugins/cryptocurrency.py b/plugins/cryptocurrency.py index 80257db1d..f0a9ad807 100644 --- a/plugins/cryptocurrency.py +++ b/plugins/cryptocurrency.py @@ -41,11 +41,13 @@ class APIRateLimitError(APIError): class TickerNotFound(APIError): def __init__(self, name): + super().__init__(name) self.currency = name class CurrencyConversionError(APIError): def __init__(self, in_name, out_name): + super().__init__(in_name, out_name) self.in_name = in_name self.out_name = out_name diff --git a/plugins/stock.py b/plugins/stock.py index ed9b5ebe2..50c97f7b0 100644 --- a/plugins/stock.py +++ b/plugins/stock.py @@ -19,6 +19,7 @@ class APIError(Exception): class StockSymbolNotFoundError(APIError): def __init__(self, symbol): + super().__init__(symbol) self.symbol = symbol