"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/ui/utils.py" (11 Mar 2007, 3000 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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.
    5 #
    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.
   10 #
   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.
   14 #
   15 # See the COPYING file for license information.
   16 #
   17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   18 
   19 import re
   20 import time
   21 
   22 UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB']
   23 
   24 def human_unit(size):
   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):
   27         base = 1 << (10 * i)
   28         if 2 * base < size:
   29             return '%.2f %s' % ((float(size) / base), UNITS[i])
   30     return str(size) + ' ' + UNITS[0]
   31 
   32 def short_string(string, length):
   33     """Returned a cut copy of the string, replace the middle with '~'."""
   34     if len(string) > length:
   35         middle = length / 2
   36         return string[:middle] + '~' + string[middle - length:]
   37     return string
   38 
   39 def sanitize_string(string):
   40     def clean_blank(c):
   41         if c != ' ' and c.isspace():
   42             return '?'
   43         return c
   44     def robust_char_decode(c):
   45         try:
   46             return c.decode('UTF-8')
   47         except UnicodeDecodeError:
   48             return '?'
   49     blank_cleaned = ''.join(map(clean_blank, string))
   50     try:
   51         return blank_cleaned.decode('UTF-8')
   52     except UnicodeDecodeError:
   53         return ''.join(map(robust_char_decode, blank_cleaned))
   54 
   55 MIN_SIZE_REGEXP = re.compile('^([0-9]+)(.?)(.*)$')
   56 
   57 def min_size_to_consider(min_size='auto', height=0):
   58     if min_size == 'auto':
   59         return 1.0 / height
   60     match = MIN_SIZE_REGEXP.match(min_size)
   61     if not match:
   62         raise Exception, 'Cannot parse: ' + min_size
   63     min_size, letter, suffix = match.groups()
   64     units_letters = [u[0] for u in UNITS]
   65     if not letter:
   66         letter = 'B'
   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)
   71 
   72 PROGRESS_CHARS = ['/', '-', '\\', '|']
   73 last_progress_char = 0
   74 last_progress_time = time.time()
   75 
   76 def update_progress():
   77     global last_progress_char, last_progress_time
   78     now = time.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]
   83 
   84 class UINotAvailableException(Exception):
   85     pass