mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-01 09:45:18 +00:00
Torwards sane signalling for exchange_rate plugin
This commit is contained in:
parent
2a9bc559b2
commit
b5f986ee56
4 changed files with 85 additions and 80 deletions
4
electrum
4
electrum
|
@ -526,7 +526,7 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
# daemon is not running
|
# daemon is not running
|
||||||
if cmd_name == 'gui':
|
if cmd_name == 'gui':
|
||||||
network = Network(config)
|
network = Network(config, plugins)
|
||||||
network.start()
|
network.start()
|
||||||
server = NetworkServer(config, network)
|
server = NetworkServer(config, network)
|
||||||
server.start()
|
server.start()
|
||||||
|
@ -540,7 +540,7 @@ if __name__ == '__main__':
|
||||||
elif subcommand == 'start':
|
elif subcommand == 'start':
|
||||||
p = os.fork()
|
p = os.fork()
|
||||||
if p == 0:
|
if p == 0:
|
||||||
network = Network(config)
|
network = Network(config, plugins)
|
||||||
network.start()
|
network.start()
|
||||||
server = NetworkServer(config, network)
|
server = NetworkServer(config, network)
|
||||||
if config.get('websocket_server'):
|
if config.get('websocket_server'):
|
||||||
|
|
|
@ -17,7 +17,6 @@ from bitcoin import *
|
||||||
from interface import Connection, Interface
|
from interface import Connection, Interface
|
||||||
from blockchain import Blockchain
|
from blockchain import Blockchain
|
||||||
from version import ELECTRUM_VERSION, PROTOCOL_VERSION
|
from version import ELECTRUM_VERSION, PROTOCOL_VERSION
|
||||||
from plugins import run_hook
|
|
||||||
|
|
||||||
DEFAULT_PORTS = {'t':'50001', 's':'50002', 'h':'8081', 'g':'8082'}
|
DEFAULT_PORTS = {'t':'50001', 's':'50002', 'h':'8081', 'g':'8082'}
|
||||||
|
|
||||||
|
@ -137,7 +136,7 @@ class Network(util.DaemonThread):
|
||||||
stop()
|
stop()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, config=None):
|
def __init__(self, config=None, plugins=None):
|
||||||
if config is None:
|
if config is None:
|
||||||
config = {} # Do not use mutables as default values!
|
config = {} # Do not use mutables as default values!
|
||||||
util.DaemonThread.__init__(self)
|
util.DaemonThread.__init__(self)
|
||||||
|
@ -194,6 +193,9 @@ class Network(util.DaemonThread):
|
||||||
self.socket_queue = Queue.Queue()
|
self.socket_queue = Queue.Queue()
|
||||||
self.start_network(deserialize_server(self.default_server)[2],
|
self.start_network(deserialize_server(self.default_server)[2],
|
||||||
deserialize_proxy(self.config.get('proxy')))
|
deserialize_proxy(self.config.get('proxy')))
|
||||||
|
self.plugins = plugins
|
||||||
|
if self.plugins:
|
||||||
|
self.plugins.set_network(self)
|
||||||
|
|
||||||
def register_callback(self, event, callback):
|
def register_callback(self, event, callback):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
@ -752,7 +754,6 @@ class Network(util.DaemonThread):
|
||||||
self.process_responses(interface)
|
self.process_responses(interface)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
run_hook('set_network', self)
|
|
||||||
self.blockchain.init()
|
self.blockchain.init()
|
||||||
while self.is_running():
|
while self.is_running():
|
||||||
self.maintain_sockets()
|
self.maintain_sockets()
|
||||||
|
@ -762,7 +763,8 @@ class Network(util.DaemonThread):
|
||||||
self.process_pending_sends()
|
self.process_pending_sends()
|
||||||
|
|
||||||
self.stop_network()
|
self.stop_network()
|
||||||
run_hook('set_network', None)
|
if self.plugins:
|
||||||
|
self.plugins.set_network(None)
|
||||||
self.print_error("stopped")
|
self.print_error("stopped")
|
||||||
|
|
||||||
def on_header(self, i, header):
|
def on_header(self, i, header):
|
||||||
|
|
|
@ -26,13 +26,6 @@ from util import *
|
||||||
from i18n import _
|
from i18n import _
|
||||||
from util import print_error, profiler
|
from util import print_error, profiler
|
||||||
|
|
||||||
hook_names = set()
|
|
||||||
hooks = {}
|
|
||||||
|
|
||||||
def hook(func):
|
|
||||||
hook_names.add(func.func_name)
|
|
||||||
return func
|
|
||||||
|
|
||||||
class Plugins:
|
class Plugins:
|
||||||
|
|
||||||
@profiler
|
@profiler
|
||||||
|
@ -126,11 +119,10 @@ class Plugins:
|
||||||
x += (lambda: self.wallet_plugin_loader(config, name),)
|
x += (lambda: self.wallet_plugin_loader(config, name),)
|
||||||
wallet.wallet_types.append(x)
|
wallet.wallet_types.append(x)
|
||||||
|
|
||||||
@hook
|
|
||||||
def set_network(self, network):
|
def set_network(self, network):
|
||||||
if network != self.network:
|
if network != self.network:
|
||||||
jobs = [job in plugin.thread_jobs()
|
jobs = [job for plugin in self.plugins.values()
|
||||||
for plugin in self.plugins.values()]
|
for job in plugin.thread_jobs()]
|
||||||
if self.network:
|
if self.network:
|
||||||
self.network.remove_jobs(jobs)
|
self.network.remove_jobs(jobs)
|
||||||
self.network = network
|
self.network = network
|
||||||
|
@ -149,6 +141,14 @@ class Plugins:
|
||||||
self.windows.remove(window)
|
self.windows.remove(window)
|
||||||
self.trigger('on_close_window', window)
|
self.trigger('on_close_window', window)
|
||||||
|
|
||||||
|
|
||||||
|
hook_names = set()
|
||||||
|
hooks = {}
|
||||||
|
|
||||||
|
def hook(func):
|
||||||
|
hook_names.add(func.func_name)
|
||||||
|
return func
|
||||||
|
|
||||||
def run_hook(name, *args):
|
def run_hook(name, *args):
|
||||||
return _run_hook(name, False, *args)
|
return _run_hook(name, False, *args)
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,10 @@ from electrum_gui.qt.util import *
|
||||||
from electrum_gui.qt.amountedit import AmountEdit
|
from electrum_gui.qt.amountedit import AmountEdit
|
||||||
|
|
||||||
class ExchangeBase:
|
class ExchangeBase:
|
||||||
history = {}
|
def __init__(self, sig):
|
||||||
quotes = {}
|
self.history = {}
|
||||||
|
self.quotes = {}
|
||||||
|
self.sig = sig
|
||||||
|
|
||||||
def get_json(self, site, get_string):
|
def get_json(self, site, get_string):
|
||||||
response = requests.request('GET', 'https://' + site + get_string,
|
response = requests.request('GET', 'https://' + site + get_string,
|
||||||
|
@ -35,6 +37,7 @@ class ExchangeBase:
|
||||||
|
|
||||||
def update(self, ccy):
|
def update(self, ccy):
|
||||||
self.quotes = self.get_rates(ccy)
|
self.quotes = self.get_rates(ccy)
|
||||||
|
self.sig.emit(SIGNAL('fx_quotes'))
|
||||||
return self.quotes
|
return self.quotes
|
||||||
|
|
||||||
def history_ccys(self):
|
def history_ccys(self):
|
||||||
|
@ -43,6 +46,7 @@ class ExchangeBase:
|
||||||
def set_history(self, ccy, history):
|
def set_history(self, ccy, history):
|
||||||
'''History is a map of "%Y-%m-%d" strings to values'''
|
'''History is a map of "%Y-%m-%d" strings to values'''
|
||||||
self.history[ccy] = history
|
self.history[ccy] = history
|
||||||
|
self.sig.emit(SIGNAL("fx_history"))
|
||||||
|
|
||||||
def get_historical_rates(self, ccy):
|
def get_historical_rates(self, ccy):
|
||||||
result = self.history.get(ccy)
|
result = self.history.get(ccy)
|
||||||
|
@ -183,15 +187,18 @@ class Plugin(BasePlugin, ThreadJob):
|
||||||
|
|
||||||
def __init__(self, parent, config, name):
|
def __init__(self, parent, config, name):
|
||||||
BasePlugin.__init__(self, parent, config, name)
|
BasePlugin.__init__(self, parent, config, name)
|
||||||
|
# Signal object first
|
||||||
|
self.sig = QObject()
|
||||||
|
self.sig.connect(self.sig, SIGNAL('fx_quotes'), self.on_fx_quotes)
|
||||||
|
self.sig.connect(self.sig, SIGNAL('fx_history'), self.on_fx_history)
|
||||||
|
self.ccy_combo = None
|
||||||
|
|
||||||
is_exchange = lambda obj: (inspect.isclass(obj)
|
is_exchange = lambda obj: (inspect.isclass(obj)
|
||||||
and issubclass(obj, ExchangeBase))
|
and issubclass(obj, ExchangeBase))
|
||||||
self.exchanges = dict(inspect.getmembers(sys.modules[__name__],
|
self.exchanges = dict(inspect.getmembers(sys.modules[__name__],
|
||||||
is_exchange))
|
is_exchange))
|
||||||
self.set_exchange(self.config_exchange())
|
self.set_exchange(self.config_exchange())
|
||||||
self.currencies = [self.fiat_unit()]
|
|
||||||
self.btc_rate = Decimal("0.0")
|
self.btc_rate = Decimal("0.0")
|
||||||
self.get_historical_rates()
|
|
||||||
self.timeout = 0
|
|
||||||
|
|
||||||
def thread_jobs(self):
|
def thread_jobs(self):
|
||||||
return [self]
|
return [self]
|
||||||
|
@ -201,7 +208,6 @@ class Plugin(BasePlugin, ThreadJob):
|
||||||
if self.parent.windows and self.timeout <= time.time():
|
if self.parent.windows and self.timeout <= time.time():
|
||||||
self.timeout = time.time() + 150
|
self.timeout = time.time() + 150
|
||||||
rates = self.exchange.update(self.fiat_unit())
|
rates = self.exchange.update(self.fiat_unit())
|
||||||
self.set_currencies(rates)
|
|
||||||
self.refresh_fields()
|
self.refresh_fields()
|
||||||
|
|
||||||
def config_exchange(self):
|
def config_exchange(self):
|
||||||
|
@ -216,16 +222,51 @@ class Plugin(BasePlugin, ThreadJob):
|
||||||
self.print_error("using exchange", name)
|
self.print_error("using exchange", name)
|
||||||
if self.config_exchange() != name:
|
if self.config_exchange() != name:
|
||||||
self.config.set_key('use_exchange', name, True)
|
self.config.set_key('use_exchange', name, True)
|
||||||
self.exchange = class_()
|
self.exchange = class_(self.sig)
|
||||||
|
# A new exchange means new fx quotes, initially empty. Force
|
||||||
|
# a quote refresh
|
||||||
|
self.timeout = 0
|
||||||
|
self.get_historical_rates()
|
||||||
|
self.on_fx_quotes()
|
||||||
|
|
||||||
|
def update_status_bars(self):
|
||||||
|
'''Update status bar fiat balance in all windows'''
|
||||||
|
for window in self.parent.windows:
|
||||||
|
window.update_status()
|
||||||
|
|
||||||
def on_new_window(self, window):
|
def on_new_window(self, window):
|
||||||
window.connect(window, SIGNAL("refresh_currencies()"),
|
|
||||||
window.update_status)
|
|
||||||
window.fx_fields = {}
|
window.fx_fields = {}
|
||||||
self.add_send_edit(window)
|
self.add_send_edit(window)
|
||||||
self.add_receive_edit(window)
|
self.add_receive_edit(window)
|
||||||
window.update_status()
|
window.update_status()
|
||||||
|
|
||||||
|
def on_fx_history(self):
|
||||||
|
'''Called when historical fx quotes are updated'''
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_fx_quotes(self):
|
||||||
|
'''Called when fresh spot fx quotes come in'''
|
||||||
|
self.update_status_bars()
|
||||||
|
self.populate_ccy_combo()
|
||||||
|
|
||||||
|
def on_ccy_combo_change(self):
|
||||||
|
'''Called when the chosen currency changes'''
|
||||||
|
ccy = str(self.ccy_combo.currentText())
|
||||||
|
if ccy != self.fiat_unit():
|
||||||
|
self.config.set_key('currency', ccy, True)
|
||||||
|
self.update_status_bars()
|
||||||
|
self.get_historical_rates()
|
||||||
|
hist_checkbox_update()
|
||||||
|
|
||||||
|
def populate_ccy_combo(self):
|
||||||
|
# There should be at most one instance of the settings dialog
|
||||||
|
combo = self.ccy_combo
|
||||||
|
# NOTE: bool(combo) is False if it is empty. Nuts.
|
||||||
|
if combo is not None:
|
||||||
|
combo.clear()
|
||||||
|
combo.addItems(self.exchange.quotes.keys())
|
||||||
|
combo.setCurrentIndex(combo.findText(self.fiat_unit()))
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
BasePlugin.close(self)
|
BasePlugin.close(self)
|
||||||
for window in self.parent.windows:
|
for window in self.parent.windows:
|
||||||
|
@ -234,12 +275,6 @@ class Plugin(BasePlugin, ThreadJob):
|
||||||
window.update_history_tab()
|
window.update_history_tab()
|
||||||
window.update_status()
|
window.update_status()
|
||||||
|
|
||||||
def set_currencies(self, currency_options):
|
|
||||||
self.currencies = sorted(currency_options)
|
|
||||||
for window in self.parent.windows:
|
|
||||||
window.emit(SIGNAL("refresh_currencies()"))
|
|
||||||
window.emit(SIGNAL("refresh_currencies_combo()"))
|
|
||||||
|
|
||||||
def exchange_rate(self):
|
def exchange_rate(self):
|
||||||
'''Returns None, or the exchange rate as a Decimal'''
|
'''Returns None, or the exchange rate as a Decimal'''
|
||||||
rate = self.exchange.quotes.get(self.fiat_unit())
|
rate = self.exchange.quotes.get(self.fiat_unit())
|
||||||
|
@ -333,42 +368,22 @@ class Plugin(BasePlugin, ThreadJob):
|
||||||
layout.addWidget(QLabel(_('Exchange rate API: ')), 0, 0)
|
layout.addWidget(QLabel(_('Exchange rate API: ')), 0, 0)
|
||||||
layout.addWidget(QLabel(_('Currency: ')), 1, 0)
|
layout.addWidget(QLabel(_('Currency: ')), 1, 0)
|
||||||
layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
|
layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
|
||||||
combo = QComboBox()
|
|
||||||
combo_ex = QComboBox()
|
|
||||||
combo_ex.addItems(sorted(self.exchanges.keys()))
|
|
||||||
combo_ex.setCurrentIndex(combo_ex.findText(self.config_exchange()))
|
|
||||||
|
|
||||||
hist_checkbox = QCheckBox()
|
# Currency list
|
||||||
ok_button = QPushButton(_("OK"))
|
self.ccy_combo = QComboBox()
|
||||||
|
self.ccy_combo.currentIndexChanged.connect(self.on_ccy_combo_change)
|
||||||
|
self.populate_ccy_combo()
|
||||||
|
|
||||||
def hist_checkbox_update():
|
def hist_checkbox_update():
|
||||||
hist_checkbox.setEnabled(self.fiat_unit() in
|
hist_checkbox.setEnabled(self.fiat_unit() in
|
||||||
self.exchange.history_ccys())
|
self.exchange.history_ccys())
|
||||||
hist_checkbox.setChecked(self.config_history())
|
hist_checkbox.setChecked(self.config_history())
|
||||||
|
|
||||||
def on_change(x):
|
|
||||||
try:
|
|
||||||
ccy = str(self.currencies[x])
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
if ccy != self.fiat_unit():
|
|
||||||
self.config.set_key('currency', ccy, True)
|
|
||||||
self.get_historical_rates()
|
|
||||||
hist_checkbox_update()
|
|
||||||
for window in self.parent.windows:
|
|
||||||
window.update_status()
|
|
||||||
|
|
||||||
def on_change_ex(idx):
|
def on_change_ex(idx):
|
||||||
exchange = str(combo_ex.currentText())
|
exchange = str(combo_ex.currentText())
|
||||||
if exchange != self.exchange.name():
|
if exchange != self.exchange.name():
|
||||||
self.set_exchange(exchange)
|
self.set_exchange(exchange)
|
||||||
self.currencies = []
|
|
||||||
combo.clear()
|
|
||||||
self.timeout = 0
|
|
||||||
hist_checkbox_update()
|
hist_checkbox_update()
|
||||||
set_currencies(combo)
|
|
||||||
for window in self.parent.windows:
|
|
||||||
window.update_status()
|
|
||||||
|
|
||||||
def on_change_hist(checked):
|
def on_change_hist(checked):
|
||||||
if checked:
|
if checked:
|
||||||
|
@ -377,44 +392,32 @@ class Plugin(BasePlugin, ThreadJob):
|
||||||
else:
|
else:
|
||||||
self.config.set_key('history_rates', 'unchecked')
|
self.config.set_key('history_rates', 'unchecked')
|
||||||
|
|
||||||
def set_currencies(combo):
|
|
||||||
try:
|
|
||||||
combo.blockSignals(True)
|
|
||||||
current_currency = self.fiat_unit()
|
|
||||||
combo.clear()
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
combo.addItems(self.currencies)
|
|
||||||
try:
|
|
||||||
index = self.currencies.index(current_currency)
|
|
||||||
except Exception:
|
|
||||||
index = 0
|
|
||||||
combo.blockSignals(False)
|
|
||||||
combo.setCurrentIndex(index)
|
|
||||||
|
|
||||||
def ok_clicked():
|
def ok_clicked():
|
||||||
if self.exchange in ["CoinDesk", "itBit"]:
|
if self.exchange in ["CoinDesk", "itBit"]:
|
||||||
self.timeout = 0
|
self.timeout = 0
|
||||||
d.accept();
|
d.accept();
|
||||||
|
|
||||||
hist_checkbox_update()
|
combo_ex = QComboBox()
|
||||||
set_currencies(combo)
|
combo_ex.addItems(sorted(self.exchanges.keys()))
|
||||||
combo.currentIndexChanged.connect(on_change)
|
combo_ex.setCurrentIndex(combo_ex.findText(self.config_exchange()))
|
||||||
combo_ex.currentIndexChanged.connect(on_change_ex)
|
combo_ex.currentIndexChanged.connect(on_change_ex)
|
||||||
|
|
||||||
|
hist_checkbox = QCheckBox()
|
||||||
|
hist_checkbox_update()
|
||||||
hist_checkbox.stateChanged.connect(on_change_hist)
|
hist_checkbox.stateChanged.connect(on_change_hist)
|
||||||
for window in self.parent.windows:
|
|
||||||
combo.connect(window, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))
|
|
||||||
combo_ex.connect(d, SIGNAL('refresh_exchanges_combo()'), lambda: set_exchanges(combo_ex))
|
combo_ex.connect(d, SIGNAL('refresh_exchanges_combo()'), lambda: set_exchanges(combo_ex))
|
||||||
|
|
||||||
|
ok_button = QPushButton(_("OK"))
|
||||||
ok_button.clicked.connect(lambda: ok_clicked())
|
ok_button.clicked.connect(lambda: ok_clicked())
|
||||||
layout.addWidget(combo,1,1)
|
|
||||||
|
layout.addWidget(self.ccy_combo,1,1)
|
||||||
layout.addWidget(combo_ex,0,1)
|
layout.addWidget(combo_ex,0,1)
|
||||||
layout.addWidget(hist_checkbox,2,1)
|
layout.addWidget(hist_checkbox,2,1)
|
||||||
layout.addWidget(ok_button,3,1)
|
layout.addWidget(ok_button,3,1)
|
||||||
|
|
||||||
if d.exec_():
|
result = d.exec_()
|
||||||
return True
|
self.ccy_combo = None
|
||||||
else:
|
return result
|
||||||
return False
|
|
||||||
|
|
||||||
def fiat_unit(self):
|
def fiat_unit(self):
|
||||||
return self.config.get("currency", "EUR")
|
return self.config.get("currency", "EUR")
|
||||||
|
|
Loading…
Add table
Reference in a new issue