Commit 5de1d0c2 authored by kaiyou's avatar kaiyou

Support loading multiple config files

parent fddaadfc
import yaml
import os
import glob
from amonit import util
def expand(conf):
......@@ -23,7 +26,11 @@ def expand(conf):
def load():
""" Load the configuration from a yaml file
"""
with open(os.environ.get("AMONIT_CONFIG", "amonit.yaml")) as handle:
conf = yaml.load(handle)
return expand(conf)
config_path = glob.glob(os.environ.get("AMONIT_CONFIG", "amonit.yaml"))
if not config_path:
raise ValueError("No configuration was loaded")
loaded = [{}]
for filename in config_path:
with open(filename, "r") as handle:
loaded.append(expand(yaml.load(handle)))
return util.merge(*loaded)
......@@ -21,19 +21,34 @@ def filter_match(candidate, filters):
""" Apply a list of filters to a candidate dictionary
"""
if type(filters) is list:
return any(
filter_match(candidate, subfilters)
for subfilters in filters
)
return any(filter_match(candidate, subfilters)
for subfilters in filters)
elif type(filters) is dict:
return all(
filter_match(candidate.get(key, None), value)
for key, value in filters.items()
)
return all(filter_match(candidate.get(key, None), value)
for key, value in filters.items())
else:
return candidate == filters
def merge(*objects):
""" Merge a list of dictionaries, lists or immutables
"""
mode = type(objects[0])
if not all(type(obj) is mode for obj in objects):
raise ValueError("Cannot merge mixed typed objects")
if len(objects) == 1:
return objects[0]
elif mode is dict:
return {
key: merge(*[obj[key] for obj in objects if key in obj])
for obj in objects for key in obj.keys()
}
elif mode is list:
return sum(objects)
else:
raise ValueError("Cannot merge objects of type {}".format(mode))
def wrap(function):
""" Wrap a check function to handle simple results and exceptions
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment