Completed
Push — master ( bcff18...adb900 )
by Nicolas
01:15
created

glances.GlancesPasswordList.load()   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 18
rs 9.4285
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2015 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances 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 Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Manage the Glances passwords list."""
21
22
from glances.logger import logger
23
from glances.password import GlancesPassword
24
25
26
class GlancesPasswordList(GlancesPassword):
27
28
    """Manage the Glances passwords list for the client|browser/server."""
29
30
    _section = "passwords"
31
32
    def __init__(self, config=None, args=None):
33
        super(GlancesPasswordList, self).__init__()
34
        # password_dict is a dict (JSON compliant)
35
        # {'host': 'password', ... }
36
        # Load the configuration file
37
        self._password_dict = self.load(config)
38
39
    def load(self, config):
40
        """Load the password from the configuration file."""
41
        password_dict = []
42
43
        if config is None:
44
            logger.warning("No configuration file available. Cannot load password list.")
45
        elif not config.has_section(self._section):
46
            logger.warning("No [%s] section in the configuration file. Cannot load password list." % self._section)
47
        else:
48
            logger.info("Start reading the [%s] section in the configuration file" % self._section)
49
50
            password_dict = dict(config.items(self._section))
51
52
            # Password list loaded
53
            logger.info("%s password(s) loaded from the configuration file" % len(password_dict))
54
            logger.debug("Password dictionary: %s" % password_dict)
55
56
        return password_dict
57
58
    def get_password(self, host=None):
59
        """
60
        If host=None, return the current server list (dict).
61
        Else, return the host's password (or the default one if defined or None)
62
        """
63
        if host is None:
64
            return self._password_dict
65
        else:
66
            try:
67
                return self._password_dict[host]
68
            except (KeyError, TypeError):
69
                try:
70
                    return self._password_dict['default']
71
                except (KeyError, TypeError):
72
                    return None
73
                return None
74
75
    def set_password(self, host, password):
76
        """Set a password for a specific host."""
77
        self._password_dict[host] = password
78