blob: b26a2903f3ebea077908aeadd8d7028406a7cb8a [file] [log] [blame]
Lianhao Lu4e3368b2018-08-09 16:45:49 +08001# Copyright (c) 2018 Intel Corp Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14#
15
16import logging
17import os
18
19from ruamel import yaml
20
21LOG = logging.getLogger(__name__)
22
23
24def load_definitions(config_file, defaults=None):
25 if not os.path.exists(config_file):
26 LOG.info("No definitions configuration file %s found! "
27 "Using default config.", config_file)
28 definition_cfg = defaults
29 else:
30 LOG.debug("Loading definitions configuration file: %s", config_file)
31 with open(config_file) as cf:
32 config = cf.read()
33 try:
34 definition_cfg = yaml.safe_load(config)
35 except yaml.YAMLError as err:
36 if hasattr(err, 'problem_mark'):
37 mark = err.problem_mark
38 errmsg = ("Invalid YAML syntax in Definitions file %(file)s "
39 "at line: %(line)s, column: %(column)s." %
40 dict(file=config_file,
41 line=mark.line + 1,
42 column=mark.column + 1))
43 else:
44 errmsg = ("YAML error reading Definitions file %s" % config_file)
45 LOG.error(errmsg)
46 raise
47
48 LOG.debug("Definitions: %s", definition_cfg)
49 return definition_cfg