home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / rb / Loader.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.9 KB  |  350 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gobject
  5. import gtk
  6.  
  7. def callback_with_gdk_lock(callback, data, args):
  8.     gtk.gdk.threads_enter()
  9.     v = callback(data, *args)
  10.     gtk.gdk.threads_leave()
  11.     return v
  12.  
  13.  
  14. class GioLoader(object):
  15.     
  16.     def __init__(self):
  17.         self._cancel = gio.Cancellable()
  18.  
  19.     
  20.     def _contents_cb(self, file, result):
  21.         
  22.         try:
  23.             (contents, length, etag) = file.load_contents_finish(result)
  24.             callback_with_gdk_lock(self.callback, contents, self.args)
  25.         except gio.Error:
  26.             e = None
  27.             callback_with_gdk_lock(self.callback, None, self.args)
  28.  
  29.  
  30.     
  31.     def get_url(self, url, callback, *args):
  32.         self.url = url
  33.         self.callback = callback
  34.         self.args = args
  35.         
  36.         try:
  37.             file = gio.File(url)
  38.             file.load_contents_async(callback = self._contents_cb, cancellable = self._cancel)
  39.         except Exception:
  40.             e = None
  41.             print 'error getting contents of %s: %s' % e
  42.             callback(None, *args)
  43.  
  44.  
  45.     
  46.     def cancel(self):
  47.         self._cancel.cancel()
  48.  
  49.  
  50.  
  51. class GioChunkLoader(object):
  52.     
  53.     def __init__(self):
  54.         self._cancel = gio.Cancellable()
  55.  
  56.     
  57.     def _callback(self, result):
  58.         return self.callback(result, self.total, *self.args)
  59.  
  60.     
  61.     def _callback_gdk(self, result):
  62.         gtk.gdk.threads_enter()
  63.         v = self._callback(result)
  64.         gtk.gdk.threads_leave()
  65.         return v
  66.  
  67.     
  68.     def _read_cb(self, stream, result):
  69.         
  70.         try:
  71.             data = stream.read_finish(result)
  72.         except gio.Error:
  73.             e = None
  74.             print 'error reading file %s: %s' % (self.uri, e.message)
  75.             stream.close()
  76.             self._callback_gdk(e)
  77.  
  78.         if self._callback_gdk(data) is not False and data:
  79.             
  80.             def again():
  81.                 stream.read_async(self.chunksize, self._read_cb, cancellable = self._cancel)
  82.                 return False
  83.  
  84.             gobject.idle_add(again)
  85.         else:
  86.             stream.close()
  87.  
  88.     
  89.     def _open_cb(self, file, result):
  90.         
  91.         try:
  92.             stream = file.read_finish(result)
  93.         except gio.Error:
  94.             e = None
  95.             print 'error reading file %s: %s' % (self.uri, e.message)
  96.             self._callback_gdk(e)
  97.  
  98.         stream.read_async(self.chunksize, self._read_cb, cancellable = self._cancel)
  99.  
  100.     
  101.     def _info_cb(self, file, result):
  102.         
  103.         try:
  104.             info = file.query_info_finish(result)
  105.             self.total = info.get_attribute_uint64(gio.FILE_ATTRIBUTE_STANDARD_SIZE)
  106.             file.read_async(self._open_cb, cancellable = self._cancel)
  107.         except gio.Error:
  108.             e = None
  109.             print 'error checking size of source file %s: %s' % (self.uri, e.message)
  110.             self._callback_gdk(e)
  111.  
  112.  
  113.     
  114.     def get_url_chunks(self, uri, chunksize, want_size, callback, *args):
  115.         
  116.         try:
  117.             self.uri = uri
  118.             self.chunksize = chunksize
  119.             self.total = 0
  120.             self.callback = callback
  121.             self.args = args
  122.             file = gio.File(uri)
  123.             if want_size:
  124.                 file.query_info_async(self._info_cb, gio.FILE_ATTRIBUTE_STANDARD_SIZE, cancellable = self._cancel)
  125.             else:
  126.                 file.read_async(self._open_cb, cancellable = self.cancel)
  127.         except gio.Error:
  128.             e = None
  129.             print 'error reading file %s: %s' % (uri, e.message)
  130.             self._callback(e)
  131.  
  132.  
  133.     
  134.     def cancel(self):
  135.         self._cancel.cancel()
  136.  
  137.  
  138.  
  139. class GioUpdateCheck(object):
  140.     
  141.     def __init__(self):
  142.         self._cancel = gio.Cancellable()
  143.  
  144.     
  145.     def _file_info_cb(self, file, result):
  146.         
  147.         try:
  148.             rfi = file.query_info_finish(result)
  149.             remote_mod = rfi.get_attribute_uint64(gio.FILE_ATTRIBUTE_TIME_MODIFIED)
  150.             callback_with_gdk_lock(self.callback, remote_mod != self.local_mod, self.args)
  151.         except Exception:
  152.             e = None
  153.             print 'error checking for update: %s' % e
  154.             callback_with_gdk_lock(self.callback, False, self.args)
  155.  
  156.  
  157.     
  158.     def check_for_update(self, local, remote, callback, *args):
  159.         self.local = local
  160.         self.remote = remote
  161.         self.callback = callback
  162.         self.args = args
  163.         
  164.         try:
  165.             lf = gio.File(local)
  166.             lfi = lf.query_info(gio.FILE_ATTRIBUTE_TIME_MODIFIED)
  167.             self.local_mod = lfi.get_attribute_uint64(gio.FILE_ATTRIBUTE_TIME_MODIFIED)
  168.             rf = gio.File(remote)
  169.             rf.query_info_async(self._file_info_cb, gio.FILE_ATTRIBUTE_TIME_MODIFIED, cancellable = self._cancel)
  170.         except Exception:
  171.             e = None
  172.             print 'error checking for update: %s' % e
  173.             self.callback(True, *self.args)
  174.  
  175.  
  176.     
  177.     def cancel(self):
  178.         self._cancel.cancel()
  179.  
  180.  
  181.  
  182. class GnomeVFSLoader(object):
  183.     
  184.     def __init__(self):
  185.         self.chunk = 4096
  186.  
  187.     
  188.     def _read_cb(self, handle, buffer, exc_type, bytes_req):
  189.         if exc_type:
  190.             if issubclass(exc_type, gnomevfs.EOFError):
  191.                 callback_with_gdk_lock(self.callback, self.data, self.args)
  192.                 handle.close((lambda : pass))
  193.             else:
  194.                 callback_with_gdk_lock(self.callback, None, self.args)
  195.                 handle.close((lambda : pass))
  196.             return None
  197.         self.data += buffer
  198.         handle.read(self.chunk, self._read_cb)
  199.  
  200.     
  201.     def _open_cb(self, handle, exc_type):
  202.         if exc_type:
  203.             callback_with_gdk_lock(self.callback, None, self.args)
  204.             return None
  205.         self.handle = handle
  206.         self.data = ''
  207.         handle.read(self.chunk, self._read_cb)
  208.  
  209.     
  210.     def get_url(self, url, callback, *args):
  211.         self.url = url
  212.         self.callback = callback
  213.         self.args = args
  214.         gnomevfs.async.open(url, self._open_cb)
  215.  
  216.     
  217.     def cancel(self):
  218.         self.handle.cancel()
  219.  
  220.  
  221.  
  222. class GnomeVFSChunkLoader(object):
  223.     
  224.     def __init__(self):
  225.         pass
  226.  
  227.     
  228.     def _callback(self, result):
  229.         return self.callback(result, self.total, *self.args)
  230.  
  231.     
  232.     def _callback_gdk(self, result):
  233.         gtk.gdk.threads_enter()
  234.         v = self._callback(result)
  235.         gtk.gdk.threads_leave()
  236.         return v
  237.  
  238.     
  239.     def _read_cb(self, handle, buffer, exc_type, bytes_requested):
  240.         if exc_type:
  241.             if issubclass(exc_type, gnomevfs.EOFError):
  242.                 self._callback_gdk(None)
  243.             else:
  244.                 self._callback_gdk(exc_type())
  245.             handle.close((lambda : pass))
  246.         elif self._callback_gdk(buffer) is False:
  247.             handle.close((lambda : pass))
  248.         else:
  249.             handle.read(self.chunksize, self._read_cb)
  250.  
  251.     
  252.     def _open_cb(self, handle, exc_type):
  253.         if exc_type:
  254.             self._callback_gdk(exc_type())
  255.         else:
  256.             handle.read(self.chunksize, self._read_cb)
  257.  
  258.     
  259.     def _info_cb(self, handle, results):
  260.         
  261.         try:
  262.             (uri, exc, info) = results[0]
  263.             self.total = info.size
  264.         except ValueError:
  265.             pass
  266.  
  267.         self.handle = gnomevfs.async.open(self.vfs_uri, self._open_cb)
  268.  
  269.     
  270.     def get_url_chunks(self, uri, chunksize, want_size, callback, *args):
  271.         self.uri = uri
  272.         self.chunksize = chunksize
  273.         self.total = 0
  274.         self.callback = callback
  275.         self.args = args
  276.         self.vfs_uri = gnomevfs.URI(uri)
  277.         if want_size:
  278.             self.handle = gnomevfs.async.get_file_info((self.vfs_uri,), self._info_cb)
  279.         else:
  280.             self.handle = gnomevfs.async.open(self.vfs_uri, self._open_cb)
  281.  
  282.     
  283.     def cancel(self):
  284.         if self.handle:
  285.             self.handle.cancel()
  286.         
  287.  
  288.  
  289.  
  290. class GnomeVFSUpdateCheck(object):
  291.     
  292.     def __init__(self):
  293.         pass
  294.  
  295.     
  296.     def _info_cb(self, handle, results):
  297.         (local_uri, local_exc, local_info) = results[0]
  298.         (remote_uri, remote_exc, remote_info) = results[1]
  299.         result = True
  300.         if remote_exc:
  301.             print 'error checking remote URI %s: %s' % (self.remote, remote_exc)
  302.             result = False
  303.         elif local_exc:
  304.             if issubclass(local_exc, gnomevfs.NotFoundError):
  305.                 print 'local URI %s not found' % self.local
  306.             else:
  307.                 print 'error checking local URI %s: %s' % (self.local, local_exc)
  308.         else:
  309.             
  310.             try:
  311.                 result = remote_info.mtime > local_info.mtime
  312.             except ValueError:
  313.                 e = None
  314.                 print 'error comparing modification times: %s' % e
  315.  
  316.         callback_with_gdk_lock(self.callback, result, self.args)
  317.  
  318.     
  319.     def check_for_update(self, local, remote, callback, *args):
  320.         self.callback = callback
  321.         self.args = args
  322.         self.local = local
  323.         self.remote = remote
  324.         uris = (gnomevfs.URI(local), gnomevfs.URI(remote))
  325.         self.handle = gnomevfs.async.get_file_info(uris, self._info_cb)
  326.  
  327.     
  328.     def cancel(self):
  329.         self.handle.cancel()
  330.  
  331.  
  332. use_gio = False
  333.  
  334. try:
  335.     import gio
  336.     if gio.pygio_version > (2, 15, 4):
  337.         use_gio = True
  338. except:
  339.     pass
  340.  
  341. if use_gio:
  342.     Loader = GioLoader
  343.     ChunkLoader = GioChunkLoader
  344.     UpdateCheck = GioUpdateCheck
  345. else:
  346.     import gnomevfs
  347.     Loader = GnomeVFSLoader
  348.     ChunkLoader = GnomeVFSChunkLoader
  349.     UpdateCheck = GnomeVFSUpdateCheck
  350.