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
|
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}
|
|
|
|
|
2022-02-16 22:04:52 +01:00
|
|
|
def safeGetBoolean(self, section, option):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Return value as boolean, False on exceptions"""
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2022-02-16 22:04:52 +01:00
|
|
|
return self.getboolean(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-05-15 12:18:07 +02:00
|
|
|
return False
|
2017-01-11 14:27:19 +01:00
|
|
|
|
2022-02-16 22:04:52 +01:00
|
|
|
def safeGetInt(self, section, option, 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:
|
2022-02-16 22:04:52 +01:00
|
|
|
return int(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-05-15 12:18:07 +02:00
|
|
|
return default
|
2017-01-14 23:18:06 +01:00
|
|
|
|
2022-02-16 22:04:52 +01:00
|
|
|
def safeGetFloat(self, section, option, default=0.0):
|
2021-08-16 17:35:04 +02:00
|
|
|
"""Return value as float, default on exceptions,
|
|
|
|
0.0 if default missing"""
|
|
|
|
try:
|
2022-02-16 22:04:52 +01:00
|
|
|
return self.getfloat(section, option)
|
2021-08-16 17:35:04 +02:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
|
|
|
ValueError, AttributeError):
|
|
|
|
return default
|
|
|
|
|
2018-04-19 13:02:12 +02:00
|
|
|
def safeGet(self, section, option, default=None):
|
2022-02-16 22:04:52 +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()
|
2022-02-16 22:04:52 +01:00
|
|
|
SafeConfigParser.read(
|
|
|
|
self, os.path.join(os.path.dirname(__file__), 'default.ini'))
|
2022-01-28 13:55:23 +01:00
|
|
|
if filenames:
|
|
|
|
SafeConfigParser.read(self, filenames)
|
|
|
|
|
2022-09-30 22:20:42 +02:00
|
|
|
def addresses(self, sort=False):
|
2022-02-16 22:04:52 +01:00
|
|
|
"""Return a list of local bitmessage addresses (from section labels)"""
|
2022-09-30 22:20:42 +02:00
|
|
|
sections = [x for x in self.sections() if x.startswith('BM-')]
|
|
|
|
if sort:
|
|
|
|
sections.sort(key=lambda item: self.get(item, 'label').lower())
|
|
|
|
return sections
|
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
|
|
|
|
2022-05-05 09:51:11 +02:00
|
|
|
def search_addresses(self, address, searched_text):
|
|
|
|
"""Return the searched label of MyAddress"""
|
|
|
|
return [x for x in [self.get(address, 'label').lower(),
|
|
|
|
address.lower()] if searched_text in x]
|
|
|
|
|
|
|
|
def disable_address(self, address):
|
|
|
|
""""Disabling the specific Address"""
|
|
|
|
self.set(str(address), 'enabled', 'false')
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def enable_address(self, address):
|
|
|
|
""""Enabling the specific Address"""
|
|
|
|
self.set(address, 'enabled', 'true')
|
|
|
|
self.save()
|
|
|
|
|
2022-01-28 13:55:23 +01:00
|
|
|
|
2022-02-16 22:04:52 +01:00
|
|
|
if not getattr(BMConfigParser, 'read_file', False):
|
|
|
|
BMConfigParser.read_file = BMConfigParser.readfp
|
|
|
|
|
|
|
|
config = BMConfigParser() # TODO: remove this crutch
|