home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / Loader.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  2.0 KB  |  74 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*- 
  2. #
  3. # Copyright (C) 2006 - Jonathan Matthew
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  17.  
  18. import gobject
  19.  
  20. try:
  21.     import gnomevfs
  22.     use_gnomevfs = True
  23. except:
  24.     import urllib
  25.     use_gnomevfs = False
  26.  
  27.  
  28. class GnomeVFSAsyncSrc (object):  
  29.     def __init__ (self):
  30.             self.chunk = 4096
  31.  
  32.     def read_cb (self, handle, buffer, exc_type, bytes_requested, (data, callback, args)):
  33.         if exc_type:
  34.             if issubclass (exc_type, gnomevfs.EOFError):
  35.                 gobject.idle_add (callback, data, *args)
  36.                 handle.close (lambda *args: None)
  37.             else:
  38.                 gobject.idle_add (callback, None, *args)
  39.                 handle.close (lambda *args: None)
  40.             return
  41.              
  42.         data += buffer
  43.         handle.read (self.chunk, self.read_cb, (data, callback, args))
  44.  
  45.     def open_cb (self, handle, exc_type, (data, callback, args)):
  46.         if exc_type:
  47.             gobject.idle_add (callback, None, *args)
  48.             return
  49.  
  50.         handle.read (self.chunk, self.read_cb, (data, callback, args))
  51.     
  52.     def get_url (self, url, callback, *args):
  53.         gnomevfs.async.open (url, self.open_cb, data=("", callback, args))
  54.  
  55.  
  56. class URLLibSrc (object):
  57.     def get_url (self, url, callback, *args):
  58.             try:
  59.                 sock = urllib.urlopen (url)
  60.                 data = sock.read ()
  61.                 sock.close ()
  62.                 callback (data, *args)
  63.             except:
  64.                 callback (None, *args)
  65.                 raise
  66.  
  67.  
  68. def Loader ():
  69.     if use_gnomevfs:
  70.         return GnomeVFSAsyncSrc ()
  71.     else:
  72.         return URLLibSrc ()
  73.