2016-12-06 13:04:52 +01:00
|
|
|
import ConfigParser
|
2017-01-15 10:50:02 +01:00
|
|
|
import datetime
|
|
|
|
import shutil
|
|
|
|
import os
|
2016-12-06 10:47:39 +01:00
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
from singleton import Singleton
|
2017-01-15 10:50:02 +01:00
|
|
|
import state
|
2017-01-11 14:27:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Singleton
|
2016-12-06 13:04:52 +01:00
|
|
|
class BMConfigParser(ConfigParser.SafeConfigParser):
|
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:
|
|
|
|
if not isinstance(value, basestring):
|
|
|
|
raise TypeError("option values must be strings")
|
2016-12-06 13:04:52 +01:00
|
|
|
return ConfigParser.ConfigParser.set(self, section, option, value)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
|
|
|
def get(self, section, option, raw=False, vars=None):
|
2016-12-06 13:04:52 +01:00
|
|
|
if section == "bitmessagesettings" and option == "timeformat":
|
|
|
|
try:
|
|
|
|
return ConfigParser.ConfigParser.get(self, section, option, raw, vars)
|
|
|
|
except ConfigParser.InterpolationError:
|
|
|
|
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
|
|
|
|
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
def safeGetBoolean(self, section, field):
|
|
|
|
if self.has_option(section, field):
|
|
|
|
try:
|
|
|
|
return self.getboolean(section, field)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
2017-02-26 17:46:02 +01:00
|
|
|
def safeGetInt(self, section, field, default=0):
|
2017-01-14 23:18:06 +01:00
|
|
|
if self.has_option(section, field):
|
|
|
|
try:
|
|
|
|
return self.getint(section, field)
|
|
|
|
except ValueError:
|
2017-02-26 17:46:02 +01:00
|
|
|
return default
|
|
|
|
return default
|
2017-01-14 23:18:06 +01:00
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
def safeGet(self, section, option, default = None):
|
|
|
|
if self.has_option(section, option):
|
|
|
|
return self.get(section, option)
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
2016-12-06 11:01:17 +01:00
|
|
|
def items(self, section, raw=False, vars=None):
|
2016-12-06 13:04:52 +01:00
|
|
|
return ConfigParser.ConfigParser.items(self, section, True, vars)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2017-01-15 10:50:02 +01:00
|
|
|
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
|
2017-02-08 14:19:02 +01:00
|
|
|
except (IOError, Exception):
|
2017-01-15 10:50:02 +01:00
|
|
|
# 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)
|