summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Waldon <bcwaldon@gmail.com>2012-04-25 16:11:45 -0700
committerBrian Waldon <bcwaldon@gmail.com>2012-04-26 06:50:20 -0700
commit9768498a537f6046e30920982cf54c91d08d79fa (patch)
tree621699cf3f1b6c53d21ce176540d1f052fc55856
parent6e4948b95278f86f5bf2925308797c014cb31c59 (diff)
downloadoslo-9768498a537f6046e30920982cf54c91d08d79fa.tar.gz
oslo-9768498a537f6046e30920982cf54c91d08d79fa.tar.xz
oslo-9768498a537f6046e30920982cf54c91d08d79fa.zip
Provide file extension when when looking for files
* Allow an extension to be passed to find_config files, defaulting to '.conf' Change-Id: I022a3b28d9067158e9ed0da741a5e72cb73af167
-rw-r--r--openstack/common/cfg.py11
-rw-r--r--tests/unit/test_cfg.py10
2 files changed, 16 insertions, 5 deletions
diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py
index 31da063..3602fee 100644
--- a/openstack/common/cfg.py
+++ b/openstack/common/cfg.py
@@ -319,11 +319,12 @@ class ConfigFileValueError(Error):
pass
-def find_config_files(project=None, prog=None):
+def find_config_files(project=None, prog=None, extension='.conf'):
"""Return a list of default configuration files.
:param project: an optional project name
:param prog: the program name, defaulting to the basename of sys.argv[0]
+ :param extension: the type of the config file
We default to two config files: [${project}.conf, ${prog}.conf]
@@ -356,16 +357,16 @@ def find_config_files(project=None, prog=None):
]
cfg_dirs = filter(bool, cfg_dirs)
- def search_dirs(dirs, basename):
+ def search_dirs(dirs, basename, extension):
for d in dirs:
- path = os.path.join(d, basename)
+ path = os.path.join(d, '%s%s' % (basename, extension))
if os.path.exists(path):
return path
config_files = []
if project:
- config_files.append(search_dirs(cfg_dirs, '%s.conf' % project))
- config_files.append(search_dirs(cfg_dirs, '%s.conf' % prog))
+ config_files.append(search_dirs(cfg_dirs, project, extension))
+ config_files.append(search_dirs(cfg_dirs, prog, extension))
return filter(bool, config_files)
diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py
index 4e8ec21..da78254 100644
--- a/tests/unit/test_cfg.py
+++ b/tests/unit/test_cfg.py
@@ -133,6 +133,16 @@ class FindConfigFilesTestCase(BaseTestCase):
self.assertEquals(find_config_files(project='blaa'), config_files)
+ def test_find_config_files_with_extension(self):
+ config_files = ['/etc/foo.json']
+
+ self.stubs.Set(sys, 'argv', ['foo'])
+ self.stubs.Set(os.path, 'exists', lambda p: p in config_files)
+
+ self.assertEquals(find_config_files(project='blaa'), [])
+ self.assertEquals(find_config_files(project='blaa', extension='.json'),
+ config_files)
+
class CliOptsTestCase(BaseTestCase):