Skip to content

Commit

Permalink
Merge pull request #22 from jacobtomlinson/config-location
Browse files Browse the repository at this point in the history
Check multiple locations for config files
  • Loading branch information
jacobtomlinson committed Aug 7, 2016
2 parents a83fe06 + 8df8c6b commit 3583802
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion opsdroid/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def main():
logging.info("Stated application")
with OpsDroid() as opsdroid:
loader = Loader(opsdroid)
opsdroid.config = loader.load_config_file("./configuration.yaml")
opsdroid.config = loader.load_config_file([
"./configuration.yaml",
"~/.opsdroid/configuration.yaml",
"/etc/opsdroid/configuration.yaml"
])
if "logging" in opsdroid.config:
set_logging_level(opsdroid.config['logging'])
loader.load_config(opsdroid.config)
Expand Down
16 changes: 12 additions & 4 deletions opsdroid/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ def __init__(self, opsdroid):
self.opsdroid = opsdroid
logging.debug("Loaded loader")

def load_config_file(self, config_path):
def load_config_file(self, config_paths):
"""Load a yaml config file from path."""
if not os.path.isfile(config_path):
self.opsdroid.critical("Config file " + config_path +
" not found", 1)
config_path = ""
for possible_path in config_paths:
if not os.path.isfile(possible_path):
logging.warning("Config file " + possible_path +
" not found", 1)
else:
config_path = possible_path
break

if not config_path:
self.opsdroid.critical("No configuration files found", 1)

try:
with open(config_path, 'r') as stream:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ def reset_subprocess_mocks(self):

def test_load_config_file(self):
opsdroid, loader = self.setup()
config = loader.load_config_file("tests/configs/minimal.yaml")
config = loader.load_config_file(["tests/configs/minimal.yaml"])
self.assertIsNotNone(config)

def test_load_non_existant_config_file(self):
opsdroid, loader = self.setup()
loader.load_config_file("file_which_does_not_exist")
loader.load_config_file(["file_which_does_not_exist"])
self.assertEqual(len(opsdroid.mock_calls), 2)

def test_load_broken_config_file(self):
opsdroid, loader = self.setup()
loader.load_config_file("tests/configs/broken.yaml")
loader.load_config_file(["tests/configs/broken.yaml"])
self.assertRaises(yaml.YAMLError)

def test_setup_modules(self):
Expand Down

0 comments on commit 3583802

Please sign in to comment.