Browse Source

Handy new class PrintError

Saves adding "def print_error" to endless classes.
283
Neil Booth 10 years ago
parent
commit
93b99ebded
  1. 5
      lib/blockchain.py
  2. 12
      lib/interface.py
  3. 15
      lib/plugins.py
  4. 25
      lib/util.py
  5. 12
      plugins/exchange_rate.py

5
lib/blockchain.py

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

12
lib/interface.py

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

15
lib/plugins.py

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

25
lib/util.py

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

12
plugins/exchange_rate.py

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

Loading…
Cancel
Save