summaryrefslogtreecommitdiff
path: root/obnam/progress.py
blob: e8cd7ad2c4508013e9123eb70c31eef5a9f26ebb (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
80
81
82
83
84
85
86
87
# Copyright (C) 2007  Lars Wirzenius <liw@iki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


"""Progress reporting for Obnam"""


import sys
import time


class ProgressReporter:

    initial_values = (("total_files", 0), ("uploaded", 0), ("downloaded", 0),
                      ("current_action", None))

    def __init__(self, config):
        self.config = config
        self.dict = dict(self.initial_values)
        self.prev_output = ""
        self.timestamp = 0
        self.min_time = 1.0 # seconds

    def reporting_is_allowed(self):
        return self.config.getboolean("backup", "report-progress")

    def clear(self):
        if self.reporting_is_allowed():
            sys.stdout.write("\r" + " " * len(self.prev_output) + "\r")
            sys.stdout.flush()
        
    def update(self, key, value):
        self.dict[key] = value
        if self.reporting_is_allowed():
            now = time.time()
            if now - self.timestamp >= self.min_time:
                self.clear()
                parts = []
                parts.append("Files: %(total_files)d" % self.dict)
                parts.append("up: %d MB" % 
                             (self.dict["uploaded"] / 1024 / 1024))
                parts.append("down: %d MB" % 
                             (self.dict["downloaded"] / 1024 / 1024))
                current = self.dict["current_action"]
                if current:
                    parts.append("now:")
                    part_one = ", ".join(parts)
                    progress = "%s%s" % (part_one, 
                                         current[-(79-len(part_one)):])
                else:
                    progress = ", ".join(parts)
                sys.stdout.write(progress)
                sys.stdout.flush()
                self.prev_output = progress
                self.timestamp = now

    def update_total_files(self, total_files):
        self.update("total_files", total_files)

    def update_uploaded(self, uploaded):
        self.update("uploaded", uploaded)

    def update_downloaded(self, downloaded):
        self.update("downloaded", downloaded)

    def update_current_action(self, current_action):
        self.update("current_action", current_action)

    def final_report(self):
        self.timestamp = 0
        self.update_current_action(None)
        if self.reporting_is_allowed():
            sys.stdout.write("\n")
            sys.stdout.flush()