Use the Python logger instead of the Twisted one

This commit is contained in:
2022-03-05 21:52:31 +00:00
parent 80c696ef73
commit 539d6f1fbb
15 changed files with 224 additions and 151 deletions

View File

@@ -1,6 +1,3 @@
# Twisted/Klein imports
from twisted.logger import Logger
# Other library imports
# import requests
# from json import dumps
@@ -20,7 +17,7 @@ class UX(object):
"""
def __init__(self):
self.log = Logger("ux")
super().__init__()
self.irc = ux.irc.bot()
self.notify = ux.notify.Notify()

View File

@@ -447,5 +447,32 @@ class IRCCommands(object):
@staticmethod
def run(cmd, spl, length, authed, msg, agora, tx, ux):
auth_url = agora.truelayer.create_auth_url()
auth_url = tx.truelayer.create_auth_url()
msg(f"Auth URL: {auth_url}")
class accounts(object):
name = "accounts"
authed = True
helptext = "Get a list of acccounts."
@staticmethod
def run(cmd, spl, length, authed, msg, agora, tx, ux):
accounts = tx.sinks.truelayer.get_accounts()
msg(dumps(accounts))
class transactions(object):
name = "transactions"
authed = True
helptext = "Get a list of transactions. Usage: transactions <account_id>"
@staticmethod
def run(cmd, spl, length, authed, msg, agora, tx, ux):
if length == 2:
account_id = spl[1]
transactions = tx.sinks.truelayer.get_transactions(account_id)
for transaction in transactions:
timestamp = transaction["timestamp"]
amount = transaction["amount"]
currency = transaction["currency"]
recipient = transaction["counter_party_preferred_name"]
msg(f"{timestamp} {amount}{currency} {recipient}")

View File

@@ -1,5 +1,4 @@
# Twisted/Klein imports
from twisted.logger import Logger
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor, ssl
from twisted.internet.task import deferLater
@@ -7,6 +6,7 @@ from twisted.internet.task import deferLater
# Project imports
from settings import settings
from ux.commands import IRCCommands
import util
class IRCBot(irc.IRCClient):
@@ -106,7 +106,7 @@ class IRCBot(irc.IRCClient):
Called when we have signed on to IRC.
Join our channel.
"""
self.log.info("Signed on as %s" % (self.nickname))
self.log.info(f"Signed on as {self.nickname}")
deferLater(reactor, 2, self.join, self.channel)
def joined(self, channel):
@@ -118,7 +118,7 @@ class IRCBot(irc.IRCClient):
:type channel: string
"""
self.agora.setup_loop()
self.log.info("Joined channel %s" % (channel))
self.log.info(f"Joined channel {channel}")
def privmsg(self, user, channel, msg):
"""
@@ -141,7 +141,7 @@ class IRCBot(irc.IRCClient):
ident = user.split("!")[1]
ident = ident.split("@")[0]
self.log.info("(%s) %s: %s" % (channel, user, msg))
self.log.info(f"({channel}) {user}: {msg}")
if msg[0] == self.prefix:
if len(msg) > 1:
if msg.split()[0] != "!":
@@ -171,7 +171,8 @@ class IRCBot(irc.IRCClient):
class IRCBotFactory(protocol.ClientFactory):
def __init__(self):
self.log = Logger("irc")
self.log = util.get_logger("IRC")
self.log.info("Class initialised")
def sendmsg(self, msg):
"""
@@ -180,7 +181,7 @@ class IRCBotFactory(protocol.ClientFactory):
if self.client:
self.client.msg(self.client.channel, msg)
else:
self.log.error("Trying to send a message without connected client: {msg}", msg=msg)
self.log.error(f"Trying to send a message without connected client: {msg}")
return
def buildProtocol(self, addr):
@@ -189,6 +190,7 @@ class IRCBotFactory(protocol.ClientFactory):
Passes through the Agora instance to IRC.
:return: IRCBot Protocol instance
"""
# Pass through the logger
prcol = IRCBot(self.log)
self.client = prcol
setattr(self.client, "agora", self.agora)
@@ -205,7 +207,7 @@ class IRCBotFactory(protocol.ClientFactory):
:type connector: object
:type reason: string
"""
self.log.error("Lost connection: {reason}, reconnecting", reason=reason)
self.log.error(f"Lost connection: {reason}, reconnecting")
connector.connect()
def clientConnectionFailed(self, connector, reason):
@@ -216,7 +218,7 @@ class IRCBotFactory(protocol.ClientFactory):
:type connector: object
:type reason: string
"""
self.log.error("Could not connect: {reason}", reason=reason)
self.log.error(f"Could not connect: {reason}")
connector.connect()

View File

@@ -1,21 +1,16 @@
# Twisted/Klein imports
from twisted.logger import Logger
# Other library imports
import requests
# Project imports
from settings import settings
import util
class Notify(object):
class Notify(util.Base):
"""
Class to handle more robust notifications.
"""
def __init__(self):
self.log = Logger("notify")
def sendmsg(self, msg, title=None, priority=None, tags=None):
headers = {"Title": "Bot"}
if title: