From cb15346a78c9d692f727552729c8161a94509e32 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 14 Mar 2022 21:19:26 +0000 Subject: [PATCH] Refactor Agora into sources --- handler/app.py | 4 +-- handler/{ => lib}/agoradesk_py.py | 0 handler/{ => lib}/localbitcoins.py | 4 +-- handler/sources/__init__.py | 40 ++++++++++++++++++++++++++++++ handler/{ => sources}/agora.py | 4 +-- handler/tests/test_agora.py | 2 +- handler/tests/test_markets.py | 2 +- handler/ux/__init__.py | 2 +- handler/ux/irc.py | 2 +- 9 files changed, 50 insertions(+), 10 deletions(-) rename handler/{ => lib}/agoradesk_py.py (100%) rename handler/{ => lib}/localbitcoins.py (98%) create mode 100644 handler/sources/__init__.py rename handler/{ => sources}/agora.py (99%) diff --git a/handler/app.py b/handler/app.py index 3234a66..e89da6e 100755 --- a/handler/app.py +++ b/handler/app.py @@ -12,13 +12,13 @@ from settings import settings import util # Old style classes -from agora import Agora from transactions import Transactions from markets import Markets from money import Money # New style classes import sinks +import sources import ux init_map = None @@ -80,8 +80,8 @@ class WebApp(util.Base): if __name__ == "__main__": init_map = { "ux": ux.UX(), - "agora": Agora(), "markets": Markets(), + "sources": sources.Sources(), "sinks": sinks.Sinks(), "tx": Transactions(), "webapp": WebApp(), diff --git a/handler/agoradesk_py.py b/handler/lib/agoradesk_py.py similarity index 100% rename from handler/agoradesk_py.py rename to handler/lib/agoradesk_py.py diff --git a/handler/localbitcoins.py b/handler/lib/localbitcoins.py similarity index 98% rename from handler/localbitcoins.py rename to handler/lib/localbitcoins.py index b21e448..619bd66 100644 --- a/handler/localbitcoins.py +++ b/handler/lib/localbitcoins.py @@ -60,7 +60,7 @@ class Connection: # If URL is absolute, then convert it if url.startswith(self.server): - url = url[len(self.server) :] + url = url[len(self.server) :] # noqa # If OAuth2 if self.access_token: @@ -140,7 +140,7 @@ class Connection: if int(response_json.get("error", {}).get("error_code")) == 42: time.sleep(0.1) continue - except: + except: # noqa # No JSONic response, or interrupt, better just give up pass diff --git a/handler/sources/__init__.py b/handler/sources/__init__.py new file mode 100644 index 0000000..f1451dd --- /dev/null +++ b/handler/sources/__init__.py @@ -0,0 +1,40 @@ +# Project imports +# from settings import settings +import util +import sources.agora + +# import sources.localbitcoins + + +class Sources(util.Base): + """ + Class to manage calls to various sources. + """ + + def __init__(self): + super().__init__() + self.agora = sources.agora.Agora() + # self.localbitcoins = sources.localbitcoins.LocalBitcoins() + + def __irc_started__(self): + self.agora.setup_loop() + # self.localbitcoins.setup_loop() + + def __xmerged__(self): + """ + Called when xmerge has been completed in the webapp. + Merge all instances into child classes. + """ + init_map = { + "ux": self.ux, + "agora": self.agora, + "markets": self.markets, + "sinks": self.sinks, + "sources": self, + "tx": self.tx, + "webapp": self.webapp, + "money": self.money, + "irc": self.irc, + "notify": self.notify, + } + util.xmerge_attrs(init_map) diff --git a/handler/agora.py b/handler/sources/agora.py similarity index 99% rename from handler/agora.py rename to handler/sources/agora.py index db4dd2f..c4b9349 100644 --- a/handler/agora.py +++ b/handler/sources/agora.py @@ -5,7 +5,7 @@ from twisted.internet.threads import deferToThread # Other library imports from json import loads from forex_python.converter import CurrencyRates -from agoradesk_py import AgoraDesk +from lib.agoradesk_py import AgoraDesk from pycoingecko import CoinGeckoAPI # TODO: remove this import and defer to money from time import sleep from pyotp import TOTP @@ -40,7 +40,7 @@ class Agora(util.Base): # Assets that cheat has been run on self.cheat_run_on = [] - def setup_loop(self): + def setup_loop(self): # TODO:: move to main sources """ Set up the LoopingCall to get all active trades and messages. """ diff --git a/handler/tests/test_agora.py b/handler/tests/test_agora.py index 66a20e8..081af84 100644 --- a/handler/tests/test_agora.py +++ b/handler/tests/test_agora.py @@ -4,7 +4,7 @@ from json import loads from copy import deepcopy from tests.common import fake_public_ads, cg_prices, expected_to_update -from agora import Agora +from sources.agora import Agora from markets import Markets from money import Money import util diff --git a/handler/tests/test_markets.py b/handler/tests/test_markets.py index bf931ba..85bc8cd 100644 --- a/handler/tests/test_markets.py +++ b/handler/tests/test_markets.py @@ -1,7 +1,7 @@ from unittest import TestCase from tests.common import fake_public_ads, expected_to_update from markets import Markets -from agora import Agora +from sources.agora import Agora class TestMarkets(TestCase): diff --git a/handler/ux/__init__.py b/handler/ux/__init__.py index 27899c8..51757e1 100644 --- a/handler/ux/__init__.py +++ b/handler/ux/__init__.py @@ -29,9 +29,9 @@ class UX(object): """ init_map = { "ux": self, - "agora": self.agora, "markets": self.markets, "sinks": self.sinks, + "sources": self.sources, "tx": self.tx, "webapp": self.webapp, "money": self.money, diff --git a/handler/ux/irc.py b/handler/ux/irc.py index 301d573..c0ce0cd 100644 --- a/handler/ux/irc.py +++ b/handler/ux/irc.py @@ -117,8 +117,8 @@ class IRCBot(irc.IRCClient): :param channel: channel we joined :type channel: string """ - self.agora.setup_loop() self.sinks.__irc_started__() + self.sources.__irc_started__() self.log.info(f"Joined channel {channel}") def privmsg(self, user, channel, msg):