Recognizes if input is ogg or not.
[xiph/unicode.git] / positron / positron / progress.py
blobc236ed90645290b4913b271f0a6b6d234b154656
1 # -*- Mode: python -*-
3 # progress.py - progress meter
5 # Copyright (C) 2003, Xiph.org Foundation
7 # This file is part of positron.
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of a BSD-style license (see the COPYING file in the
11 # distribution).
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17 from sys import stderr
19 class Progress:
21 def sync_start(self, num_items):
22 stderr.write("Downloading %d files...\n" % (num_items,))
23 return true
25 def transfer_start(self, filename, size):
26 stderr.write("%s:\n")
28 def transfer_progress(self, filename, size, bytes_so_far, rate):
29 self._print_line(filename, size, bytes_so_far, rate)
31 def transfer_stop(self, filename, size):
32 self._print_line(filename, size, size, 0)
33 stderr.write("\n")
35 def sync_stop(self):
36 pass
38 def _print_line(self, filename, size, bytes_so_far, rate):
39 percent_str = "%3d%%" % (100 * bytes_so_far / size,)
41 max_bar_width = 38
42 if bytes_so_far < size:
43 bar_width = max_bar_width * bytes_so_far / size
44 bar = "=" * (bar_width - 1) + ">"
45 else:
46 bar = "=" * max_bar_width
48 size_str = self._gen_size_str(size)
50 if rate > 0:
51 rate_str = self._gen_size_str(rate) + "/s"
53 if bytes_so_far < size:
54 time_remaining = (size - bytes_so_far) / rate
55 seconds = time_remaining % 60
56 minutes = (time_remaining // 60) % 60
57 hours = time_remaining // 3600
59 if hours > 0:
60 eta_str = "ETA: %d:%02d:%02d" % (hours, minutes, seconds)
61 else:
62 eta_str = "ETA: %d:%02d" % (minutes, seconds)
63 else:
64 eta_str = ""
65 else:
66 rate_str = ""
67 eta_str = ""
69 str = "%-4s|%-*s|%8s %10s %-15s" % (percent_str, max_bar_width,
70 bar, size_str, rate_str, eta_str)
71 str = "\r" + " " * 78 + "\r" + str
72 stderr.write(str)
74 def _gen_size_str(self, size):
75 kB = 1024.0
76 MB = kB * 1024.0
77 GB = MB * 1024.0
78 if size < kB:
79 size_str = "%dB" % (size, )
80 elif size < MB:
81 size_str = "%1.1fkB" % (size/kB,)
82 elif size < GB:
83 size_str = "%1.1fMB" % (size/MB,)
84 else:
85 size_str = "%1.1fGB" % (size/GB,)
86 return size_str