home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / common.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  6.1 KB  |  168 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. import statvfs
  6. import sys
  7. import tempfile
  8. from gettext import gettext as _
  9. from os import path
  10.  
  11. class ApplicationLocations:
  12.     
  13.     def __init__(self, root, appname):
  14.         self.root = path.abspath(root)
  15.         pyver = 'python%d.%d' % sys.version_info[0:2]
  16.         self.lib = path.join(root, 'lib', pyver, 'site-packages')
  17.         self.data = path.join(root, 'share', appname)
  18.         self.bin = path.join(root, 'bin')
  19.         self.locale = path.join(root, 'share', 'locale')
  20.  
  21.     
  22.     def get_data_file(self, filename):
  23.         return path.join(self.data, filename)
  24.  
  25.     
  26.     def get_lib_file(self, filename):
  27.         return path.join(self.lib, filename)
  28.  
  29.     
  30.     def get_bin_file(self, filename):
  31.         return path.join(self.bin, filename)
  32.  
  33.     
  34.     def get_locale_file(self, filename):
  35.         return path.join(self.locale, filename)
  36.  
  37.     
  38.     def get_root_file(self, filename):
  39.         return path.join(self.root, filename)
  40.  
  41.  
  42.  
  43. class SerpentineError(StandardError):
  44.     pass
  45.  
  46.  
  47. class SerpentineCacheError(SerpentineError):
  48.     INVALID = 1
  49.     NO_SPACE = 2
  50.     
  51.     def __init__(self, error_id, error_message):
  52.         self._SerpentineCacheError__error_id = error_id
  53.         self._SerpentineCacheError__error_message = error_message
  54.  
  55.     error_id = property((lambda self: self._SerpentineCacheError__error_id))
  56.     error_message = property((lambda self: self._SerpentineCacheError__error_message))
  57.     
  58.     def __str__(self):
  59.         return '[Error %d] %s' % (self.error_id, self.error_message)
  60.  
  61.  
  62.  
  63. class SerpentineNotSupportedError(SerpentineError):
  64.     pass
  65.  
  66.  
  67. def __hig_bytes(bytes):
  68.     hig_desc = [
  69.         (_('GByte'), _('GBytes')),
  70.         (_('MByte'), _('MBytes')),
  71.         (_('KByte'), _('KByte')),
  72.         (_('byte'), _('bytes'))]
  73.     (value, strings) = __decompose_bytes(bytes, 30, hig_desc)
  74.     return '%.1f %s' % (value, __plural(value, strings))
  75.  
  76.  
  77. def __decompose_bytes(bytes, offset, hig_desc):
  78.     if bytes == 0:
  79.         return (0.0, hig_desc[-1:])
  80.     
  81.     if offset == 0:
  82.         return (float(bytes), hig_desc[-1:])
  83.     
  84.     part = bytes >> offset
  85.     if part > 0:
  86.         sub_part = part ^ (part >> offset) << offset
  87.         return ((part * 1024 + sub_part) / 1024.0, hig_desc[0])
  88.     else:
  89.         del hig_desc[0]
  90.         return __decompose_bytes(bytes, offset - 10, hig_desc)
  91.  
  92.  
  93. def __plural(value, strings):
  94.     if value == 1:
  95.         return strings[0]
  96.     else:
  97.         return strings[1]
  98.  
  99.  
  100. class SafeFileWrite:
  101.     '''This class enables the user to safely write the contents to a file and
  102.     if something wrong happens the original file will not be damaged. It writes
  103.     the contents in a temporary file and when the file descriptor is closed the
  104.     contents are transfered to the real filename.'''
  105.     
  106.     def __init__(self, filename):
  107.         self.filename = filename
  108.         basedir = path.dirname(filename)
  109.         (fd, self.tmp_filename) = tempfile.mkstemp(dir = basedir)
  110.         os.close(fd)
  111.         self.fd = open(self.tmp_filename, 'w')
  112.  
  113.     
  114.     def __getattr__(self, attr):
  115.         return getattr(self.fd, attr)
  116.  
  117.     
  118.     def close(self):
  119.         self.fd.close()
  120.         
  121.         try:
  122.             os.unlink(self.filename)
  123.         except:
  124.             pass
  125.  
  126.         os.rename(self.tmp_filename, self.filename)
  127.  
  128.     
  129.     def abort(self):
  130.         '''Abort is used to cancel the changes made and remove the temporary
  131.         file. The original filename will not be altered.'''
  132.         self.fd.close()
  133.         
  134.         try:
  135.             os.unlink(self.tmp_filename)
  136.         except:
  137.             pass
  138.  
  139.  
  140.  
  141.  
  142. def validate_music_list(music_list, preferences):
  143.     secs = 0
  144.     for music in music_list:
  145.         if not preferences.pool.is_available(music['location']):
  146.             secs += music['duration']
  147.             continue
  148.     
  149.     size_needed = secs * 0x2B110L
  150.     
  151.     try:
  152.         s = os.statvfs(preferences.temporaryDir)
  153.         if not preferences.temporaryDirIsOk:
  154.             raise AssertionError
  155.     except OSError:
  156.         AssertionError = None
  157.         raise SerpentineCacheError(SerpentineCacheError.INVALID, _('Please check if the cache location exists and has writable permissions.'))
  158.  
  159.     size_avail = s[statvfs.F_BAVAIL] * long(s[statvfs.F_BSIZE])
  160.     if size_avail - size_needed < 0:
  161.         preferences.pool.clear()
  162.     
  163.     size_avail = s[statvfs.F_BAVAIL] * long(s[statvfs.F_BSIZE])
  164.     if size_avail - size_needed < 0:
  165.         raise SerpentineCacheError(SerpentineCacheError.NO_SPACE, _('Remove some music tracks or make sure your cache location location has enough free space (about %s).') % __hig_bytes(size_needed - size_avail))
  166.     
  167.  
  168.