This repository has been archived on 2024-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-20/src/bmconfigparser.py

176 lines
5.9 KiB
Python
Raw Normal View History

2018-04-19 13:02:12 +02:00
"""
BMConfigParser class definition and default configuration settings
"""
import configparser
import shutil
import os
2018-04-19 13:02:12 +02:00
from datetime import datetime
import state
2018-04-19 13:02:12 +02:00
from singleton import Singleton
BMConfigDefaults = {
"bitmessagesettings": {
"maxaddrperstreamsend": 500,
"maxbootstrapconnections": 20,
"maxdownloadrate": 0,
"maxoutboundconnections": 8,
"maxtotalconnections": 200,
"maxuploadrate": 0,
"apiinterface": "127.0.0.1",
"apiport": 8442
},
"threads": {
"receive": 3,
},
"network": {
"bind": '',
"dandelion": 90,
},
"inventory": {
"storage": "sqlite",
"acceptmismatch": False,
},
2017-06-24 12:17:01 +02:00
"knownnodes": {
"maxnodes": 20000,
},
"zlib": {
'maxsize': 1048576
}
}
2018-04-19 13:02:12 +02:00
@Singleton
class BMConfigParser(configparser.ConfigParser):
"""Singleton class inherited from ConfigParsedadfeConfigParser
2018-04-19 13:02:12 +02:00
with additional methods specific to bitmessage config."""
_temp = {}
def set(self, section, option, value=None):
2016-12-06 11:01:17 +01:00
if self._optcre is self.OPTCRE or value:
if not isinstance(value, str):
2016-12-06 11:01:17 +01:00
raise TypeError("option values must be strings")
if not self.validate(section, option, value):
2018-04-19 13:02:12 +02:00
raise ValueError("Invalid value %s" % value)
return configparser.ConfigParser.set(self, section, option, value)
2016-12-06 11:01:17 +01:00
2017-06-24 12:13:35 +02:00
def get(self, section, option, raw=False, variables=None):
try:
if section == "bitmessagesettings" and option == "timeformat":
return configparser.ConfigParser.get(
self, section, option, raw=True, vars=variables)
try:
return self._temp[section][option]
except KeyError:
pass
return configparser.ConfigParser.get(
self, section, option, raw=True, vars=variables)
except configparser.InterpolationError:
return configparser.ConfigParser.get(
self, section, option, raw=True, vars=variables)
except (configparser.NoSectionError, configparser.NoOptionError) as e:
try:
return BMConfigDefaults[section][option]
except (KeyError, ValueError, AttributeError):
raise e
2016-12-06 11:01:17 +01: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}
def safeGetBoolean(self, section, field):
config = configparser.ConfigParser()
try:
#Used in the python2.7
# return self.getboolean(section, field)
#Used in the python3.5.2
return config.getboolean(section, field)
except (configparser.NoSectionError, configparser.NoOptionError,
2018-04-19 13:02:12 +02:00
ValueError, AttributeError):
return False
def safeGetInt(self, section, field, default=0):
config = configparser.ConfigParser()
try:
#Used in the python2.7
# return self.getint(section, field)
#Used in the python3.5.2
return config.getint(section, field)
except (configparser.NoSectionError, configparser.NoOptionError,
2018-04-19 13:02:12 +02:00
ValueError, AttributeError):
return default
2017-01-14 23:18:06 +01:00
2018-04-19 13:02:12 +02:00
def safeGet(self, section, option, default=None):
try:
return self.get(section, option)
except (configparser.NoSectionError, configparser.NoOptionError,
2018-04-19 13:02:12 +02:00
ValueError, AttributeError):
return default
2017-06-24 12:13:35 +02:00
def items(self, section, raw=False, variables=None):
return configparser.ConfigParser.items(self, section, True, variables)
2016-12-06 11:01:17 +01:00
def addresses(self):
return [x for x in BMConfigParser().sections() if x.startswith('BM-')]
def read(self, filenames):
configparser.ConfigParser.read(self, filenames)
for section in self.sections():
for option in self.options(section):
try:
2018-04-19 13:02:12 +02:00
if not self.validate(
section, option,
self[section][option]
2018-04-19 13:02:12 +02:00
):
try:
newVal = BMConfigDefaults[section][option]
except configparser.NoSectionError:
continue
except KeyError:
continue
configparser.ConfigParser.set(
2018-04-19 13:02:12 +02:00
self, section, option, newVal)
except configparser.InterpolationError:
continue
def save(self):
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
try:
shutil.copyfile(fileName, fileNameBak)
# The backup succeeded.
fileNameExisted = True
except (IOError, Exception):
2018-04-19 13:02:12 +02:00
# The backup failed. This can happen if the file
# didn't exist before.
fileNameExisted = False
# write the file
with open(fileName, 'w') as configfile:
self.write(configfile)
# delete the backup
if fileNameExisted:
os.remove(fileNameBak)
def validate(self, section, option, value):
try:
return getattr(self, 'validate_{}_{}'.format(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