Synchronise the openstack bugs
[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 import os
16
17 from six.moves import configparser
18
19 from toscaparser.utils.gettextutils import _
20 import translator.common.exception as exception
21
22
23 class ConfigProvider(object):
24     '''Global config proxy that wraps a ConfigParser object.
25
26     Allows for class based access to config values. Should only be initialized
27     once using the corresponding translator.conf file in the conf directory.
28
29     '''
30
31     # List that captures all of the conf file sections.
32     # Append any new sections to this list.
33     _sections = ['DEFAULT']
34     _translator_config = None
35
36     @classmethod
37     def _load_config(cls, conf_file):
38         '''Private method only to be called once from the __init__ module'''
39
40         cls._translator_config = configparser.ConfigParser()
41         try:
42             cls._translator_config.read(conf_file)
43         except configparser.ParsingError:
44             msg = _('Unable to parse translator.conf file.'
45                     'Check to see that it exists in the conf directory.')
46             raise exception.ConfFileParseError(message=msg)
47
48     @classmethod
49     def get_value(cls, section, key):
50         try:
51             value = cls._translator_config.get(section, key)
52         except configparser.NoOptionError:
53             raise exception.ConfOptionNotDefined(key=key, section=section)
54         except configparser.NoSectionError:
55             raise exception.ConfSectionNotDefined(section=section)
56
57         return value
58
59     @classmethod
60     def get_all_values(cls):
61         values = []
62         for section in cls._sections:
63             try:
64                 values.extend(cls._translator_config.items(section=section))
65             except configparser.NoOptionError:
66                 raise exception.ConfSectionNotDefined(section=section)
67
68         return values
69
70     @classmethod
71     def get_translator_logging_file(cls):
72         conf_file = ''
73         CONF_FILENAME = 'heat_translator_logging.conf'
74         conf_path = os.path.dirname(os.path.abspath(__file__))
75         conf_file = os.path.join(conf_path, CONF_FILENAME)
76         return conf_file