Merge "netsted template validate type error"
[parser.git] / tosca2heat / heat-translator / translator / conf / config.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 ''' Provide a global configuration for the TOSCA translator'''
15
16 from six.moves import configparser
17
18 from toscaparser.utils.gettextutils import _
19 import translator.common.exception as exception
20
21
22 class ConfigProvider(object):
23     '''Global config proxy that wraps a ConfigParser object.
24
25     Allows for class based access to config values. Should only be initialized
26     once using the corresponding translator.conf file in the conf directory.
27
28     '''
29
30     # List that captures all of the conf file sections.
31     # Append any new sections to this list.
32     _sections = ['DEFAULT']
33     _translator_config = None
34
35     @classmethod
36     def _load_config(cls, conf_file):
37         '''Private method only to be called once from the __init__ module'''
38
39         cls._translator_config = configparser.ConfigParser()
40         try:
41             cls._translator_config.read(conf_file)
42         except configparser.ParsingError:
43             msg = _('Unable to parse translator.conf file.'
44                     'Check to see that it exists in the conf directory.')
45             raise exception.ConfFileParseError(message=msg)
46
47     @classmethod
48     def get_value(cls, section, key):
49         try:
50             value = cls._translator_config.get(section, key)
51         except configparser.NoOptionError:
52             raise exception.ConfOptionNotDefined(key=key, section=section)
53         except configparser.NoSectionError:
54             raise exception.ConfSectionNotDefined(section=section)
55
56         return value
57
58     @classmethod
59     def get_all_values(cls):
60         values = []
61         for section in cls._sections:
62             try:
63                 values.extend(cls._translator_config.items(section=section))
64             except configparser.NoOptionError:
65                 raise exception.ConfSectionNotDefined(section=section)
66
67         return values