OOF design framework seed code
[optf/osdf.git] / config / loader.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import json
20
21 import yaml
22
23
24 def load_config_file(config_file: str, child_name="dockerConfiguration") -> dict:
25     """
26     Load OSDF configuration from a file -- currently only yaml/json are supported
27     :param config_file:  path to config file (.yaml or .json).
28     :param child_name: if present, return only that child node
29     :return: config (all or specific child node)
30     """
31     with open(config_file, 'r') as fid:
32         res = {}
33         if config_file.endswith(".yaml"):
34             res = yaml.load(fid)
35         elif config_file.endswith(".json") or config_file.endswith("json"):
36             res = json.load(fid)
37     return res.get(child_name, res) if child_name else res
38
39
40 def dcae_config(config_file: str) -> dict:
41     return load_config_file(config_file, child_name="dockerConfiguration")
42
43
44 def all_configs(**kwargs: dict) -> dict:
45     """
46     Load all specified configurations
47     :param config_file_spec: key-value pairs
48            (e.g. { "core": "common_config.yaml", "deployment": "/tmp/1452523532json" })
49     :return: merged config as a nested dictionary
50     """
51     return {k: load_config_file(fname) for k, fname in kwargs.items()}