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

View file

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