Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions src/bitmessageqt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
import queues
import state
import widgets
from bmconfigparser import config
from bmconfigparser import config as config_obj
from helper_sql import sqlExecute, sqlStoredProcedure
from helper_startup import start_proxyconfig
from network import knownnodes, AnnounceThread
from network.asyncore_pollchoose import set_rates
from tr import _translate


def getSOCKSProxyType(config_):
def getSOCKSProxyType(config):
"""Get user socksproxytype setting from *config*"""
try:
result = ConfigParser.SafeConfigParser.get(
config_, 'bitmessagesettings', 'socksproxytype')
config, 'bitmessagesettings', 'socksproxytype')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
return None
else:
Expand All @@ -45,7 +45,7 @@ def __init__(self, parent=None, firstrun=False):

self.parent = parent
self.firstrun = firstrun
self.config = config
self.config = config_obj
self.net_restart_needed = False
self.timer = QtCore.QTimer()

Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(self, parent=None, firstrun=False):
)
QtGui.QWidget.resize(self, QtGui.QWidget.sizeHint(self))

def adjust_from_config(self, config_):
def adjust_from_config(self, config):
"""Adjust all widgets state according to config settings"""
# pylint: disable=too-many-branches,too-many-statements
if not self.parent.tray.isSystemTrayAvailable():
Expand All @@ -89,31 +89,31 @@ def adjust_from_config(self, config_):
"MainWindow", "Tray (not available in your system)"))
for setting in (
'minimizetotray', 'trayonclose', 'startintray'):
config_.set('bitmessagesettings', setting, 'false')
config.set('bitmessagesettings', setting, 'false')
else:
self.checkBoxMinimizeToTray.setChecked(
config_.getboolean('bitmessagesettings', 'minimizetotray'))
config.getboolean('bitmessagesettings', 'minimizetotray'))
self.checkBoxTrayOnClose.setChecked(
config_.safeGetBoolean('bitmessagesettings', 'trayonclose'))
config.safeGetBoolean('bitmessagesettings', 'trayonclose'))
self.checkBoxStartInTray.setChecked(
config_.getboolean('bitmessagesettings', 'startintray'))
config.getboolean('bitmessagesettings', 'startintray'))

self.checkBoxHideTrayConnectionNotifications.setChecked(
config_.getboolean(
config.getboolean(
'bitmessagesettings', 'hidetrayconnectionnotifications'))
self.checkBoxShowTrayNotifications.setChecked(
config_.getboolean('bitmessagesettings', 'showtraynotifications'))
config.getboolean('bitmessagesettings', 'showtraynotifications'))

self.checkBoxStartOnLogon.setChecked(
config_.getboolean('bitmessagesettings', 'startonlogon'))
config.getboolean('bitmessagesettings', 'startonlogon'))

self.checkBoxWillinglySendToMobile.setChecked(
config_.safeGetBoolean(
config.safeGetBoolean(
'bitmessagesettings', 'willinglysendtomobile'))
self.checkBoxUseIdenticons.setChecked(
config_.safeGetBoolean('bitmessagesettings', 'useidenticons'))
config.safeGetBoolean('bitmessagesettings', 'useidenticons'))
self.checkBoxReplyBelow.setChecked(
config_.safeGetBoolean('bitmessagesettings', 'replybelow'))
config.safeGetBoolean('bitmessagesettings', 'replybelow'))

if state.appdata == paths.lookupExeFolder():
self.checkBoxPortableMode.setChecked(True)
Expand Down Expand Up @@ -142,57 +142,57 @@ def adjust_from_config(self, config_):

# On the Network settings tab:
self.lineEditTCPPort.setText(str(
config_.get('bitmessagesettings', 'port')))
config.get('bitmessagesettings', 'port')))
self.checkBoxUPnP.setChecked(
config_.safeGetBoolean('bitmessagesettings', 'upnp'))
config.safeGetBoolean('bitmessagesettings', 'upnp'))
self.checkBoxUDP.setChecked(
config_.safeGetBoolean('bitmessagesettings', 'udp'))
config.safeGetBoolean('bitmessagesettings', 'udp'))
self.checkBoxAuthentication.setChecked(
config_.getboolean('bitmessagesettings', 'socksauthentication'))
config.getboolean('bitmessagesettings', 'socksauthentication'))
self.checkBoxSocksListen.setChecked(
config_.getboolean('bitmessagesettings', 'sockslisten'))
config.getboolean('bitmessagesettings', 'sockslisten'))
self.checkBoxOnionOnly.setChecked(
config_.safeGetBoolean('bitmessagesettings', 'onionservicesonly'))
config.safeGetBoolean('bitmessagesettings', 'onionservicesonly'))

