From 30fafb6bd2bb7ee5f2f814d316645f16e874295b Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Wed, 1 Apr 2015 09:16:11 -0700 Subject: [PATCH] Rewrote weeutil.weeutil.read_config to use utilities in config_util --- bin/config_util.py | 5 ++-- bin/wee_config | 2 ++ bin/weeutil/weeutil.py | 55 ++++++++++++++++-------------------------- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/bin/config_util.py b/bin/config_util.py index 481ecea9..7dc59282 100644 --- a/bin/config_util.py +++ b/bin/config_util.py @@ -54,7 +54,7 @@ metricwx_group = {'group_altitude': 'meter', def find_file(file_path=None, args=None, locations=['/etc/weewx', '/home/weewx'], file_name='weewx.conf'): - """Find and return a path to a file, looking in "the usual places. + """Find and return a path to a file, looking in "the usual places." General strategy: @@ -116,7 +116,8 @@ def read_config(config_path, args=None, """ # Find and open the config file: try: - config_path = find_file(config_path, args, locations=locations) + config_path = find_file(config_path, args, + locations=locations, file_name=file_name) try: # Now open it up and parse it. config_dict = configobj.ConfigObj(config_path, file_error=True) diff --git a/bin/wee_config b/bin/wee_config index 0d79f1c2..35f0bc98 100755 --- a/bin/wee_config +++ b/bin/wee_config @@ -157,6 +157,8 @@ class WeeConfig(object): # Open up and parse the distribution config file: try: dist_config_dict = configobj.ConfigObj(options.dist_config, file_error=True) + except IOError, e: + sys.exit(str(e)) except SyntaxError, e: sys.exit("Syntax error in distribution configuration file '%s': %s" % (options.dist_config, e)) diff --git a/bin/weeutil/weeutil.py b/bin/weeutil/weeutil.py index ee498805..7773020c 100644 --- a/bin/weeutil/weeutil.py +++ b/bin/weeutil/weeutil.py @@ -1050,7 +1050,7 @@ def max_with_none(x_seq): xmax = max(x, xmax) return xmax -def read_config(config_fn, args=None, msg_to_stderr=True, exit_on_fail=True): +def read_config(file_path, args=None, msg_to_stderr=True, exit_on_fail=True): """Read the specified configuration file, return a dictionary of the file contents. If no file is specified, look in the standard locations for weewx.conf. Returns the filename of the actual configuration file @@ -1059,7 +1059,7 @@ def read_config(config_fn, args=None, msg_to_stderr=True, exit_on_fail=True): first arg will be interpreted as the filename as long as it does not start with a hyphen. - config_fn: configuration filename + file_path: Possible path to the file args: command-line arguments @@ -1067,58 +1067,45 @@ def read_config(config_fn, args=None, msg_to_stderr=True, exit_on_fail=True): messages go to syslog. exit_on_fail: If this is true, exit when file not found or parsing fails. - Otherwise re-throw the exception that caused the error. + Otherwise re-raise the exception that caused the error. - return: filename, dictionary + return: A tuple: (path-to-config-file, config_dict) """ + import config_util import configobj - locations = ['/etc/weewx', '/home/weewx'] - # Figure out the config file - if config_fn is None: - if args is not None and len(args) > 0 and not args[0].startswith('-'): - config_fn = args[0] - # Shift args to the left: - del args[0] - if config_fn is None: - for f in locations: - fn = f + '/weewx.conf' - if os.path.isfile(fn): - config_fn = fn - break - if config_fn is None: - msg = 'No configuration file specified, and none found in any of:\n %s' % ', '.join(locations) + try: + config_path = config_util.find_file(file_path, args) + except IOError, e: if msg_to_stderr: - print >>sys.stderr, msg + print >>sys.stderr, str(e) else: - syslog.syslog(syslog.LOG_CRIT, msg) + syslog.syslog(syslog.LOG_CRIT, str(e)) if exit_on_fail: exit(1) - return None, None + raise # Try to open up the configuration file. Declare an error if unable to. try : - config_dict = configobj.ConfigObj(config_fn, file_error=True) - except IOError: - msg = "Unable to open configuration file %s" % config_fn + config_dict = configobj.ConfigObj(config_path, file_error=True) + except IOError, e: if msg_to_stderr: - print >>sys.stderr, msg + print >>sys.stderr, str(e) else: - syslog.syslog(syslog.LOG_CRIT, msg) + syslog.syslog(syslog.LOG_CRIT, str(e)) if exit_on_fail: - exit(1) + sys.exit(str(e)) raise - except configobj.ConfigObjError: - msg = "Error wile parsing configuration file %s" % config_fn + except configobj.ConfigObjError, e: if msg_to_stderr: - print >>sys.stderr, msg + print >>sys.stderr, str(e) else: - syslog.syslog(syslog.LOG_CRIT, msg) + syslog.syslog(syslog.LOG_CRIT, str(e)) if exit_on_fail: - exit(1) + sys.exit(1) raise - return config_fn, config_dict + return config_path, config_dict def print_dict(d, margin=0, increment=4): """Pretty print a dictionary.