3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

conf: fix deprecation warnings crashing because log is unavailable

This allows conf methods to access to global logger by via an optional 'logger' argument. However,
the caveat is that the logging facilities are still unavailable on first start, because log can
only be imported *after* the configuration is loaded.
This commit is contained in:
James Lu 2016-12-05 22:43:01 -08:00
parent 62a4bc01ce
commit b3387f2d41
3 changed files with 13 additions and 6 deletions

15
conf.py
View File

@ -44,7 +44,7 @@ conf = {'bot':
} }
confname = 'unconfigured' confname = 'unconfigured'
def validateConf(conf): def validateConf(conf, logger=None):
"""Validates a parsed configuration dict.""" """Validates a parsed configuration dict."""
assert type(conf) == dict, "Invalid configuration given: should be type dict, not %s." % type(conf).__name__ assert type(conf) == dict, "Invalid configuration given: should be type dict, not %s." % type(conf).__name__
@ -54,7 +54,14 @@ def validateConf(conf):
# Make sure at least one form of authentication is valid. # Make sure at least one form of authentication is valid.
# Also we'll warn them that login:user/login:password is deprecated # Also we'll warn them that login:user/login:password is deprecated
if conf['login'].get('password') or conf['login'].get('user'): if conf['login'].get('password') or conf['login'].get('user'):
log.warning("The 'login:user' and 'login:password' options are deprecated since PyLink 1.1. Please switch to the new 'login:accounts' format as outlined in the example config.") e = "The 'login:user' and 'login:password' options are deprecated since PyLink 1.1. " \
"Please switch to the new 'login:accounts' format as outlined in the example config."
if logger:
logger.warning(e)
else:
# FIXME: we need a better fallback when log isn't available on first
# start.
print('WARNING: %s' % e)
old_login_valid = type(conf['login'].get('password')) == type(conf['login'].get('user')) == str old_login_valid = type(conf['login'].get('password')) == type(conf['login'].get('user')) == str
newlogins = conf['login'].get('accounts', {}) newlogins = conf['login'].get('accounts', {})
@ -69,7 +76,7 @@ def validateConf(conf):
return conf return conf
def loadConf(filename, errors_fatal=True): def loadConf(filename, errors_fatal=True, logger=None):
"""Loads a PyLink configuration file from the filename given.""" """Loads a PyLink configuration file from the filename given."""
global confname, conf, fname global confname, conf, fname
# Note: store globally the last loaded conf filename, for REHASH in coremods/control. # Note: store globally the last loaded conf filename, for REHASH in coremods/control.
@ -79,7 +86,7 @@ def loadConf(filename, errors_fatal=True):
try: try:
with open(filename, 'r') as f: with open(filename, 'r') as f:
conf = yaml.load(f) conf = yaml.load(f)
conf = validateConf(conf) conf = validateConf(conf, logger=logger)
except Exception as e: except Exception as e:
print('ERROR: Failed to load config from %r: %s: %s' % (filename, type(e).__name__, e), file=sys.stderr) print('ERROR: Failed to load config from %r: %s: %s' % (filename, type(e).__name__, e), file=sys.stderr)
print(' Users upgrading from users < 0.9-alpha1 should note that the default configuration has been renamed to *pylink.yml*, not *config.yml*', file=sys.stderr) print(' Users upgrading from users < 0.9-alpha1 should note that the default configuration has been renamed to *pylink.yml*, not *config.yml*', file=sys.stderr)

View File

@ -54,7 +54,7 @@ def _rehash():
"""Rehashes the PyLink daemon.""" """Rehashes the PyLink daemon."""
old_conf = conf.conf.copy() old_conf = conf.conf.copy()
fname = conf.fname fname = conf.fname
new_conf = conf.loadConf(fname, errors_fatal=False) new_conf = conf.loadConf(fname, errors_fatal=False, logger=log)
new_conf = conf.validateConf(new_conf) new_conf = conf.validateConf(new_conf)
conf.conf = new_conf conf.conf = new_conf

2
pylink
View File

@ -29,7 +29,7 @@ if __name__ == '__main__':
print('PyLink %s (in VCS: %s)' % (__version__, real_version)) print('PyLink %s (in VCS: %s)' % (__version__, real_version))
sys.exit() sys.exit()
# Load the config # FIXME: we can't pass logging on to conf until we set up the config...
conf.loadConf(args.config) conf.loadConf(args.config)
from pylinkirc.log import log from pylinkirc.log import log