Reduced BMConfigParser in the sake of readability

This commit is contained in:
Dmitri Bogomolov 2018-03-03 17:59:53 +02:00
parent ba5caf3fda
commit 529559d06a
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -30,19 +30,20 @@ def _loadTrustedPeer():
def loadConfig(): def loadConfig():
config = BMConfigParser()
if state.appdata: if state.appdata:
BMConfigParser().read(state.appdata + 'keys.dat') config.read(state.appdata + 'keys.dat')
# state.appdata must have been specified as a startup option. # state.appdata must have been specified as a startup option.
needToCreateKeysFile = BMConfigParser().safeGet( needToCreateKeysFile = config.safeGet(
'bitmessagesettings', 'settingsversion') is None 'bitmessagesettings', 'settingsversion') is None
if not needToCreateKeysFile: if not needToCreateKeysFile:
print( print(
'Loading config files from directory specified' 'Loading config files from directory specified'
' on startup: %s' % state.appdata) ' on startup: %s' % state.appdata)
else: else:
BMConfigParser().read(paths.lookupExeFolder() + 'keys.dat') config.read(paths.lookupExeFolder() + 'keys.dat')
try: try:
BMConfigParser().get('bitmessagesettings', 'settingsversion') config.get('bitmessagesettings', 'settingsversion')
print 'Loading config files from same directory as program.' print 'Loading config files from same directory as program.'
needToCreateKeysFile = False needToCreateKeysFile = False
state.appdata = paths.lookupExeFolder() state.appdata = paths.lookupExeFolder()
@ -50,8 +51,8 @@ def loadConfig():
# Could not load the keys.dat file in the program directory. # Could not load the keys.dat file in the program directory.
# Perhaps it is in the appdata directory. # Perhaps it is in the appdata directory.
state.appdata = paths.lookupAppdataFolder() state.appdata = paths.lookupAppdataFolder()
BMConfigParser().read(state.appdata + 'keys.dat') config.read(state.appdata + 'keys.dat')
needToCreateKeysFile = BMConfigParser().safeGet( needToCreateKeysFile = config.safeGet(
'bitmessagesettings', 'settingsversion') is None 'bitmessagesettings', 'settingsversion') is None
if not needToCreateKeysFile: if not needToCreateKeysFile:
print 'Loading existing config files from', state.appdata print 'Loading existing config files from', state.appdata
@ -60,73 +61,61 @@ def loadConfig():
# This appears to be the first time running the program; there is # This appears to be the first time running the program; there is
# no config file (or it cannot be accessed). Create config file. # no config file (or it cannot be accessed). Create config file.
BMConfigParser().add_section('bitmessagesettings') config.add_section('bitmessagesettings')
BMConfigParser().set('bitmessagesettings', 'settingsversion', '10') config.set('bitmessagesettings', 'settingsversion', '10')
BMConfigParser().set('bitmessagesettings', 'port', '8444') config.set('bitmessagesettings', 'port', '8444')
BMConfigParser().set( config.set('bitmessagesettings', 'timeformat', '%%c')
'bitmessagesettings', 'timeformat', '%%c') config.set('bitmessagesettings', 'blackwhitelist', 'black')
BMConfigParser().set('bitmessagesettings', 'blackwhitelist', 'black') config.set('bitmessagesettings', 'startonlogon', 'false')
BMConfigParser().set('bitmessagesettings', 'startonlogon', 'false')
if 'linux' in sys.platform: if 'linux' in sys.platform:
BMConfigParser().set( config.set('bitmessagesettings', 'minimizetotray', 'false')
'bitmessagesettings', 'minimizetotray', 'false')
# This isn't implimented yet and when True on # This isn't implimented yet and when True on
# Ubuntu causes Bitmessage to disappear while # Ubuntu causes Bitmessage to disappear while
# running when minimized. # running when minimized.
else: else:
BMConfigParser().set( config.set('bitmessagesettings', 'minimizetotray', 'true')
'bitmessagesettings', 'minimizetotray', 'true') config.set('bitmessagesettings', 'showtraynotifications', 'true')
BMConfigParser().set( config.set('bitmessagesettings', 'startintray', 'false')
'bitmessagesettings', 'showtraynotifications', 'true') config.set('bitmessagesettings', 'socksproxytype', 'none')
BMConfigParser().set('bitmessagesettings', 'startintray', 'false') config.set('bitmessagesettings', 'sockshostname', 'localhost')
BMConfigParser().set('bitmessagesettings', 'socksproxytype', 'none') config.set('bitmessagesettings', 'socksport', '9050')
BMConfigParser().set( config.set('bitmessagesettings', 'socksauthentication', 'false')
'bitmessagesettings', 'sockshostname', 'localhost') # config.set('bitmessagesettings', 'sockslisten', 'false')
BMConfigParser().set('bitmessagesettings', 'socksport', '9050') config.set('bitmessagesettings', 'socksusername', '')
BMConfigParser().set( config.set('bitmessagesettings', 'sockspassword', '')
'bitmessagesettings', 'socksauthentication', 'false') config.set('bitmessagesettings', 'keysencrypted', 'false')
# BMConfigParser().set( config.set('bitmessagesettings', 'messagesencrypted', 'false')
# 'bitmessagesettings', 'sockslisten', 'false') config.set(
BMConfigParser().set('bitmessagesettings', 'socksusername', '')
BMConfigParser().set('bitmessagesettings', 'sockspassword', '')
BMConfigParser().set('bitmessagesettings', 'keysencrypted', 'false')
BMConfigParser().set(
'bitmessagesettings', 'messagesencrypted', 'false')
BMConfigParser().set(
'bitmessagesettings', 'defaultnoncetrialsperbyte', 'bitmessagesettings', 'defaultnoncetrialsperbyte',
str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte))
BMConfigParser().set( config.set(
'bitmessagesettings', 'defaultpayloadlengthextrabytes', 'bitmessagesettings', 'defaultpayloadlengthextrabytes',
str(defaults.networkDefaultPayloadLengthExtraBytes)) str(defaults.networkDefaultPayloadLengthExtraBytes))
BMConfigParser().set('bitmessagesettings', 'minimizeonclose', 'false') config.set('bitmessagesettings', 'minimizeonclose', 'false')
# BMConfigParser().set( # config.set(
# 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0') # 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0')
# BMConfigParser().set( # config.set(
# 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', # 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes',
# '0') # '0')
BMConfigParser().set('bitmessagesettings', 'dontconnect', 'true') config.set('bitmessagesettings', 'dontconnect', 'true')
# BMConfigParser().set('bitmessagesettings', 'userlocale', 'system') # config.set('bitmessagesettings', 'userlocale', 'system')
# BMConfigParser().set('bitmessagesettings', 'useidenticons', 'True') # config.set('bitmessagesettings', 'useidenticons', 'True')
# BMConfigParser().set( # config.set(
# 'bitmessagesettings', 'identiconsuffix', # 'bitmessagesettings', 'identiconsuffix',
# ''.join(helper_random.randomchoice( # ''.join(helper_random.randomchoice(
# "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" # "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
# ) for x in range(12) # ) for x in range(12)
# )) # a twelve character pseudo-password to salt the identicons # )) # a twelve character pseudo-password to salt the identicons
BMConfigParser().set('bitmessagesettings', 'replybelow', 'False') config.set('bitmessagesettings', 'replybelow', 'False')
BMConfigParser().set('bitmessagesettings', 'maxdownloadrate', '0') config.set('bitmessagesettings', 'maxdownloadrate', '0')
BMConfigParser().set('bitmessagesettings', 'maxuploadrate', '0') config.set('bitmessagesettings', 'maxuploadrate', '0')
# BMConfigParser().set( # config.set('bitmessagesettings', 'maxoutboundconnections', '8')
# 'bitmessagesettings', 'maxoutboundconnections', '8') # config.set('bitmessagesettings', 'ttl', '367200')
# BMConfigParser().set('bitmessagesettings', 'ttl', '367200')
# UI setting to stop trying to send messages after X days/months # UI setting to stop trying to send messages after X days/months
BMConfigParser().set( config.set('bitmessagesettings', 'stopresendingafterxdays', '')
'bitmessagesettings', 'stopresendingafterxdays', '') config.set('bitmessagesettings', 'stopresendingafterxmonths', '')
BMConfigParser().set( # config.set('bitmessagesettings', 'timeperiod', '-1')
'bitmessagesettings', 'stopresendingafterxmonths', '')
# BMConfigParser().set(
# 'bitmessagesettings', 'timeperiod', '-1')
# Are you hoping to add a new option to the keys.dat file? You're in # Are you hoping to add a new option to the keys.dat file? You're in
# the right place for adding it to users who install the software for # the right place for adding it to users who install the software for
@ -145,7 +134,7 @@ def loadConfig():
os.makedirs(state.appdata) os.makedirs(state.appdata)
if not sys.platform.startswith('win'): if not sys.platform.startswith('win'):
os.umask(0o077) os.umask(0o077)
BMConfigParser().save() config.save()
else: else:
updateConfig() updateConfig()
@ -153,36 +142,33 @@ def loadConfig():
def updateConfig(): def updateConfig():
settingsversion = BMConfigParser().getint( config = BMConfigParser()
'bitmessagesettings', 'settingsversion') settingsversion = config.getint('bitmessagesettings', 'settingsversion')
if settingsversion == 1: if settingsversion == 1:
BMConfigParser().set('bitmessagesettings', 'socksproxytype', 'none') config.set('bitmessagesettings', 'socksproxytype', 'none')
BMConfigParser().set( config.set('bitmessagesettings', 'sockshostname', 'localhost')
'bitmessagesettings', 'sockshostname', 'localhost') config.set('bitmessagesettings', 'socksport', '9050')
BMConfigParser().set('bitmessagesettings', 'socksport', '9050') config.set('bitmessagesettings', 'socksauthentication', 'false')
BMConfigParser().set( config.set('bitmessagesettings', 'socksusername', '')
'bitmessagesettings', 'socksauthentication', 'false') config.set('bitmessagesettings', 'sockspassword', '')
BMConfigParser().set('bitmessagesettings', 'socksusername', '') config.set('bitmessagesettings', 'sockslisten', 'false')
BMConfigParser().set('bitmessagesettings', 'sockspassword', '') config.set('bitmessagesettings', 'keysencrypted', 'false')
BMConfigParser().set('bitmessagesettings', 'sockslisten', 'false') config.set('bitmessagesettings', 'messagesencrypted', 'false')
BMConfigParser().set('bitmessagesettings', 'keysencrypted', 'false')
BMConfigParser().set(
'bitmessagesettings', 'messagesencrypted', 'false')
settingsversion = 2 settingsversion = 2
# let class_sqlThread update SQL and continue # let class_sqlThread update SQL and continue
elif settingsversion == 4: elif settingsversion == 4:
BMConfigParser().set( config.set(
'bitmessagesettings', 'defaultnoncetrialsperbyte', 'bitmessagesettings', 'defaultnoncetrialsperbyte',
str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte))
BMConfigParser().set( config.set(
'bitmessagesettings', 'defaultpayloadlengthextrabytes', 'bitmessagesettings', 'defaultpayloadlengthextrabytes',
str(defaults.networkDefaultPayloadLengthExtraBytes)) str(defaults.networkDefaultPayloadLengthExtraBytes))
settingsversion = 5 settingsversion = 5
if settingsversion == 5: if settingsversion == 5:
BMConfigParser().set( config.set(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0') 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0')
BMConfigParser().set( config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0') 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0')
settingsversion = 7 settingsversion = 7
@ -200,60 +186,54 @@ def updateConfig():
# ) # )
# settingsversion = 7 # settingsversion = 7
if not BMConfigParser().has_option('bitmessagesettings', 'sockslisten'): if not config.has_option('bitmessagesettings', 'sockslisten'):
BMConfigParser().set('bitmessagesettings', 'sockslisten', 'false') config.set('bitmessagesettings', 'sockslisten', 'false')
if not BMConfigParser().has_option('bitmessagesettings', 'userlocale'): if not config.has_option('bitmessagesettings', 'userlocale'):
BMConfigParser().set('bitmessagesettings', 'userlocale', 'system') config.set('bitmessagesettings', 'userlocale', 'system')
if not BMConfigParser().has_option( if not config.has_option('bitmessagesettings', 'sendoutgoingconnections'):
'bitmessagesettings', 'sendoutgoingconnections'): config.set('bitmessagesettings', 'sendoutgoingconnections', 'True')
BMConfigParser().set(
'bitmessagesettings', 'sendoutgoingconnections', 'True')
if not BMConfigParser().has_option( if not config.has_option('bitmessagesettings', 'useidenticons'):
'bitmessagesettings', 'useidenticons'): config.set('bitmessagesettings', 'useidenticons', 'True')
BMConfigParser().set('bitmessagesettings', 'useidenticons', 'True') if not config.has_option('bitmessagesettings', 'identiconsuffix'):
if not BMConfigParser().has_option(
'bitmessagesettings', 'identiconsuffix'):
# acts as a salt # acts as a salt
BMConfigParser().set( config.set(
'bitmessagesettings', 'identiconsuffix', 'bitmessagesettings', 'identiconsuffix',
''.join(helper_random.randomchoice( ''.join(helper_random.randomchoice(
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
) for x in range(12) ) for x in range(12)
)) # a twelve character pseudo-password to salt the identicons )) # a twelve character pseudo-password to salt the identicons
# Add settings to support no longer resending messages after # Add settings to support no longer resending messages after
# a certain period of time even if we never get an ack # a certain period of time even if we never get an ack
if settingsversion == 7: if settingsversion == 7:
BMConfigParser().set( config.set('bitmessagesettings', 'stopresendingafterxdays', '')
'bitmessagesettings', 'stopresendingafterxdays', '') config.set('bitmessagesettings', 'stopresendingafterxmonths', '')
BMConfigParser().set(
'bitmessagesettings', 'stopresendingafterxmonths', '')
settingsversion = 8 settingsversion = 8
# With the change to protocol version 3, reset the user-settable # With the change to protocol version 3, reset the user-settable
# difficulties to 1 # difficulties to 1
if settingsversion == 8: if settingsversion == 8:
BMConfigParser().set( config.set(
'bitmessagesettings', 'defaultnoncetrialsperbyte', 'bitmessagesettings', 'defaultnoncetrialsperbyte',
str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte))
BMConfigParser().set( config.set(
'bitmessagesettings', 'defaultpayloadlengthextrabytes', 'bitmessagesettings', 'defaultpayloadlengthextrabytes',
str(defaults.networkDefaultPayloadLengthExtraBytes)) str(defaults.networkDefaultPayloadLengthExtraBytes))
previousTotalDifficulty = int( previousTotalDifficulty = int(
BMConfigParser().getint( config.getint(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte') 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte')
) / 320 ) / 320
previousSmallMessageDifficulty = int( previousSmallMessageDifficulty = int(
BMConfigParser().getint( config.getint(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes') 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes')
) / 14000 ) / 14000
BMConfigParser().set( config.set(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte',
str(previousTotalDifficulty * 1000)) str(previousTotalDifficulty * 1000))
BMConfigParser().set( config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes',
str(previousSmallMessageDifficulty * 1000)) str(previousSmallMessageDifficulty * 1000))
settingsversion = 9 settingsversion = 9
@ -261,75 +241,70 @@ def updateConfig():
# Adjust the required POW values for each of this user's addresses # Adjust the required POW values for each of this user's addresses
# to conform to protocol v3 norms. # to conform to protocol v3 norms.
if settingsversion == 9: if settingsversion == 9:
for addressInKeysFile in BMConfigParser().addresses(): for addressInKeysFile in config.addresses():
try: try:
previousTotalDifficulty = float( previousTotalDifficulty = float(
BMConfigParser().getint( config.getint(
addressInKeysFile, 'noncetrialsperbyte')) / 320 addressInKeysFile, 'noncetrialsperbyte')) / 320
previousSmallMessageDifficulty = float( previousSmallMessageDifficulty = float(
BMConfigParser().getint( config.getint(
addressInKeysFile, 'payloadlengthextrabytes')) / 14000 addressInKeysFile, 'payloadlengthextrabytes')) / 14000
if previousTotalDifficulty <= 2: if previousTotalDifficulty <= 2:
previousTotalDifficulty = 1 previousTotalDifficulty = 1
if previousSmallMessageDifficulty < 1: if previousSmallMessageDifficulty < 1:
previousSmallMessageDifficulty = 1 previousSmallMessageDifficulty = 1
BMConfigParser().set( config.set(
addressInKeysFile, 'noncetrialsperbyte', addressInKeysFile, 'noncetrialsperbyte',
str(int(previousTotalDifficulty * 1000))) str(int(previousTotalDifficulty * 1000)))
BMConfigParser().set( config.set(
addressInKeysFile, 'payloadlengthextrabytes', addressInKeysFile, 'payloadlengthextrabytes',
str(int(previousSmallMessageDifficulty * 1000))) str(int(previousSmallMessageDifficulty * 1000)))
except Exception: except Exception:
continue continue
BMConfigParser().set('bitmessagesettings', 'maxdownloadrate', '0') config.set('bitmessagesettings', 'maxdownloadrate', '0')
BMConfigParser().set('bitmessagesettings', 'maxuploadrate', '0') config.set('bitmessagesettings', 'maxuploadrate', '0')
settingsversion = 10 settingsversion = 10
# sanity check # sanity check
if BMConfigParser().safeGetInt( if config.safeGetInt(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte') == 0: 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte') == 0:
BMConfigParser().set( config.set(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte',
str( str(defaults.ridiculousDifficulty *
defaults.ridiculousDifficulty defaults.networkDefaultProofOfWorkNonceTrialsPerByte)
* defaults.networkDefaultProofOfWorkNonceTrialsPerByte)
) )
if BMConfigParser().safeGetInt( if config.safeGetInt(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes' 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes'
) == 0: ) == 0:
BMConfigParser().set( config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes',
str( str(defaults.ridiculousDifficulty *
defaults.ridiculousDifficulty defaults.networkDefaultPayloadLengthExtraBytes)
* defaults.networkDefaultPayloadLengthExtraBytes)
) )
if not BMConfigParser().has_option('bitmessagesettings', 'onionhostname'): if not config.has_option('bitmessagesettings', 'onionhostname'):
BMConfigParser().set('bitmessagesettings', 'onionhostname', '') config.set('bitmessagesettings', 'onionhostname', '')
if not BMConfigParser().has_option('bitmessagesettings', 'onionport'): if not config.has_option('bitmessagesettings', 'onionport'):
BMConfigParser().set('bitmessagesettings', 'onionport', '8444') config.set('bitmessagesettings', 'onionport', '8444')
if not BMConfigParser().has_option('bitmessagesettings', 'onionbindip'): if not config.has_option('bitmessagesettings', 'onionbindip'):
BMConfigParser().set('bitmessagesettings', 'onionbindip', '127.0.0.1') config.set('bitmessagesettings', 'onionbindip', '127.0.0.1')
if not BMConfigParser().has_option('bitmessagesettings', 'smtpdeliver'): if not config.has_option('bitmessagesettings', 'smtpdeliver'):
BMConfigParser().set('bitmessagesettings', 'smtpdeliver', '') config.set('bitmessagesettings', 'smtpdeliver', '')
if not BMConfigParser().has_option( if not config.has_option(
'bitmessagesettings', 'hidetrayconnectionnotifications'): 'bitmessagesettings', 'hidetrayconnectionnotifications'):
BMConfigParser().set( config.set(
'bitmessagesettings', 'hidetrayconnectionnotifications', 'false') 'bitmessagesettings', 'hidetrayconnectionnotifications', 'false')
if BMConfigParser().safeGetInt( if config.safeGetInt('bitmessagesettings', 'maxoutboundconnections') < 1:
'bitmessagesettings', 'maxoutboundconnections') < 1: config.set('bitmessagesettings', 'maxoutboundconnections', '8')
BMConfigParser().set(
'bitmessagesettings', 'maxoutboundconnections', '8')
print('WARNING: your maximum outbound connections must be a number.') print('WARNING: your maximum outbound connections must be a number.')
# TTL is now user-specifiable. Let's add an option to save # TTL is now user-specifiable. Let's add an option to save
# whatever the user selects. # whatever the user selects.
if not BMConfigParser().has_option('bitmessagesettings', 'ttl'): if not config.has_option('bitmessagesettings', 'ttl'):
BMConfigParser().set('bitmessagesettings', 'ttl', '367200') config.set('bitmessagesettings', 'ttl', '367200')
BMConfigParser().set( config.set('bitmessagesettings', 'settingsversion', str(settingsversion))
'bitmessagesettings', 'settingsversion', str(settingsversion)) config.save()
BMConfigParser().save()
def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections():