Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit a2ce685

Browse files
committed
Add config directory support
This takes @rtrox's work in GH-93 and cleans it up a bit; pretty much just taking out unrelated changes that were inadvertently included. @rtrox deserves credit for the feature. This closes #93.
1 parent 05b1bad commit a2ce685

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

supernova/config.py

+43-5
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,28 @@ def load_config(config_file_override=False):
4242
Pulls the supernova configuration file and reads it
4343
"""
4444
supernova_config = get_config_file(config_file_override)
45+
supernova_config_dir = get_config_directory(config_file_override)
46+
47+
if not supernova_config and not supernova_config_dir:
48+
raise Exception("Couldn't find a valid configuration file to parse")
49+
50+
nova_creds = ConfigObj()
4551

4652
# Can we successfully read the configuration file?
47-
try:
48-
nova_creds = ConfigObj(supernova_config)
49-
except:
50-
raise
53+
if supernova_config:
54+
try:
55+
nova_creds.merge(ConfigObj(supernova_config))
56+
except:
57+
raise("There's an error in your configuration file")
58+
59+
if supernova_config_dir:
60+
for dir_file in os.listdir(supernova_config_dir):
61+
full_path = ''.join((supernova_config_dir, dir_file))
62+
try:
63+
nova_creds.merge(ConfigObj(full_path))
64+
except:
65+
msg = "Skipping '%s', Parsing Error.".format(full_path)
66+
print(msg)
5167

5268
create_dynamic_configs(nova_creds)
5369
return nova_creds
@@ -74,7 +90,29 @@ def get_config_file(override_files=False):
7490
if os.path.isfile(config_file):
7591
return config_file
7692

77-
raise Exception("Couldn't find a valid configuration file to parse")
93+
return False
94+
95+
96+
def get_config_directory(override_files=False):
97+
"""
98+
Looks for the most specific configuration directory possible, in order to
99+
load individual configuration files.
100+
"""
101+
if override_files:
102+
possible_dirs = [override_files]
103+
104+
else:
105+
xdg_config_home = os.environ.get('XDG_CONFIG_HOME') or \
106+
os.path.expanduser('~/.config')
107+
possible_dirs = [os.path.join(xdg_config_home, "supernova.d/"),
108+
os.path.expanduser("~/.supernova.d/"),
109+
".supernova.d/"]
110+
111+
for config_dir in reversed(possible_dirs):
112+
if os.path.isdir(config_dir):
113+
return config_dir
114+
115+
return False
78116

79117

80118
def create_dynamic_configs(config,

tests/test_executable.py

+8
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ def test_broken_configuration_file(self):
176176
assert result.exit_code != 0
177177
assert "There's an error in your configuration file" in result.output
178178

179+
def test_broken_configuration_directory(self):
180+
runner = CliRunner()
181+
command = ['-c', 'tests/configs/', 'dfw',
182+
'list']
183+
result = runner.invoke(executable.run_supernova, command)
184+
assert "Skipping 'tests/configs/rax_without_keyring_malformed', "
185+
"Parsing Error." in result.output
186+
179187
def test_dashboard(self, monkeypatch):
180188
def mockreturn(url):
181189
return False

0 commit comments

Comments
 (0)