Handy new class PrintError

Saves adding "def print_error" to endless classes.
This commit is contained in:
Neil Booth 2015-09-06 21:40:00 +09:00
parent 735a9e9a29
commit 93b99ebded
5 changed files with 32 additions and 39 deletions

View File

@ -22,7 +22,7 @@ import util
from bitcoin import *
class Blockchain():
class Blockchain(util.PrintError):
'''Manages blockchain headers and their verification'''
def __init__(self, config, network):
self.config = config
@ -31,9 +31,6 @@ class Blockchain():
self.local_height = 0
self.set_local_height()
def print_error(self, *msg):
util.print_error("[blockchain]", *msg)
def height(self):
return self.local_height

View File

@ -49,7 +49,7 @@ def Connection(server, queue, config_path):
c.start()
return c
class TcpConnection(threading.Thread):
class TcpConnection(threading.Thread, util.PrintError):
def __init__(self, server, queue, config_path):
threading.Thread.__init__(self)
@ -62,8 +62,8 @@ class TcpConnection(threading.Thread):
self.port = int(self.port)
self.use_ssl = (self.protocol == 's')
def print_error(self, *msg):
util.print_error("[%s]" % self.host, *msg)
def diagnostic_name(self):
return self.host
def check_host_name(self, peercert, name):
"""Simple certificate/host name checker. Returns True if the
@ -203,7 +203,7 @@ class TcpConnection(threading.Thread):
self.print_error("connected")
self.queue.put((self.server, socket))
class Interface:
class Interface(util.PrintError):
"""The Interface class handles a socket connected to a single remote
electrum server. It's exposed API is:
@ -229,8 +229,8 @@ class Interface:
self.last_ping = 0
self.closed_remotely = False
def print_error(self, *msg):
util.print_error("[%s]" % self.host, *msg)
def diagnostic_name(self):
return self.host
def fileno(self):
# Needed for select

View File

@ -24,9 +24,9 @@ import pkgutil
from util import *
from i18n import _
from util import print_error, profiler
from util import profiler, PrintError
class Plugins:
class Plugins(PrintError):
@profiler
def __init__(self, config, is_local, gui_name):
@ -52,9 +52,6 @@ class Plugins:
if config.get('use_' + name):
self.load_plugin(config, name)
def print_error(self, *msg):
print_error("[%s]" % self.__class__.__name__, *msg)
def get(self, name):
return self.plugins.get(name)
@ -184,7 +181,7 @@ def _run_hook(name, always, *args):
return results[0]
class BasePlugin:
class BasePlugin(PrintError):
def __init__(self, parent, config, name):
self.parent = parent # The plugins object
@ -198,6 +195,9 @@ class BasePlugin:
l.append((self, getattr(self, k)))
hooks[k] = l
def diagnostic_name(self):
return self.name
def close(self):
# remove self from hooks
for k in dir(self):
@ -207,9 +207,6 @@ class BasePlugin:
hooks[k] = l
self.parent.close_plugin(self)
def print_error(self, *msg):
print_error("[%s]"%self.name, *msg)
def requires_settings(self):
return False

View File

@ -25,22 +25,27 @@ class MyEncoder(json.JSONEncoder):
return obj.as_dict()
return super(MyEncoder, self).default(obj)
class ThreadJob:
class PrintError:
'''A handy base class'''
def diagnostic_name(self):
return self.__class__.__name__
def print_error(self, *msg):
print_error("[%s]" % self.diagnostic_name(), *msg)
def print_msg(self, *msg):
print_msg("[%s]" % self.diagnostic_name(), *msg)
class ThreadJob(PrintError):
"""A job that is run periodically from a thread's main loop. run() is
called from that thread's context.
"""
def print_error(self, *msg):
print_error("[%s]" % self.__class__.__name__, *msg)
def print_msg(self, *msg):
print_msg("[%s]" % self.__class__.__name__, *msg)
def run(self):
"""Called periodically from the thread"""
pass
class DaemonThread(threading.Thread):
class DaemonThread(threading.Thread, PrintError):
""" daemon thread that terminates cleanly """
def __init__(self):
@ -84,12 +89,6 @@ class DaemonThread(threading.Thread):
with self.running_lock:
self.running = False
def print_error(self, *msg):
print_error("[%s]" % self.__class__.__name__, *msg)
def print_msg(self, *msg):
print_msg("[%s]" % self.__class__.__name__, *msg)
is_verbose = False

View File

@ -14,7 +14,7 @@ from functools import partial
from electrum.bitcoin import COIN
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
from electrum.util import print_error, ThreadJob, timestamp_to_datetime
from electrum.util import PrintError, ThreadJob, timestamp_to_datetime
from electrum.util import format_satoshis
from electrum_gui.qt.util import *
from electrum_gui.qt.amountedit import AmountEdit
@ -28,7 +28,7 @@ CCY_PRECISIONS = {'BHD': 3, 'BIF': 0, 'BYR': 0, 'CLF': 4, 'CLP': 0,
'VUV': 0, 'XAF': 0, 'XAG': 2, 'XAU': 4, 'XOF': 0,
'XPF': 0}
class ExchangeBase:
class ExchangeBase(PrintError):
def __init__(self, sig):
self.history = {}
self.quotes = {}
@ -39,9 +39,6 @@ class ExchangeBase:
headers={'User-Agent' : 'Electrum'})
return response.json()
def print_error(self, *msg):
print_error("[%s]" % self.name(), *msg)
def name(self):
return self.__class__.__name__
@ -114,11 +111,14 @@ class BitPay(ExchangeBase):
json = self.get_json('bitpay.com', '/api/rates')
return dict([(r['code'], Decimal(r['rate'])) for r in json])
class Blockchain(ExchangeBase):
class BlockchainInfo(ExchangeBase):
def get_rates(self, ccy):
json = self.get_json('blockchain.info', '/ticker')
return dict([(r, Decimal(json[r]['15m'])) for r in json])
def name(self):
return "Blockchain"
class BTCChina(ExchangeBase):
def get_rates(self, ccy):
json = self.get_json('data.btcchina.com', '/data/ticker')