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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
14 ''' Provide a global configuration for the TOSCA translator'''
16 from six.moves import configparser
18 from toscaparser.utils.gettextutils import _
19 import translator.common.exception as exception
22 class ConfigProvider(object):
23 '''Global config proxy that wraps a ConfigParser object.
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.
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
36 def _load_config(cls, conf_file):
37 '''Private method only to be called once from the __init__ module'''
39 cls._translator_config = configparser.ConfigParser()
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)
48 def get_value(cls, section, key):
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)
59 def get_all_values(cls):
61 for section in cls._sections:
63 values.extend(cls._translator_config.items(section=section))
64 except configparser.NoOptionError:
65 raise exception.ConfSectionNotDefined(section=section)