From ddf7efb3bff73450bd0a8520fb1ef351e6462c4e Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 14 Jun 2015 02:40:38 +0100 Subject: [PATCH] Refactoring loadConfig() - Don't use backslash, os.path's OS-agnostic functions guarantee portability. - Delegate the reponsibility of building the conffile's path to a private auxiliary function: _path_to_conffile() - Avoid duplication of information, i.e. store filename's name template in a global variable. Nedless to say, all this needs testing. --- securepass/utils.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/securepass/utils.py b/securepass/utils.py index 1a21693..506cf23 100644 --- a/securepass/utils.py +++ b/securepass/utils.py @@ -12,20 +12,38 @@ import os import sys +DEFAULT_CONF_FILENAME = "securepass.conf" -def loadConfig(): - """loadConfig returns cassandra servers""" - conffiles = ['/etc/securepass.conf', - '/usr/local/etc/securepass.conf', - os.getcwd() + '/securepass.conf'] - #conffiles.append(os.path.join(os.path.expanduser("~"), ".securepass")) +def _path_to_conffile(*path): + return os.path.join(*(path + (DEFAULT_CONF_FILENAME,))) + - ## Virtual Environment handling - # VIRTUAL_ENV is not reliable, switching to sys.real_prefix +def _list_conffiles_locations(): + prefix = sys.prefix + venv_prefix = None if hasattr(sys, 'real_prefix'): - conffiles.append(sys.prefix + "/securepass.conf") - conffiles.append(sys.prefix + "/etc/securepass.conf") + prefix = sys.real_prefix + venv_prefix = sys.prefix + + conffiles = [ + _path_to_conffile(prefix, "etc"), + _path_to_conffile(prefix, "usr", "local", "etc"), + _path_to_conffile(os.getcwd()), + ] + if venv_prefix is not None: + conffiles.extend([ + _path_to_conffile(venv_prefix), + _path_to_conffile(venv_prefix, "etc"), + ]) + + return conffiles + + +def loadConfig(): + """loadConfig returns cassandra servers""" + + conffiles = _list_conffiles_locations() conf_found = 0