summaryrefslogtreecommitdiff
path: root/cloudinit/sources/helpers/vmware/imc/config_file.py
blob: 9f86838964f7d029a2f90acf0d1270326106c5cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright (C) 2015 Canonical Ltd.
# Copyright (C) 2015 VMware Inc.
#
# Author: Sankar Tanguturi <stanguturi@vmware.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

import configparser
import logging

from cloudinit.sources.helpers.vmware.imc.config_source import ConfigSource

logger = logging.getLogger(__name__)


class ConfigFile(ConfigSource, dict):
    """ConfigFile module to load the content from a specified source."""

    def __init__(self, filename):
        self._loadConfigFile(filename)

    def _insertKey(self, key, val):
        """
        Inserts a Key Value pair.

        Keyword arguments:
        key -- The key to insert
        val -- The value to insert for the key

        """
        key = key.strip()
        val = val.strip()

        if key.startswith("-") or "|-" in key:
            canLog = False
        else:
            canLog = True

        # "sensitive" settings shall not be logged
        if canLog:
            logger.debug("ADDED KEY-VAL :: '%s' = '%s'", key, val)
        else:
            logger.debug("ADDED KEY-VAL :: '%s' = '*****************'", key)

        self[key] = val

    def _loadConfigFile(self, filename):
        """
        Parses properties from the specified config file.

        Any previously available properties will be removed.
        Sensitive data will not be logged in case the key starts
        from '-'.

        Keyword arguments:
        filename - The full path to the config file.
        """
        logger.info("Parsing the config file %s.", filename)

        config = configparser.ConfigParser()
        config.optionxform = str
        config.read(filename)

        self.clear()

        for category in config.sections():
            logger.debug("FOUND CATEGORY = '%s'", category)

            for (key, value) in config.items(category):
                self._insertKey(category + "|" + key, value)

    def get_count_with_prefix(self, prefix):
        """
        Return the total count of keys that start with the specified prefix.

        Keyword arguments:
        prefix -- prefix of the key
        """
        return len([key for key in self if key.startswith(prefix)])