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

93 lines
3.3 KiB
Python
Raw Normal View History

import ConfigParser
import datetime
import shutil
import os
from singleton import Singleton
import state
BMConfigDefaults = {
"bitmessagesettings": {
"maxaddrperstreamsend": 500,
"maxbootstrapconnections": 20,
"maxdownloadrate": 0,
"maxoutboundconnections": 8,
"maxtotalconnections": 200,
"maxuploadrate": 0,
},
"network": {
"asyncore": True,
"bind": None,
},
"inventory": {
"storage": "sqlite",
"acceptmismatch": False,
},
"zlib": {
'maxsize': 1048576
}
}
@Singleton
class BMConfigParser(ConfigParser.SafeConfigParser):
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, basestring):
raise TypeError("option values must be strings")
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":
2017-06-24 12:13:35 +02:00
return ConfigParser.ConfigParser.get(self, section, option, raw, variables)
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
except ConfigParser.InterpolationError:
2017-06-24 12:13:35 +02:00
return ConfigParser.ConfigParser.get(self, section, option, True, 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 safeGetBoolean(self, section, field):
try:
return self.getboolean(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
return False
def safeGetInt(self, section, field, default=0):
try:
return self.getint(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
return default
2017-01-14 23:18:06 +01:00
def safeGet(self, section, option, default = None):
try:
return self.get(section, option)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, 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 filter(lambda x: x.startswith('BM-'), BMConfigParser().sections())
def save(self):
fileName = os.path.join(state.appdata, 'keys.dat')
fileNameBak = fileName + "." + datetime.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):
# The backup failed. This can happen if the file didn't exist before.
fileNameExisted = False
# write the file
with open(fileName, 'wb') as configfile:
self.write(configfile)
# delete the backup
if fileNameExisted:
os.remove(fileNameBak)