normalize apiurl
[opensuse:osc.git] / osc / meter.py
1 #   This library is free software; you can redistribute it and/or
2 #   modify it under the terms of the GNU Lesser General Public
3 #   License as published by the Free Software Foundation; either
4 #   version 2.1 of the License, or (at your option) any later version.
5 #
6 #   This library is distributed in the hope that it will be useful,
7 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
8 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9 #   Lesser General Public License for more details.
10 #
11 #   You should have received a copy of the GNU Lesser General Public
12 #   License along with this library; if not, write to the
13 #      Free Software Foundation, Inc.,
14 #      59 Temple Place, Suite 330,
15 #      Boston, MA  02111-1307  USA
16
17 # this is basically a copy of python-urlgrabber's TextMeter class,
18 # with support added for dynamical sizing according to screen size.
19 # it uses getScreenWidth() scrapped from smart.
20 # 2007-04-24, poeml
21
22
23 from urlgrabber.progress import BaseMeter, format_time, format_number
24 import sys, os
25
26 def getScreenWidth():
27     import termios, struct, fcntl
28     s = struct.pack('HHHH', 0, 0, 0, 0)
29     try:
30         x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
31     except IOError:
32         return 80
33     return struct.unpack('HHHH', x)[1]
34
35
36 class TextMeter(BaseMeter):
37     def __init__(self, fo=sys.stderr, hide_finished=False):
38         BaseMeter.__init__(self)
39         self.fo = fo
40         self.hide_finished = hide_finished
41         try:
42             width = int(os.environ['COLUMNS'])
43         except (KeyError, ValueError):
44             width = getScreenWidth()
45
46
47         #self.unsized_templ = '\r%-60.60s    %5sB %s '
48         self.unsized_templ = '\r%%-%s.%ss    %%5sB %%s ' % (width *4/3, width*3/5)
49         #self.sized_templ = '\r%-45.45s %3i%% |%-15.15s| %5sB %8s '
50         self.bar_length = width/5
51         self.sized_templ = '\r%%-%s.%ss %%3i%%%% |%%-%s.%ss| %%5sB %%8s ' %(width*4/10, width*4/10, self.bar_length, self.bar_length)
52
53
54     def _do_start(self, *args, **kwargs):
55         BaseMeter._do_start(self, *args, **kwargs)
56         self._do_update(0)
57
58     def _do_update(self, amount_read, now=None):
59         etime = self.re.elapsed_time()
60         fetime = format_time(etime)
61         fread = format_number(amount_read)
62         #self.size = None
63         if self.text is not None:
64             text = self.text
65         else:
66             text = self.basename
67         if self.size is None:
68             out = self.unsized_templ % \
69                   (text, fread, fetime)
70         else:
71             rtime = self.re.remaining_time()
72             frtime = format_time(rtime)
73             frac = self.re.fraction_read()
74             bar = '='*int(self.bar_length * frac)
75
76             out = self.sized_templ % \
77                   (text, frac*100, bar, fread, frtime) + 'ETA '
78
79         self.fo.write(out)
80         self.fo.flush()
81
82     def _do_end(self, amount_read, now=None):
83         total_time = format_time(self.re.elapsed_time())
84         total_size = format_number(amount_read)
85         if self.text is not None:
86             text = self.text
87         else:
88             text = self.basename
89         if self.size is None:
90             out = self.unsized_templ % \
91                   (text, total_size, total_time)
92         else:
93             bar = '=' * self.bar_length
94             out = self.sized_templ % \
95                   (text, 100, bar, total_size, total_time) + '    '
96         if self.hide_finished:
97             self.fo.write('\r'+ ' '*len(out) + '\r')
98         else:
99             self.fo.write(out + '\n')
100         self.fo.flush()
101
102 # vim: sw=4 et