self._proxy_type = getSOCKSProxyType(config_)
self._proxy_type = getSOCKSProxyType(config)
self.comboBoxProxyType.setCurrentIndex(
0 if not self._proxy_type
else self.comboBoxProxyType.findText(self._proxy_type))
self.comboBoxProxyTypeChanged(self.comboBoxProxyType.currentIndex())

self.lineEditSocksHostname.setText(
config_.get('bitmessagesettings', 'sockshostname'))
config.get('bitmessagesettings', 'sockshostname'))
self.lineEditSocksPort.setText(str(
config_.get('bitmessagesettings', 'socksport')))
config.get('bitmessagesettings', 'socksport')))
self.lineEditSocksUsername.setText(
config_.get('bitmessagesettings', 'socksusername'))
config.get('bitmessagesettings', 'socksusername'))
self.lineEditSocksPassword.setText(
config_.get('bitmessagesettings', 'sockspassword'))
config.get('bitmessagesettings', 'sockspassword'))

self.lineEditMaxDownloadRate.setText(str(
config_.get('bitmessagesettings', 'maxdownloadrate')))
config.get('bitmessagesettings', 'maxdownloadrate')))
self.lineEditMaxUploadRate.setText(str(
config_.get('bitmessagesettings', 'maxuploadrate')))
config.get('bitmessagesettings', 'maxuploadrate')))
self.lineEditMaxOutboundConnections.setText(str(
config_.get('bitmessagesettings', 'maxoutboundconnections')))
config.get('bitmessagesettings', 'maxoutboundconnections')))

# Demanded difficulty tab
self.lineEditTotalDifficulty.setText(str((float(
config_.getint(
config.getint(
'bitmessagesettings', 'defaultnoncetrialsperbyte')
) / defaults.networkDefaultProofOfWorkNonceTrialsPerByte)))
self.lineEditSmallMessageDifficulty.setText(str((float(
config_.getint(
config.getint(
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
) / defaults.networkDefaultPayloadLengthExtraBytes)))

# Max acceptable difficulty tab
self.lineEditMaxAcceptableTotalDifficulty.setText(str((float(
config_.getint(
config.getint(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte')
) / defaults.networkDefaultProofOfWorkNonceTrialsPerByte)))
self.lineEditMaxAcceptableSmallMessageDifficulty.setText(str((float(
config_.getint(
config.getint(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes')
) / defaults.networkDefaultPayloadLengthExtraBytes)))

Expand All @@ -203,21 +203,21 @@ def adjust_from_config(self, config_):
self.comboBoxOpenCL.addItems(openclpow.vendors)
self.comboBoxOpenCL.setCurrentIndex(0)
for i in range(self.comboBoxOpenCL.count()):
if self.comboBoxOpenCL.itemText(i) == config_.safeGet(
if self.comboBoxOpenCL.itemText(i) == config.safeGet(
'bitmessagesettings', 'opencl'):
self.comboBoxOpenCL.setCurrentIndex(i)
break

# Namecoin integration tab
nmctype = config_.get('bitmessagesettings', 'namecoinrpctype')
nmctype = config.get('bitmessagesettings', 'namecoinrpctype')
self.lineEditNamecoinHost.setText(
config_.get('bitmessagesettings', 'namecoinrpchost'))
config.get('bitmessagesettings', 'namecoinrpchost'))
self.lineEditNamecoinPort.setText(str(
config_.get('bitmessagesettings', 'namecoinrpcport')))
config.get('bitmessagesettings', 'namecoinrpcport')))
self.lineEditNamecoinUser.setText(
config_.get('bitmessagesettings', 'namecoinrpcuser'))
config.get('bitmessagesettings', 'namecoinrpcuser'))
self.lineEditNamecoinPassword.setText(
config_.get('bitmessagesettings', 'namecoinrpcpassword'))
config.get('bitmessagesettings', 'namecoinrpcpassword'))

if nmctype == "namecoind":
self.radioButtonNamecoinNamecoind.setChecked(True)
Expand All @@ -232,9 +232,9 @@ def adjust_from_config(self, config_):

# Message Resend tab
self.lineEditDays.setText(str(
config_.get('bitmessagesettings', 'stopresendingafterxdays')))
config.get('bitmessagesettings', 'stopresendingafterxdays')))
self.lineEditMonths.setText(str(
config_.get('bitmessagesettings', 'stopresendingafterxmonths')))
config.get('bitmessagesettings', 'stopresendingafterxmonths')))

