1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program 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
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
22 UNITS
= ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB']
25 """Return a string of the form '12.34 MiB' given a size in bytes."""
26 for i
in xrange(len(UNITS
) - 1, 0, -1):
29 return '%.2f %s' % ((float(size
) / base
), UNITS
[i
])
30 return str(size
) + ' ' + UNITS
[0]
32 def short_string(string
, length
):
33 """Returned a cut copy of the string, replace the middle with '~'."""
34 if len(string
) > length
:
36 return string
[:middle
] + '~' + string
[middle
- length
:]
39 def sanitize_string(string
):
41 if c
!= ' ' and c
.isspace():
44 def robust_char_decode(c
):
46 return c
.decode('UTF-8')
47 except UnicodeDecodeError:
49 blank_cleaned
= ''.join(map(clean_blank
, string
))
51 return blank_cleaned
.decode('UTF-8')
52 except UnicodeDecodeError:
53 return ''.join(map(robust_char_decode
, blank_cleaned
))
55 MIN_SIZE_REGEXP
= re
.compile('^([0-9]+)(.?)(.*)$')
57 def min_size_to_consider(min_size
='auto', height
=0):
58 if min_size
== 'auto':
60 match
= MIN_SIZE_REGEXP
.match(min_size
)
62 raise Exception, 'Cannot parse: ' + min_size
63 min_size
, letter
, suffix
= match
.groups()
64 units_letters
= [u
[0] for u
in UNITS
]
67 letter
= letter
.upper()
68 if not (letter
in units_letters
and suffix
in ('iB', '')):
69 raise Exception, 'Cannot parse unit in: ' + min_size
70 return int(min_size
) * 1024 ** units_letters
.index(letter
)
72 PROGRESS_CHARS
= ['/', '-', '\\', '|']
73 last_progress_char
= 0
74 last_progress_time
= time
.time()
76 def update_progress():
77 global last_progress_char
, last_progress_time
79 if now
- last_progress_time
> 0.04:
80 last_progress_char
= (last_progress_char
+ 1) % len(PROGRESS_CHARS
)
81 last_progress_time
= now
82 return PROGRESS_CHARS
[last_progress_char
]
84 class UINotAvailableException(Exception):