URI: 
       tTorwards sane signalling for exchange_rate plugin - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit b5f986ee56bf0fe1b2dcdd4b0ede937d7321e7d5
   DIR parent 2a9bc559b2a30d7591f1012fe6d99c25206448b7
  HTML Author: Neil Booth <kyuupichan@gmail.com>
       Date:   Sat,  5 Sep 2015 21:47:35 +0900
       
       Torwards sane signalling for exchange_rate plugin
       
       Diffstat:
         M electrum                            |       4 ++--
         M lib/network.py                      |      10 ++++++----
         M lib/plugins.py                      |      20 ++++++++++----------
         M plugins/exchange_rate.py            |     131 ++++++++++++++++---------------
       
       4 files changed, 85 insertions(+), 80 deletions(-)
       ---
   DIR diff --git a/electrum b/electrum
       t@@ -526,7 +526,7 @@ if __name__ == '__main__':
        
            # daemon is not running
            if cmd_name == 'gui':
       -        network = Network(config)
       +        network = Network(config, plugins)
                network.start()
                server = NetworkServer(config, network)
                server.start()
       t@@ -540,7 +540,7 @@ if __name__ == '__main__':
                elif subcommand == 'start':
                    p = os.fork()
                    if p == 0:
       -                network = Network(config)
       +                network = Network(config, plugins)
                        network.start()
                        server = NetworkServer(config, network)
                        if config.get('websocket_server'):
   DIR diff --git a/lib/network.py b/lib/network.py
       t@@ -17,7 +17,6 @@ from bitcoin import *
        from interface import Connection, Interface
        from blockchain import Blockchain
        from version import ELECTRUM_VERSION, PROTOCOL_VERSION
       -from plugins import run_hook
        
        DEFAULT_PORTS = {'t':'50001', 's':'50002', 'h':'8081', 'g':'8082'}
        
       t@@ -137,7 +136,7 @@ class Network(util.DaemonThread):
                  stop()
            """
        
       -    def __init__(self, config=None):
       +    def __init__(self, config=None, plugins=None):
                if config is None:
                    config = {}  # Do not use mutables as default values!
                util.DaemonThread.__init__(self)
       t@@ -194,6 +193,9 @@ class Network(util.DaemonThread):
                self.socket_queue = Queue.Queue()
                self.start_network(deserialize_server(self.default_server)[2],
                                   deserialize_proxy(self.config.get('proxy')))
       +        self.plugins = plugins
       +        if self.plugins:
       +            self.plugins.set_network(self)
        
            def register_callback(self, event, callback):
                with self.lock:
       t@@ -752,7 +754,6 @@ class Network(util.DaemonThread):
                    self.process_responses(interface)
        
            def run(self):
       -        run_hook('set_network', self)
                self.blockchain.init()
                while self.is_running():
                    self.maintain_sockets()
       t@@ -762,7 +763,8 @@ class Network(util.DaemonThread):
                    self.process_pending_sends()
        
                self.stop_network()
       -        run_hook('set_network', None)
       +        if self.plugins:
       +            self.plugins.set_network(None)
                self.print_error("stopped")
        
            def on_header(self, i, header):
   DIR diff --git a/lib/plugins.py b/lib/plugins.py
       t@@ -26,13 +26,6 @@ from util import *
        from i18n import _
        from util import print_error, profiler
        
       -hook_names = set()
       -hooks = {}
       -
       -def hook(func):
       -    hook_names.add(func.func_name)
       -    return func
       -
        class Plugins:
        
            @profiler
       t@@ -126,11 +119,10 @@ class Plugins:
                x += (lambda: self.wallet_plugin_loader(config, name),)
                wallet.wallet_types.append(x)
        
       -    @hook
            def set_network(self, network):
                if network != self.network:
       -            jobs = [job in plugin.thread_jobs()
       -                    for plugin in self.plugins.values()]
       +            jobs = [job for plugin in self.plugins.values()
       +                    for job in plugin.thread_jobs()]
                    if self.network:
                        self.network.remove_jobs(jobs)
                    self.network = network
       t@@ -149,6 +141,14 @@ class Plugins:
                self.windows.remove(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):
            return _run_hook(name, False, *args)
        
   DIR diff --git a/plugins/exchange_rate.py b/plugins/exchange_rate.py
       t@@ -19,8 +19,10 @@ from electrum_gui.qt.util import *
        from electrum_gui.qt.amountedit import AmountEdit
        
        class ExchangeBase:
       -    history = {}
       -    quotes = {}
       +    def __init__(self, sig):
       +        self.history = {}
       +        self.quotes = {}
       +        self.sig = sig
        
            def get_json(self, site, get_string):
                response = requests.request('GET', 'https://' + site + get_string,
       t@@ -35,6 +37,7 @@ class ExchangeBase:
        
            def update(self, ccy):
                self.quotes = self.get_rates(ccy)
       +        self.sig.emit(SIGNAL('fx_quotes'))
                return self.quotes
        
            def history_ccys(self):
       t@@ -43,6 +46,7 @@ class ExchangeBase:
            def set_history(self, ccy, history):
                '''History is a map of "%Y-%m-%d" strings to values'''
                self.history[ccy] = history
       +        self.sig.emit(SIGNAL("fx_history"))
        
            def get_historical_rates(self, ccy):
                result = self.history.get(ccy)
       t@@ -183,15 +187,18 @@ class Plugin(BasePlugin, ThreadJob):
        
            def __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)
                                           and issubclass(obj, ExchangeBase))
                self.exchanges = dict(inspect.getmembers(sys.modules[__name__],
                                                         is_exchange))
                self.set_exchange(self.config_exchange())
       -        self.currencies = [self.fiat_unit()]
                self.btc_rate = Decimal("0.0")
       -        self.get_historical_rates()
       -        self.timeout = 0
        
            def thread_jobs(self):
                return [self]
       t@@ -201,7 +208,6 @@ class Plugin(BasePlugin, ThreadJob):
                if self.parent.windows and self.timeout <= time.time():
                    self.timeout = time.time() + 150
                    rates = self.exchange.update(self.fiat_unit())
       -            self.set_currencies(rates)
                    self.refresh_fields()
        
            def config_exchange(self):
       t@@ -216,16 +222,51 @@ class Plugin(BasePlugin, ThreadJob):
                self.print_error("using exchange", name)
                if self.config_exchange() != name:
                    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):
       -        window.connect(window, SIGNAL("refresh_currencies()"),
       -                       window.update_status)
                window.fx_fields = {}
                self.add_send_edit(window)
                self.add_receive_edit(window)
                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):
                BasePlugin.close(self)
                for window in self.parent.windows:
       t@@ -234,12 +275,6 @@ class Plugin(BasePlugin, ThreadJob):
                    window.update_history_tab()
                    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):
                '''Returns None, or the exchange rate as a Decimal'''
                rate = self.exchange.quotes.get(self.fiat_unit())
       t@@ -333,42 +368,22 @@ class Plugin(BasePlugin, ThreadJob):
                layout.addWidget(QLabel(_('Exchange rate API: ')), 0, 0)
                layout.addWidget(QLabel(_('Currency: ')), 1, 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()
       -        ok_button = QPushButton(_("OK"))
       +        # Currency list
       +        self.ccy_combo = QComboBox()
       +        self.ccy_combo.currentIndexChanged.connect(self.on_ccy_combo_change)
       +        self.populate_ccy_combo()
        
                def hist_checkbox_update():
                    hist_checkbox.setEnabled(self.fiat_unit() in
                                             self.exchange.history_ccys())
                    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):
                    exchange = str(combo_ex.currentText())
                    if exchange != self.exchange.name():
                        self.set_exchange(exchange)
       -                self.currencies = []
       -                combo.clear()
       -                self.timeout = 0
                        hist_checkbox_update()
       -                set_currencies(combo)
       -                for window in self.parent.windows:
       -                    window.update_status()
        
                def on_change_hist(checked):
                    if checked:
       t@@ -377,44 +392,32 @@ class Plugin(BasePlugin, ThreadJob):
                    else:
                        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():
                    if self.exchange in ["CoinDesk", "itBit"]:
                        self.timeout = 0
                    d.accept();
        
       -        hist_checkbox_update()
       -        set_currencies(combo)
       -        combo.currentIndexChanged.connect(on_change)
       +        combo_ex = QComboBox()
       +        combo_ex.addItems(sorted(self.exchanges.keys()))
       +        combo_ex.setCurrentIndex(combo_ex.findText(self.config_exchange()))
                combo_ex.currentIndexChanged.connect(on_change_ex)
       +
       +        hist_checkbox = QCheckBox()
       +        hist_checkbox_update()
                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))
       +
       +        ok_button = QPushButton(_("OK"))
                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(hist_checkbox,2,1)
                layout.addWidget(ok_button,3,1)
        
       -        if d.exec_():
       -            return True
       -        else:
       -            return False
       +        result = d.exec_()
       +        self.ccy_combo = None
       +        return result
        
            def fiat_unit(self):
                return self.config.get("currency", "EUR")