2018-04-19 13:02:12 +02:00
|
|
|
"""
|
|
|
|
BMConfigParser class definition and default configuration settings
|
|
|
|
"""
|
|
|
|
|
2017-01-15 10:50:02 +01:00
|
|
|
import os
|
2020-01-24 15:03:13 +01:00
|
|
|
import shutil
|
2020-11-06 10:58:36 +01:00
|
|
|
import sys # FIXME: bad style! write more generally
|
2022-01-28 13:55:23 +01:00
|
|
|
from threading import Event
|
2018-04-19 13:02:12 +02:00
|
|
|
from datetime import datetime
|
2016-12-06 10:47:39 +01:00
|
|
|
|
2020-11-06 10:58:36 +01:00
|
|
|
from six import string_types
|
|
|
|
from six.moves import configparser
|
|
|
|
|
2021-06-11 19:14:57 +02:00
|
|
|
try:
|
|
|
|
import state
|
|
|
|
except ImportError:
|
|
|
|
from pybitmessage import state
|
2020-11-06 10:58:36 +01:00
|
|
|
|
|
|
|
SafeConfigParser = configparser.SafeConfigParser
|
2022-01-28 13:55:23 +01:00
|
|
|
config_ready = Event()
|
2020-11-06 10:58:36 +01:00
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
|
2021-03-16 10:48:38 +01:00
|
|
|
class BMConfigParser(SafeConfigParser):
|
2019-10-16 16:53:37 +02:00
|
|
|
"""
|
2020-11-06 10:58:36 +01:00
|
|
|
Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
|
|
|
|
with additional methods specific to bitmessage config.
|
2019-10-16 16:53:37 +02:00
|
|
|
"""
|
2020-01-17 14:27:36 +01:00
|
|
|
# pylint: disable=too-many-ancestors
|
2019-09-05 17:31:16 +02:00
|
|
|
_temp = {}
|
|
|
|
|
2016-12-06 10:47:39 +01:00
|
|
|
def set(self, section, option, value=None):
|
2016-12-06 11:01:17 +01:00
|
|
|
if self._optcre is self.OPTCRE or value:
|
2020-11-06 10:58:36 +01:00
|
|
|
if not isinstance(value, string_types):
|
2016-12-06 11:01:17 +01:00
|
|
|
raise TypeError("option values must be strings")
|
2017-06-27 13:19:12 +02:00
|
|
|
if not self.validate(section, option, value):
|
2018-04-19 13:02:12 +02:00
|
|
|
raise ValueError("Invalid value %s" % value)
|
2020-11-06 10:58:36 +01:00
|
|
|
return SafeConfigParser.set(self, section, option, value)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2022-02-16 21:55:17 +01:00
|
|
|
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)
|
|
|
|
|
2019-09-05 17:31:16 +02:00
|
|
|
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}
|
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
def safeGetBoolean(self, section, field):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Return value as boolean, False on exceptions"""
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2021-03-04 15:15:41 +01:00
|
|
|
# Used in the python2.7
|
|
|
|
# return self.getboolean(section, field)
|
|
|
|
# Used in the python3.5.2
|
|
|
|
# print(config, section, field)
|
2017-05-15 12:18:07 +02:00
|
|
|
return self.getboolean(section, field)
|
2020-11-06 10:58:36 +01:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
2018-04-19 13:02:12 +02:00
|
|
|
ValueError, AttributeError):
|
2017-05-15 12:18:07 +02:00
|
|
|
return False
|
2017-01-11 14:27:19 +01:00
|
|
|
|
2017-02-26 17:46:02 +01:00
|
|
|
def safeGetInt(self, section, field, default=0):
|
2020-01-17 14:27:36 +01:00
|
|
|
"""Return value as integer, default on exceptions,
|
|
|
|
0 if default missing"""
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2021-03-04 15:15:41 +01:00
|
|
|
# Used in the python2.7
|
|
|
|
# return self.getint(section, field)
|
|
|
|
# Used in the python3.7.0
|
|
|
|
return int(self.get(section, field))
|
2020-11-06 10:58:36 +01:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
2018-04-19 13:02:12 +02:00
|
|
|
ValueError, AttributeError):
|
2017-05-15 12:18:07 +02:00
|
|
|
return default
|
2017-01-14 23:18:06 +01:00
|
|
|
|
2021-08-16 17:35:04 +02:00
|
|
|
def safeGetFloat(self, section, field, default=0.0):
|
|
|
|
"""Return value as float, default on exceptions,
|
|
|
|
0.0 if default missing"""
|
|
|
|
try:
|
|
|
|
return self.getfloat(section, field)
|
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
|
|
|
ValueError, AttributeError):
|
|
|
|
return default
|
|
|
|
|
2018-04-19 13:02:12 +02:00
|
|
|
def safeGet(self, section, option, default=None):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Return value as is, default on exceptions, None if default missing"""
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2017-01-11 14:27:19 +01:00
|
|
|
return self.get(section, option)
|
2020-11-06 10:58:36 +01:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
2018-04-19 13:02:12 +02:00
|
|
|
ValueError, AttributeError):
|
2017-01-11 14:27:19 +01:00
|
|
|
return default
|
|
|
|
|
2020-01-15 11:47:26 +01:00
|
|
|
def items(self, section, raw=False, variables=None):
|
2021-03-04 15:15:41 +01:00
|
|
|
# pylint: disable=signature-differs
|
2020-01-17 14:27:36 +01:00
|
|
|
"""Return section variables as parent,
|
|
|
|
but override the "raw" argument to always True"""
|
2020-11-06 10:58:36 +01:00
|
|
|
return SafeConfigParser.items(self, section, True, variables)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2021-09-28 17:14:32 +02:00
|
|
|
def _reset(self):
|
2022-02-16 21:55:17 +01:00
|
|
|
"""
|
|
|
|
Reset current config.
|
|
|
|
There doesn't appear to be a built in method for this.
|
|
|
|
"""
|
|
|
|
self._temp = {}
|
2021-09-28 17:14:32 +02:00
|
|
|
sections = self.sections()
|
|
|
|
for x in sections:
|
|
|
|
self.remove_section(x)
|
|
|
|
|
2022-01-28 13:55:23 +01:00
|
|
|
def read(self, filenames=None):
|
|
|
|
self._reset()
|
|
|
|
SafeConfigParser.read(self, os.path.join(os.path.dirname(__file__), 'default.ini'))
|
|
|
|
if filenames:
|
|
|
|
SafeConfigParser.read(self, filenames)
|
|
|
|
|
2021-03-04 15:15:41 +01:00
|
|
|
if sys.version_info[0] == 3:
|
|
|
|
@staticmethod
|
|
|
|
def addresses(hidden=False):
|
|
|
|
"""Return a list of local bitmessage addresses (from section labels)"""
|
2022-01-28 13:55:23 +01:00
|
|
|
return [x for x in config.sections() if x.startswith('BM-') and (
|
|
|
|
hidden or not config.safeGetBoolean(x, 'hidden'))]
|
2021-03-04 15:15:41 +01:00
|
|
|
|
2021-09-28 17:14:32 +02:00
|
|
|
def readfp(self, fp, filename=None):
|
2021-09-28 17:41:45 +02:00
|
|
|
# pylint: disable=no-member
|
2021-09-28 17:14:32 +02:00
|
|
|
SafeConfigParser.read_file(self, fp)
|
2021-03-04 15:15:41 +01:00
|
|
|
else:
|
|
|
|
@staticmethod
|
|
|
|
def addresses():
|
|
|
|
"""Return a list of local bitmessage addresses (from section labels)"""
|
|
|
|
return [
|
2022-01-28 13:55:23 +01:00
|
|
|
x for x in config.sections() if x.startswith('BM-')]
|
2017-06-27 13:19:12 +02:00
|
|
|
|
2017-01-15 10:50:02 +01:00
|
|
|
def save(self):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Save the runtime config onto the filesystem"""
|
2017-01-15 10:50:02 +01:00
|
|
|
fileName = os.path.join(state.appdata, 'keys.dat')
|
2018-04-19 13:02:12 +02:00
|
|
|
fileNameBak = '.'.join([
|
|
|
|
fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak'])
|
|
|
|
# create a backup copy to prevent the accidental loss due to
|
|
|
|
# the disk write failure
|
2017-01-15 10:50:02 +01:00
|
|
|
try:
|
|
|
|
shutil.copyfile(fileName, fileNameBak)
|
|
|
|
# The backup succeeded.
|
|
|
|
fileNameExisted = True
|
2022-01-28 13:55:23 +01:00
|
|
|
except(IOError, Exception):
|
2018-04-19 13:02:12 +02:00
|
|
|
# The backup failed. This can happen if the file
|
|
|
|
# didn't exist before.
|
2017-01-15 10:50:02 +01:00
|
|
|
fileNameExisted = False
|
2021-03-04 15:15:41 +01:00
|
|
|
|
|
|
|
with open(fileName, 'w') as configfile:
|
2017-01-15 10:50:02 +01:00
|
|
|
self.write(configfile)
|
|
|
|
# delete the backup
|
|
|
|
if fileNameExisted:
|
|
|
|
os.remove(fileNameBak)
|
2017-06-27 13:19:12 +02:00
|
|
|
|
|
|
|
def validate(self, section, option, value):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Input validator interface (using factory pattern)"""
|
2017-06-27 13:19:12 +02:00
|
|
|
try:
|
2020-11-06 10:58:36 +01:00
|
|
|
return getattr(self, 'validate_%s_%s' % (section, option))(value)
|
2017-06-27 13:19:12 +02:00
|
|
|
except AttributeError:
|
|
|
|
return True
|
|
|
|
|
2019-11-04 15:45:02 +01:00
|
|
|
@staticmethod
|
|
|
|
def validate_bitmessagesettings_maxoutboundconnections(value):
|
|
|
|
"""Reject maxoutboundconnections that are too high or too low"""
|
2017-06-27 13:19:12 +02:00
|
|
|
try:
|
|
|
|
value = int(value)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if value < 0 or value > 8:
|
|
|
|
return False
|
|
|
|
return True
|
2022-01-28 13:55:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
config = BMConfigParser()
|