Completed
Push — master ( 2b80fa...6ea077 )
by Nicolas
01:22
created

FolderList   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 150
rs 9
wmc 35

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 13 4
A warning() 0 3 1
B update() 0 20 5
A set() 0 3 1
A critical() 0 3 1
A __str__() 0 2 1
A path() 0 3 1
A careful() 0 3 1
A setAll() 0 3 1
B __folder_size() 0 16 6
A getAll() 0 3 1
A __repr__() 0 2 1
B __set_folder_list() 0 22 5
A __len__() 0 2 1
A __get__() 0 12 3
A get() 0 3 1
A __getitem__() 0 2 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2017 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 folder list."""
21
22
import os
23
24
from glances.compat import range
25
from glances.logger import logger
26
27
# Use the built-in version of scandir/walk if possible, otherwise
28
# use the scandir module version
29
scandir_tag = True
30
try:
31
    # For Python 3.5 or higher
32
    from os import scandir
33
except ImportError:
34
    # For others...
35
    try:
36
        from scandir import scandir
37
    except ImportError:
38
        scandir_tag = False
39
40
41
class FolderList(object):
42
43
    """This class describes the optional monitored folder list.
44
45
    The folder list is a list of 'important' folder to monitor.
46
47
    The list (Python list) is composed of items (Python dict).
48
    An item is defined (dict keys):
49
    * path: Path to the folder
50
    * careful: optional careful threshold (in MB)
51
    * warning: optional warning threshold (in MB)
52
    * critical: optional critical threshold (in MB)
53
    """
54
55
    # Maximum number of items in the list
56
    __folder_list_max_size = 10
57
    # The folder list
58
    __folder_list = []
59
60
    def __init__(self, config):
61
        """Init the folder list from the configuration file, if it exists."""
62
        self.config = config
63
64
        if self.config is not None and self.config.has_section('folders'):
65
            if scandir_tag:
66
                # Process monitoring list
67
                logger.debug("Folder list configuration detected")
68
                self.__set_folder_list('folders')
69
            else:
70
                logger.error('Scandir not found. Please use Python 3.5+ or install the scandir lib')
71
        else:
72
            self.__folder_list = []
73
74
    def __set_folder_list(self, section):
75
        """Init the monitored folder list.
76
77
        The list is defined in the Glances configuration file.
78
        """
79
        for l in range(1, self.__folder_list_max_size + 1):
80
            value = {}
81
            key = 'folder_' + str(l) + '_'
82
83
            # Path is mandatory
84
            value['path'] = self.config.get_value(section, key + 'path')
85
            if value['path'] is None:
86
                continue
87
88
            # Optional conf keys
89
            for i in ['careful', 'warning', 'critical']:
90
                value[i] = self.config.get_value(section, key + i)
91
                if value[i] is None:
92
                    logger.debug("No {} threshold for folder {}".format(i, value["path"]))
93
94
            # Add the item to the list
95
            self.__folder_list.append(value)
96
97
    def __str__(self):
98
        return str(self.__folder_list)
99
100
    def __repr__(self):
101
        return self.__folder_list
102
103
    def __getitem__(self, item):
104
        return self.__folder_list[item]
105
106
    def __len__(self):
107
        return len(self.__folder_list)
108
109
    def __get__(self, item, key):
110
        """Meta function to return key value of item.
111
112
        Return None if not defined or item > len(list)
113
        """
114
        if item < len(self.__folder_list):
115
            try:
116
                return self.__folder_list[item][key]
117
            except Exception:
118
                return None
119
        else:
120
            return None
121
122
    def __folder_size(self, path):
123
        """Return the size of the directory given by path
124
125
        path: <string>"""
126
127
        ret = 0
128
        for f in scandir(path):
129
            if f.is_dir() and (f.name != '.' or f.name != '..'):
130
                ret += self.__folder_size(os.path.join(path, f.name))
131
            else:
132
                try:
133
                    ret += f.stat().st_size
134
                except OSError:
135
                    pass
136
137
        return ret
138
139
    def update(self):
140
        """Update the command result attributed."""
141
        # Only continue if monitor list is not empty
142
        if len(self.__folder_list) == 0:
143
            return self.__folder_list
144
145
        # Iter upon the folder list
146
        for i in range(len(self.get())):
147
            # Update folder size
148
            try:
149
                self.__folder_list[i]['size'] = self.__folder_size(self.path(i))
150
            except OSError as e:
151
                logger.debug('Cannot get folder size ({}). Error: {}'.format(self.path(i), e))
152
                if e.errno == 13:
153
                    # Permission denied
154
                    self.__folder_list[i]['size'] = '!'
155
                else:
156
                    self.__folder_list[i]['size'] = '?'
157
158
        return self.__folder_list
159
160
    def get(self):
161
        """Return the monitored list (list of dict)."""
162
        return self.__folder_list
163
164
    def set(self, newlist):
165
        """Set the monitored list (list of dict)."""
166
        self.__folder_list = newlist
167
168
    def getAll(self):
169
        # Deprecated: use get()
170
        return self.get()
171
172
    def setAll(self, newlist):
173
        # Deprecated: use set()
174
        self.set(newlist)
175
176
    def path(self, item):
177
        """Return the path of the item number (item)."""
178
        return self.__get__(item, "path")
179
180
    def careful(self, item):
181
        """Return the careful threshold of the item number (item)."""
182
        return self.__get__(item, "careful")
183
184
    def warning(self, item):
185
        """Return the warning threshold of the item number (item)."""
186
        return self.__get__(item, "warning")
187
188
    def critical(self, item):
189
        """Return the critical threshold of the item number (item)."""
190
        return self.__get__(item, "critical")
191