def __init__(self): QDialog.__init__(self) # set up ui self.__ui = Ui_Preferences() self.__ui.setupUi(self) # improve ui on mac if utilities.platform_is_mac(): self.__adjust_for_mac() # connect ui signals to logic self.__ui.lineEditKey.textChanged.connect( self.__slot_validate_credentials) self.__ui.lineEditSecret.textChanged.connect( self.__slot_validate_credentials) # initialize config parser self.configparser = RawConfigParser() # __load or (if non-existent) create config file if path.isfile(self.__FILENAME): self.__load() else: self.__init_with_defaults() self.__save() self.set_fiat_currencies([])
def __init__(self): QDialog.__init__(self) # set up ui self.__ui = Ui_Preferences() self.__ui.setupUi(self) # improve ui on mac if utilities.platform_is_mac(): self.__adjust_for_mac() # connect ui signals to logic self.__ui.lineEditPassword.textChanged.connect( self.__slot_password_changed) self.__ui.lineEditKey.textChanged.connect( self.__slot_validate_key) self.__ui.lineEditSecret.textChanged.connect( self.__slot_validate_secret) self.__ui.buttonBox.button(QDialogButtonBox.Apply).released.connect( self.__slot_validate_credentials) self.do_configfile()
def __init__(self): QDialog.__init__(self) # set up ui self.__ui = Ui_Preferences() self.__ui.setupUi(self) # improve ui on mac if utilities.platform_is_mac(): self.__adjust_for_mac() # connect ui signals to logic self.__ui.lineEditKey.textChanged.connect( self.__slot_validate_credentials) self.__ui.lineEditSecret.textChanged.connect( self.__slot_validate_credentials) # initialize config parser self.__configparser = RawConfigParser({ 'grouping': '0.0', 'currency_{}'.format(Preferences.CURRENCY_INDEX_BASE): 'BTC', 'currency_{}'.format(Preferences.CURRENCY_INDEX_QUOTE): 'USD', 'orders_column_{}'.format(Preferences.ORDERS_COLUMN_PRICE): 'True', 'orders_column_{}'.format(Preferences.ORDERS_COLUMN_SIZE): 'True', 'orders_column_{}'.format(Preferences.ORDERS_COLUMN_QUOTE): 'False', # @IgnorePep8 'orders_column_{}'.format(Preferences.ORDERS_COLUMN_TOTAL_SIZE): 'True', 'orders_column_{}'.format(Preferences.ORDERS_COLUMN_TOTAL_QUOTE): 'False', # @IgnorePep8 'key': '', 'secret': '', 'proposed_pips': '0' }) # load config file (if exists) if path.isfile(self.__FILENAME): self.__load() else: self.__configparser.add_section(self.__SECTION_GLOBAL) self.__save() self.set_fiat_currencies([])
class Preferences(QDialog): ''' Represents the application preferences. ''' CURRENCY_INDEX_BASE = 1 CURRENCY_INDEX_QUOTE = 2 __PASSPHRASE = 'fffuuuuuuu' __FILENAME = 'goxgui.ini' __SECTION_GLOBAL = 'Global' def __init__(self): QDialog.__init__(self) # set up ui self.__ui = Ui_Preferences() self.__ui.setupUi(self) # improve ui on mac if utilities.platform_is_mac(): self.__adjust_for_mac() # connect ui signals to logic self.__ui.lineEditKey.textChanged.connect( self.__slot_validate_credentials) self.__ui.lineEditSecret.textChanged.connect( self.__slot_validate_credentials) # initialize config parser self.configparser = RawConfigParser() # __load or (if non-existent) create config file if path.isfile(self.__FILENAME): self.__load() else: self.__init_with_defaults() self.__save() self.set_fiat_currencies([]) # start slots def __slot_validate_credentials(self): key = str(self.__ui.lineEditKey.text()) secret = str(self.__ui.lineEditSecret.text()) # empty credentials are allowed if key == '' and secret == '': self.__enable_ok() return try: utilities.assert_valid_key(key) except Exception as e: self.__disable_ok('Invalid key.'.format(str(e))) return try: utilities.assert_valid_secret(secret) except Exception as e: self.__disable_ok('Invalid secret.'.format(str(e))) return self.__enable_ok() # end slots # start private methods def get_fiat_currency_index(self, other): ''' Returns the index of the given currency in the fiat currency list. ''' index = 0 for currency in self.__fiat_currencies: if currency == other: return index index += 1 raise Exception('Currency {} not found.'.format(other.symbol)) def __init_with_defaults(self): self.configparser.add_section(self.__SECTION_GLOBAL) self.set_currency(Preferences.CURRENCY_INDEX_BASE, Currency('BTC')) self.set_currency(Preferences.CURRENCY_INDEX_QUOTE, Currency('USD')) self.__set_key('') self.__set_secret('') def __load_to_gui(self): self.__ui.lineEditKey.setText(self.get_key()) self.__ui.lineEditSecret.setText(self.get_secret()) quoteCurrency = self.get_currency(Preferences.CURRENCY_INDEX_QUOTE) index = self.get_fiat_currency_index(quoteCurrency) self.__ui.comboBoxCurrency.setCurrentIndex(index) self.__set_status('') def __save_from_gui(self): self.__set_key(str(self.__ui.lineEditKey.text())) self.__set_secret(str(self.__ui.lineEditSecret.text())) quoteCurrency = Currency(str(self.__ui.comboBoxCurrency.currentText())) self.set_currency(Preferences.CURRENCY_INDEX_QUOTE, quoteCurrency) def __disable_ok(self, text): self.__ui.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) self.__set_status(text) def __enable_ok(self): self.__ui.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True) self.__set_status('') def __save(self): ''' Saves the config to the .ini file ''' with open(self.__FILENAME, 'wb') as configfile: self.configparser.write(configfile) def __load(self): ''' Loads or reloads the config from the .ini file ''' self.configparser.read(self.__FILENAME) def __set_key(self, key): ''' Writes the specified key to the configuration file. ''' self.__set('key', key) def __set_secret(self, secret): ''' Writes the specified secret to the configuration file (encrypted). ''' if secret != '': secret = utilities.encrypt(secret, Preferences.__PASSPHRASE) self.__set('secret', secret) def __set_status(self, text): self.__ui.labelStatus.setText(text) def __adjust_for_mac(self): ''' Fixes some stuff that looks good on windows but bad on mac. ''' # the default fixed fontA is unreadable on mac, so replace it fontA = QtGui.QFont('Monaco', 11) self.__ui.lineEditPassword.setFont(fontA) self.__ui.lineEditKey.setFont(fontA) self.__ui.lineEditSecret.setFont(fontA) # the default label font is too big on mac fontB = QtGui.QFont('Lucida Grande', 11) self.__ui.labelPassword.setFont(fontB) self.__ui.labelKeySecret.setFont(fontB) self.__ui.labelCurrency.setFont(fontB) def __get(self, key): ''' Retrieves a property from the global section ''' return self.configparser.get(self.__SECTION_GLOBAL, key) def __set(self, key, value): ''' Stores a property to the global section ''' self.configparser.set(self.__SECTION_GLOBAL, key, value) # end private methods # start public methods def set_fiat_currencies(self, currencies): ''' Sets the fiat currencies available in the preferences dialog. ''' self.__fiat_currencies = currencies self.__ui.comboBoxCurrency.clear() index = 0 for x in self.__fiat_currencies: self.__ui.comboBoxCurrency.insertItem(index, x.symbol) index += 1 def set_currency(self, index, currency): return self.__set('currency_{}'.format(index), currency.symbol) def get_currency(self, index): return Currency(self.__get('currency_{}'.format(index))) def get_key(self): ''' Loads the key from the configuration file. ''' return self.__get('key') def get_secret(self): ''' Loads the secret from the configuration file. ''' secret = self.__get('secret') if secret == '': return secret return utilities.decrypt(secret, Preferences.__PASSPHRASE) def show(self): ''' Shows the preference dialog. @return: True if the user accepted, false otherwise ''' self.__load_to_gui() result = self.exec_() return result == QDialog.Accepted def apply(self): ''' Applies the user changes. Changes made by the user during show() do not propagate until apply() is called. ''' self.__save_from_gui() self.__save()
class Preferences(QDialog): ''' Represents the application preferences. ''' GROUP_ORDERS = 0.6 DEFAULT_PASSWORD = '******' FILENAME = 'goxgui.ini' FIAT_CURRENCIES = [ 'USD', 'EUR', 'JPY', 'CAD', 'GBP', 'CHF', 'RUB', 'AUD', 'SEK', 'DKK', 'HKD', 'PLN', 'CNY', 'SGD', 'THB', 'NZD', 'NOK' ] def __init__(self): QDialog.__init__(self) # set up ui self.__ui = Ui_Preferences() self.__ui.setupUi(self) # improve ui on mac if utilities.platform_is_mac(): self.__adjust_for_mac() # connect ui signals to logic self.__ui.lineEditPassword.textChanged.connect( self.__slot_password_changed) self.__ui.lineEditKey.textChanged.connect( self.__slot_validate_key) self.__ui.lineEditSecret.textChanged.connect( self.__slot_validate_secret) self.__ui.buttonBox.button(QDialogButtonBox.Apply).released.connect( self.__slot_validate_credentials) self.do_configfile() def get_fiat_currency_index(self, other): ''' Returns the index of the given currency in the fiat currency list. ''' index = 0 for currency in self.FIAT_CURRENCIES: if currency == other: return index index += 1 raise Exception('Currency {} not found.'.format(other)) def do_configfile(self): # initialize config parser self.configparser = RawConfigParser() # __load or (if non-existent) create config file if path.isfile(self.FILENAME): self.__load() else: self.__init_with_defaults() self.save() self.GROUP_ORDERS = float(self.configparser.get("goxgui","group_orders")) self.ignore_channels = self.configparser.get("goxgui","ignore_channels").split() # start slots def __slot_password_changed(self): self.__disable_ok('Password Changed...') return def __slot_validate_key(self,key): try: utilities.assert_valid_key(str(key)) self.__set_status('Key is valid.') except Exception as e: self.__disable_ok('Invalid key. %s' % e) return 1 def __slot_validate_secret(self,secret): try: utilities.assert_valid_secret(str(secret)) self.__set_status('Secret is valid.') except Exception as e: self.__disable_ok('Invalid secret. %s' % e) return 1 def __slot_validate_credentials(self): key = str(self.__ui.lineEditKey.text()) secret = str(self.__ui.lineEditSecret.text()) # empty credentials are allowed if key == '' and secret == '': self.__enable_ok() self.__set_status("Credentials are Empty. Allowed.") return if not self.__slot_validate_key(key) and not self.__slot_validate_secret(secret): self.__enable_ok() self.__set_status("Credentials are Valid. Click OK to Save and reload.") # end slots # start private methods def __init_with_defaults(self): self.configparser.add_section('goxgui') self.set('key', '') self.set('secret', '') self.set('password', self.DEFAULT_PASSWORD) self.set('group_orders', self.GROUP_ORDERS) self.set('ignore_channels','') def __load_to_gui(self): self.do_configfile() self.__ui.lineEditKey.setText(self.get('key')) self.__ui.lineEditSecret.setText(self.decrypt_secret()) self.__ui.lineEditPassword.setText(self.get('password')) self.__set_status('') #populate the Various Settings, Currency tab #populate combo boxes with currencies: self.__ui.comboBoxCurrencyFiat.clear() self.__ui.comboBoxCurrencyFiat.addItems(self.FIAT_CURRENCIES) self.__ui.comboBoxCurrencyFiat.setCurrentIndex(self.get_fiat_currency_index(self.configparser.get("gox","quote_currency"))) self.__ui.comboBoxCurrencyTarget.clear() self.__ui.comboBoxCurrencyTarget.addItem(self.configparser.get("gox","base_currency")) #set default order grouping self.__ui.doubleSpinBoxGROUPORDERS.setValue(self.GROUP_ORDERS) self.GROUP_ORDERS = self.__ui.doubleSpinBoxGROUPORDERS.value() def __save_from_gui(self): #save settings to the configparser self.set('key',str(self.__ui.lineEditKey.text())) password = str(self.__ui.lineEditPassword.text()) if not password: password = self.DEFAULT_PASSWORD self.__set_encrypted_secret( str(self.__ui.lineEditSecret.text()),password) self.set('password',password) #save currency settings self.configparser.set("gox","quote_currency",str(self.__ui.comboBoxCurrencyFiat.currentText())) self.configparser.set("gox","base_currency",str(self.__ui.comboBoxCurrencyTarget.currentText())) #save order grouping settings self.configparser.set("goxgui","group_orders",str(self.__ui.doubleSpinBoxGROUPORDERS.value())) self.GROUP_ORDERS = self.__ui.doubleSpinBoxGROUPORDERS.value() def __disable_ok(self, text): self.__ui.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) self.__set_status(text) def __enable_ok(self): self.__ui.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True) self.__set_status('') def save(self): ''' Saves the config to the .ini file ''' with open(self.FILENAME, 'wb') as configfile: self.configparser.write(configfile) def __load(self): ''' Loads or reloads the config from the .ini file ''' self.configparser.read(self.FILENAME) def __set_key(self, key): ''' Writes the specified key to the configuration file. ''' self.set('key', key) def __set_encrypted_secret(self, secret, password): ''' Writes the specified secret to the configuration file (encrypted). ''' if secret != '': secret = utilities.encrypt(secret, password) self.set('secret', secret) def __set_status(self, text): self.__ui.labelStatus.setText(text) def __adjust_for_mac(self): ''' Fixes some stuff that looks good on windows but bad on mac. ''' # the default fixed fontA is unreadable on mac, so replace it fontA = QtGui.QFont('Monaco', 11) self.__ui.lineEditPassword.setFont(fontA) self.__ui.lineEditKey.setFont(fontA) self.__ui.lineEditSecret.setFont(fontA) # the default label font is too big on mac fontB = QtGui.QFont('Lucida Grande', 11) self.__ui.labelPassword.setFont(fontB) self.__ui.labelKeySecret.setFont(fontB) # end private methods # start public methods def get(self, key): ''' Retrieves a property from the global section ''' return self.configparser.get('goxgui', key) def set(self, key, value): ''' Stores a property to the global section ''' self.configparser.set('goxgui', key, value) def decrypt_secret(self,password=''): secret = self.get('secret') if secret == '': return '' if password == '': password = self.get('password') return utilities.decrypt(secret, password) def show(self): ''' Shows the preference dialog. @return: True if the user accepted, false otherwise ''' self.__load_to_gui() result = self.exec_() return result == QDialog.Accepted def apply(self): ''' Applies the user changes. Changes made by the user during show() do not propagate until apply() is called. ''' self.__save_from_gui() self.save()