From b3387f2d4146439565fb96908eeb2d8af0600805 Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 5 Dec 2016 22:43:01 -0800 Subject: [PATCH] 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. --- conf.py | 15 +++++++++++---- coremods/control.py | 2 +- pylink | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/conf.py b/conf.py index b02435c..0beb721 100644 --- a/conf.py +++ b/conf.py @@ -44,7 +44,7 @@ conf = {'bot': } confname = 'unconfigured' -def validateConf(conf): +def validateConf(conf, logger=None): """Validates a parsed configuration dict.""" 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. # Also we'll warn them that login:user/login:password is deprecated 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 newlogins = conf['login'].get('accounts', {}) @@ -69,7 +76,7 @@ def validateConf(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.""" global confname, conf, fname # Note: store globally the last loaded conf filename, for REHASH in coremods/control. @@ -79,7 +86,7 @@ def loadConf(filename, errors_fatal=True): try: with open(filename, 'r') as f: conf = yaml.load(f) - conf = validateConf(conf) + conf = validateConf(conf, logger=logger) except Exception as e: 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) diff --git a/coremods/control.py b/coremods/control.py index 9b3cbc8..7f7791a 100644 --- a/coremods/control.py +++ b/coremods/control.py @@ -54,7 +54,7 @@ def _rehash(): """Rehashes the PyLink daemon.""" old_conf = conf.conf.copy() 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) conf.conf = new_conf diff --git a/pylink b/pylink index 3a71ed7..3cd08d1 100755 --- a/pylink +++ b/pylink @@ -29,7 +29,7 @@ if __name__ == '__main__': print('PyLink %s (in VCS: %s)' % (__version__, real_version)) sys.exit() - # Load the config + # FIXME: we can't pass logging on to conf until we set up the config... conf.loadConf(args.config) from pylinkirc.log import log