File: progress.py

package info (click to toggle)
positron 1%3A1.1-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 500 kB
  • ctags: 274
  • sloc: python: 2,952; sh: 385; makefile: 51
file content (87 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download | duplicates (2)
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
# -*- Mode: python -*-
#
# progress.py - progress meter
#
# Copyright (C) 2003, Xiph.org Foundation
#
# This file is part of positron.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of a BSD-style license (see the COPYING file in the
# distribution).
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the license for more details.

from sys import stderr

class Progress:
    
    def sync_start(self, num_items):
        stderr.write("Downloading %d files...\n" % (num_items,))
        return true

    def transfer_start(self, filename, size):
        stderr.write("%s:\n")

    def transfer_progress(self, filename, size, bytes_so_far, rate):
        self._print_line(filename, size, bytes_so_far, rate)

    def transfer_stop(self, filename, size):
        self._print_line(filename, size, size, 0)
        stderr.write("\n")

    def sync_stop(self):
        pass

    def _print_line(self, filename, size, bytes_so_far, rate):
        percent_str = "%3d%%" % (100 * bytes_so_far / size,)

        max_bar_width = 38
        if bytes_so_far < size:
            bar_width = max_bar_width * bytes_so_far / size
            bar = "=" * (bar_width - 1) + ">"
        else:
            bar = "=" * max_bar_width

        size_str = self._gen_size_str(size)

        if rate > 0:
            rate_str = self._gen_size_str(rate) + "/s"

            if bytes_so_far < size:
                time_remaining = (size - bytes_so_far) / rate
                seconds = time_remaining % 60
                minutes = (time_remaining // 60) % 60
                hours = time_remaining // 3600

                if hours > 0:
                    eta_str = "ETA: %d:%02d:%02d" % (hours, minutes, seconds)
                else:
                    eta_str = "ETA: %d:%02d" % (minutes, seconds)
            else:
                eta_str = ""
        else:
            rate_str = ""
            eta_str = ""
            
        str = "%-4s|%-*s|%8s  %10s  %-15s" % (percent_str, max_bar_width,
                                           bar, size_str, rate_str, eta_str)
        str = "\r" + " " * 78 + "\r" + str
        stderr.write(str)

    def _gen_size_str(self, size):
        kB = 1024.0
        MB = kB * 1024.0
        GB = MB * 1024.0
        if size < kB:
            size_str = "%dB" % (size, )
        elif size < MB:
            size_str = "%1.1fkB" % (size/kB,)
        elif size < GB:
            size_str = "%1.1fMB" % (size/MB,)
        else:
            size_str = "%1.1fGB" % (size/GB,)
        return size_str