def override_config(config, override):
'''Overrides the given config by tornado.options'''
# Init config object
if 'tornado' not in config:
config['tornado'] = {}
if config['tornado'].get('server') is None:
config['tornado']['server'] = {}
if config['tornado'].get('app_settings') is None:
config['tornado']['app_settings'] = {}
if 'db' not in config:
config['db'] = {}
# Handle host, port, base, with the following priorities
# 1. command line arg (by tornado.options)
# 2. config file
# 3. hardcoded default
server_cfg = config['tornado']['server']
host = find_first([override.get('host'), server_cfg.get('host'), DEFAULT_HOST])
port = find_first([override.get('port'), server_cfg.get('port'), DEFAULT_PORT])
base = find_first([override.get('base'), server_cfg.get('base'), DEFAULT_BASE])
config['tornado']['server'].update(dict(host=host, port=port, base=base))
# If the debug flag is set, save it in app_settings and activate db echo
if override.get('debug') is not None:
config['tornado']['app_settings']['debug'] = override.get('debug')
config['db']['echo'] = override.get('debug') == 2
# Set up default database uri if it is not given
db_uri = find_first([override.get('db_path'), config['db'].get('uri'), DEFAULT_DEV_DB_URI])
config['db']['uri'] = db_uri
# Set up default cookie secret if it is not given
cookie_secret = config['tornado']['app_settings'].get('cookie_secret')
cookie_secret = find_first([cookie_secret, DEFAULT_COOKIE_SECRET])
config['tornado']['app_settings']['cookie_secret'] = cookie_secret
评论列表
文章目录