RIFT OSM R1 Initial Submission
[osm/SO.git] / common / python / rift / mano / tosca_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 # Copyright 2016 RIFT.io Inc
15
16
17 ''' Provide a global configuration for the TOSCA translator'''
18
19 from six.moves import configparser
20
21 import rift.mano.tosca_translator.common.exception as exception
22
23 from rift.mano.tosca_translator.common.utils import _
24
25
26 class ConfigProvider(object):
27 '''Global config proxy that wraps a ConfigParser object.
28
29 Allows for class based access to config values. Should only be initialized
30 once using the corresponding translator.conf file in the conf directory.
31
32 '''
33
34 # List that captures all of the conf file sections.
35 # Append any new sections to this list.
36 _sections = ['DEFAULT']
37 _translator_config = None
38
39 @classmethod
40 def _load_config(cls, conf_file):
41 '''Private method only to be called once from the __init__ module'''
42
43 cls._translator_config = configparser.ConfigParser()
44 try:
45 cls._translator_config.read(conf_file)
46 except configparser.ParsingError:
47 msg = _('Unable to parse translator.conf file.'
48 'Check to see that it exists in the conf directory.')
49 raise exception.ConfFileParseError(message=msg)
50
51 @classmethod
52 def get_value(cls, section, key):
53 try:
54 value = cls._translator_config.get(section, key)
55 except configparser.NoOptionError:
56 raise exception.ConfOptionNotDefined(key=key, section=section)
57 except configparser.NoSectionError:
58 raise exception.ConfSectionNotDefined(section=section)
59
60 return value
61
62 @classmethod
63 def get_all_values(cls):
64 values = []
65 for section in cls._sections:
66 try:
67 values.extend(cls._translator_config.items(section=section))
68 except configparser.NoOptionError:
69 raise exception.ConfSectionNotDefined(section=section)
70
71 return values