From f5a143d0b83387b2cad2f320f7067d95375ca498 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 27 Jun 2017 13:19:12 +0200 Subject: [PATCH] Config validator - config options can have validators - limit maxoutboundconnections to max 8 --- src/bitmessageqt/settings.py | 2 +- src/bmconfigparser.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index 300c3544..4342fd09 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -165,7 +165,7 @@ class Ui_settingsDialog(object): self.lineEditMaxOutboundConnections.setSizePolicy(sizePolicy) self.lineEditMaxOutboundConnections.setMaximumSize(QtCore.QSize(60, 16777215)) self.lineEditMaxOutboundConnections.setObjectName(_fromUtf8("lineEditMaxOutboundConnections")) - self.lineEditMaxOutboundConnections.setValidator(QtGui.QIntValidator(0, 4096, self.lineEditMaxOutboundConnections)) + self.lineEditMaxOutboundConnections.setValidator(QtGui.QIntValidator(0, 8, self.lineEditMaxOutboundConnections)) self.gridLayout_9.addWidget(self.lineEditMaxOutboundConnections, 2, 2, 1, 1) self.gridLayout_4.addWidget(self.groupBox_3, 2, 0, 1, 1) self.groupBox_2 = QtGui.QGroupBox(self.tabNetworkSettings) diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index 6725a4d8..913df9c0 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -37,6 +37,8 @@ class BMConfigParser(ConfigParser.SafeConfigParser): if self._optcre is self.OPTCRE or value: if not isinstance(value, basestring): raise TypeError("option values must be strings") + if not self.validate(section, option, value): + raise ValueError("Invalid value %s" % str(value)) return ConfigParser.ConfigParser.set(self, section, option, value) def get(self, section, option, raw=False, variables=None): @@ -76,6 +78,20 @@ class BMConfigParser(ConfigParser.SafeConfigParser): def addresses(self): return filter(lambda x: x.startswith('BM-'), BMConfigParser().sections()) + def read(self, filenames): + ConfigParser.ConfigParser.read(self, filenames) + for section in self.sections(): + for option in self.options(section): + try: + if not self.validate(section, option, ConfigParser.ConfigParser.get(self, section, option)): + try: + newVal = BMConfigDefaults[section][option] + except KeyError: + continue + ConfigParser.ConfigParser.set(self, section, option, newVal) + except ConfigParser.InterpolationError: + continue + def save(self): fileName = os.path.join(state.appdata, 'keys.dat') fileNameBak = fileName + "." + datetime.datetime.now().strftime("%Y%j%H%M%S%f") + '.bak' @@ -93,3 +109,18 @@ class BMConfigParser(ConfigParser.SafeConfigParser): # delete the backup if fileNameExisted: os.remove(fileNameBak) + + def validate(self, section, option, value): + try: + return getattr(self, "validate_%s_%s" % (section, option))(value) + except AttributeError: + return True + + def validate_bitmessagesettings_maxoutboundconnections(self, value): + try: + value = int(value) + except ValueError: + return False + if value < 0 or value > 8: + return False + return True