def comboBoxProxyTypeChanged(self, comboBoxIndex):
"""A callback for currentIndexChanged event of comboBoxProxyType"""
Expand Down
67 changes: 32 additions & 35 deletions src/bmconfigparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
import shutil
import sys # FIXME: bad style! write more generally
from threading import Event
from datetime import datetime

Expand Down Expand Up @@ -36,48 +35,52 @@ def set(self, section, option, value=None):
raise ValueError("Invalid value %s" % value)
return SafeConfigParser.set(self, section, option, value)

def get(self, section, option, **kwargs):
"""Try returning temporary value before using parent get()"""
try:
return self._temp[section][option]
except KeyError:
pass
return SafeConfigParser.get(
self, section, option, **kwargs)

def setTemp(self, section, option, value=None):
"""Temporary set option to value, not saving."""
try:
self._temp[section][option] = value
except KeyError:
self._temp[section] = {option: value}

def safeGetBoolean(self, section, field):
def safeGetBoolean(self, section, option):
"""Return value as boolean, False on exceptions"""
try:
# Used in the python2.7
# return self.getboolean(section, field)
# Used in the python3.5.2
# print(config, section, field)
return self.getboolean(section, field)
return self.getboolean(section, option)
except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError):
return False

def safeGetInt(self, section, field, default=0):
def safeGetInt(self, section, option, default=0):
"""Return value as integer, default on exceptions,
0 if default missing"""
try:
# Used in the python2.7
# return self.getint(section, field)
# Used in the python3.7.0
return int(self.get(section, field))
return int(self.get(section, option))
except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError):
return default

def safeGetFloat(self, section, field, default=0.0):
def safeGetFloat(self, section, option, default=0.0):
"""Return value as float, default on exceptions,
0.0 if default missing"""
try:
return self.getfloat(section, field)
return self.getfloat(section, option)
except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError):
return default

def safeGet(self, section, option, default=None):
"""Return value as is, default on exceptions, None if default missing"""
"""
Return value as is, default on exceptions, None if default missing
"""
try:
return self.get(section, option)
except (configparser.NoSectionError, configparser.NoOptionError,
Expand All @@ -91,34 +94,25 @@ def items(self, section, raw=False, variables=None):
return SafeConfigParser.items(self, section, True, variables)

def _reset(self):
"""Reset current config. There doesn't appear to be a built in
method for this"""
"""
Reset current config.
There doesn't appear to be a built in method for this.
"""
self._temp = {}
sections = self.sections()
for x in sections:
self.remove_section(x)

def read(self, filenames=None):
self._reset()
SafeConfigParser.read(self, os.path.join(os.path.dirname(__file__), 'default.ini'))
SafeConfigParser.read(
self, os.path.join(os.path.dirname(__file__), 'default.ini'))
if filenames:
SafeConfigParser.read(self, filenames)

if sys.version_info[0] == 3:
@staticmethod
def addresses(hidden=False):
"""Return a list of local bitmessage addresses (from section labels)"""
return [x for x in config.sections() if x.startswith('BM-') and (
hidden or not config.safeGetBoolean(x, 'hidden'))]

def readfp(self, fp, filename=None):
# pylint: disable=no-member
SafeConfigParser.read_file(self, fp)
else:
@staticmethod
def addresses():
"""Return a list of local bitmessage addresses (from section labels)"""
return [
x for x in config.sections() if x.startswith('BM-')]
def addresses(self):
"""Return a list of local bitmessage addresses (from section labels)"""
return [x for x in self.sections() if x.startswith('BM-')]

def save(self):
"""Save the runtime config onto the filesystem"""
Expand Down Expand Up @@ -161,4 +155,7 @@ def validate_bitmessagesettings_maxoutboundconnections(value):
return True


config = BMConfigParser()
if not getattr(BMConfigParser, 'read_file', False):
BMConfigParser.read_file = BMConfigParser.readfp

config = BMConfigParser() # TODO: remove this crutch
Loading