1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | # |
---|
22 | |
---|
23 | """This module defines classes to display progress reports.""" |
---|
24 | |
---|
25 | import sys |
---|
26 | import time |
---|
27 | |
---|
28 | class Percent : |
---|
29 | """A class to display progress.""" |
---|
30 | def __init__(self, app, size=None) : |
---|
31 | """Initializes the engine.""" |
---|
32 | self.isatty = sys.stdout.isatty() |
---|
33 | self.app = app |
---|
34 | self.size = None |
---|
35 | if size : |
---|
36 | self.setSize(size) |
---|
37 | self.previous = None |
---|
38 | self.before = time.time() |
---|
39 | |
---|
40 | def setSize(self, size) : |
---|
41 | """Sets the total size.""" |
---|
42 | self.number = 0 |
---|
43 | self.size = size |
---|
44 | if size : |
---|
45 | self.factor = 100.0 / float(size) |
---|
46 | |
---|
47 | def display(self, msg) : |
---|
48 | """Displays the value.""" |
---|
49 | if self.isatty : |
---|
50 | self.app.display(msg) |
---|
51 | sys.stdout.flush() |
---|
52 | |
---|
53 | def oneMore(self) : |
---|
54 | """Increments internal counter.""" |
---|
55 | if self.size : |
---|
56 | self.number += 1 |
---|
57 | percent = "%.02f" % (float(self.number) * self.factor) |
---|
58 | if percent != self.previous : # optimize for large number of items |
---|
59 | self.display("\r%s%%" % percent) |
---|
60 | self.previous = percent |
---|
61 | |
---|
62 | def done(self) : |
---|
63 | """Displays the 'done' message.""" |
---|
64 | after = time.time() |
---|
65 | if self.size : |
---|
66 | try : |
---|
67 | speed = self.size / ((after - self.before) + 0.00000000001) # adds an epsilon to avoid an user's problem I can't reproduce... |
---|
68 | except ZeroDivisionError : |
---|
69 | speed = 1 # Fake value in case of division by zero, shouldn't happen anyway with the epsilon above... |
---|
70 | self.display("\r100.00%%\r \r%s. %s : %.2f %s.\n" \ |
---|
71 | % (_("Done"), _("Average speed"), speed, _("entries per second"))) |
---|
72 | else : |
---|
73 | self.display("\r100.00%%\r \r%s.\n" % _("Done")) |
---|