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

glances.Timer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %
Metric Value
dl 0
loc 19
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A finished() 0 2 1
A __init__() 0 3 1
A reset() 0 2 1
A set() 0 2 1
A start() 0 2 1
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
"""The timer manager."""
21
22
from time import time
23
24
# Global list to manage the elapsed time
25
last_update_times = {}
26
27
28
def getTimeSinceLastUpdate(IOType):
29
    """Return the elapsed time since last update."""
30
    global last_update_times
31
    # assert(IOType in ['net', 'disk', 'process_disk'])
32
    current_time = time()
33
    last_time = last_update_times.get(IOType)
34
    if not last_time:
35
        time_since_update = 1
36
    else:
37
        time_since_update = current_time - last_time
38
    last_update_times[IOType] = current_time
39
    return time_since_update
40
41
42
class Timer(object):
43
44
    """The timer class. A simple chronometer."""
45
46
    def __init__(self, duration):
47
        self.duration = duration
48
        self.start()
49
50
    def start(self):
51
        self.target = time() + self.duration
52
53
    def reset(self):
54
        self.start()
55
56
    def set(self, duration):
57
        self.duration = duration
58
59
    def finished(self):
60
        return time() > self.target
61