For settings: On conversion failure, fallback to default values

This commit is contained in:
Job Evers-Meltzer 2016-10-28 15:12:51 -05:00
parent 0d066b2d11
commit 395a68b261
2 changed files with 26 additions and 8 deletions

View file

@ -1,9 +1,14 @@
import copy
import logging
import os
import sys
from appdirs import user_data_dir
log = logging.getLogger(__name__)
LINUX = 1
DARWIN = 2
WINDOWS = 3
@ -30,6 +35,16 @@ else:
def convert_setting(env_val, current_val):
try:
return _convert_setting(env_val, current_val)
except Exception as exc:
log.warning(
'Failed to convert %s. Returning original: %s: %s',
env_val, current_val, exc)
return current_val
def _convert_setting(env_val, current_val):
new_type = env_val.__class__
current_type = current_val.__class__
if current_type is bool:
@ -40,7 +55,7 @@ def convert_setting(env_val, current_val):
elif str(env_val).lower() == "true":
return True
else:
raise ValueError
raise ValueError('{} is not a valid boolean value'.format(env_val))
elif current_type is int:
return int(env_val)
elif current_type is float:
@ -79,6 +94,7 @@ def add_env_settings_to_dict(settings_dict):
class Setting(object):
"""A collection of configuration settings"""
__fixed = []
__excluded = ['get_dict', 'update']
@ -94,8 +110,9 @@ class Setting(object):
def __setitem__(self, key, value):
assert key in self and key not in self.__fixed, KeyError(key)
_value = convert_setting(value, self[key])
self.__dict__.update({key: _value})
old_value = self[key]
new_value = convert_setting(value, old_value)
self.__dict__[key] = new_value
def __contains__(self, item):
return item in iter(self)
@ -107,13 +124,12 @@ class Setting(object):
for k, v in other.iteritems():
try:
self.__setitem__(k, v)
except KeyError:
pass
except AssertionError:
except (KeyError, AssertionError):
pass
class AdjustableSettings(Setting):
"""Settings that are allowed to be overriden by the user"""
def __init__(self):
self.is_generous_host = True
self.run_on_startup = False
@ -163,6 +179,7 @@ class AdjustableSettings(Setting):
class ApplicationSettings(Setting):
"""Settings that are constants and shouldn't be overriden"""
def __init__(self):
self.MAX_HANDSHAKE_SIZE = 2**16
self.MAX_REQUEST_SIZE = 2**16

View file

@ -82,9 +82,10 @@ def start():
if args.branch:
to_pass.update({'ui_branch': args.branch})
to_pass.update({'use_auth_http': args.useauth})
to_pass.update({'wallet': args.wallet})
print to_pass
to_pass.update({'wallet_type': args.wallet})
log.debug('Settings overrides: %s', to_pass)
settings.update(to_pass)
log.debug('Final Settings: %s', settings.__dict__)
try:
JSONRPCProxy.from_url(settings.API_CONNECTION_STRING).is_running()