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 # Copyright 2016 RIFT.io Inc
17 ''' Provide a global configuration for the TOSCA translator'''
19 import rift
.mano
.yang_translator
.common
.exception
as exception
20 from rift
.mano
.yang_translator
.common
.utils
import _
22 from six
.moves
import configparser
25 class ConfigProvider(object):
26 '''Global config proxy that wraps a ConfigParser object.
28 Allows for class based access to config values. Should only be initialized
29 once using the corresponding translator.conf file in the conf directory.
33 # List that captures all of the conf file sections.
34 # Append any new sections to this list.
35 _sections
= ['DEFAULT']
36 _translator_config
= None
39 def _load_config(cls
, conf_file
):
40 '''Private method only to be called once from the __init__ module'''
42 cls
._translator
_config
= configparser
.ConfigParser()
44 cls
._translator
_config
.read(conf_file
)
45 except configparser
.ParsingError
:
46 msg
= _('Unable to parse translator.conf file.'
47 'Check to see that it exists in the conf directory.')
48 raise exception
.ConfFileParseError(message
=msg
)
51 def get_value(cls
, section
, key
):
53 value
= cls
._translator
_config
.get(section
, key
)
54 except configparser
.NoOptionError
:
55 raise exception
.ConfOptionNotDefined(key
=key
, section
=section
)
56 except configparser
.NoSectionError
:
57 raise exception
.ConfSectionNotDefined(section
=section
)
62 def get_all_values(cls
):
64 for section
in cls
._sections
:
66 values
.extend(cls
._translator
_config
.items(section
=section
))
67 except configparser
.NoOptionError
:
68 raise exception
.ConfSectionNotDefined(section
=section
)