3
# Copyright (C) 2015 Canonical Ltd.
4
# Copyright (C) 2015 VMware Inc.
6
# Author: Sankar Tanguturi <stanguturi@vmware.com>
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License version 3, as
10
# published by the Free Software Foundation.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
25
import ConfigParser as configparser
27
from .config_source import ConfigSource
29
logger = logging.getLogger(__name__)
32
class ConfigFile(ConfigSource, dict):
33
"""ConfigFile module to load the content from a specified source."""
35
def __init__(self, filename):
36
self._loadConfigFile(filename)
39
def _insertKey(self, key, val):
41
Inserts a Key Value pair.
44
key -- The key to insert
45
val -- The value to insert for the key
51
if key.startswith('-') or '|-' in key:
56
# "sensitive" settings shall not be logged
58
logger.debug("ADDED KEY-VAL :: '%s' = '%s'" % (key, val))
60
logger.debug("ADDED KEY-VAL :: '%s' = '*****************'" % key)
64
def _loadConfigFile(self, filename):
66
Parses properties from the specified config file.
68
Any previously available properties will be removed.
69
Sensitive data will not be logged in case the key starts
73
filename - The full path to the config file.
75
logger.info('Parsing the config file %s.' % filename)
77
config = configparser.ConfigParser()
78
config.optionxform = str
83
for category in config.sections():
84
logger.debug("FOUND CATEGORY = '%s'" % category)
86
for (key, value) in config.items(category):
87
self._insertKey(category + '|' + key, value)
89
def should_keep_current_value(self, key):
91
Determines whether a value for a property must be kept.
93
If the propery is missing, it is treated as it should be not
94
changed by the engine.
97
key -- The key to search for.
99
# helps to distinguish from "empty" value which is used to indicate
101
return key not in self
103
def should_remove_current_value(self, key):
105
Determines whether a value for the property must be removed.
107
If the specified key is empty, it is treated as it should be
108
removed by the engine.
110
Return true if the value can be removed, false otherwise.
113
key -- The key to search for.
115
# helps to distinguish from "missing" value which is used to indicate
116
# "keeping unchanged"
118
return not bool(self[key])
122
def get_count_with_prefix(self, prefix):
124
Return the total count of keys that start with the specified prefix.
127
prefix -- prefix of the key
129
return len([key for key in self if key.startswith(prefix)])