Updated Finnish translation
[rhythmbox.git] / plugins / rb / Loader.py
blob8b3146e8ca97d826056051f25c070c2fdfa28bdd
1 # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
3 # Copyright (C) 2006 - Jonathan Matthew
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 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 import gobject
21 try:
22 import gnomevfs
23 use_gnomevfs = True
24 except:
25 import urllib
26 use_gnomevfs = False
29 class GnomeVFSAsyncSrc (object):
30 def __init__ (self):
31 self.chunk = 4096
33 def read_cb (self, handle, buffer, exc_type, bytes_requested, (data, callback, args)):
34 if exc_type:
35 if issubclass (exc_type, gnomevfs.EOFError):
36 gobject.idle_add (callback, data, *args)
37 handle.close (lambda *args: None)
38 else:
39 gobject.idle_add (callback, None, *args)
40 handle.close (lambda *args: None)
41 return
43 data += buffer
44 handle.read (self.chunk, self.read_cb, (data, callback, args))
46 def open_cb (self, handle, exc_type, (data, callback, args)):
47 if exc_type:
48 gobject.idle_add (callback, None, *args)
49 return
51 handle.read (self.chunk, self.read_cb, (data, callback, args))
53 def get_url (self, url, callback, *args):
54 gnomevfs.async.open (url, self.open_cb, data=("", callback, args))
57 class URLLibSrc (object):
58 def get_url (self, url, callback, *args):
59 try:
60 sock = urllib.urlopen (url)
61 data = sock.read ()
62 sock.close ()
63 callback (data, *args)
64 except:
65 callback (None, *args)
66 raise
69 def Loader ():
70 if use_gnomevfs:
71 return GnomeVFSAsyncSrc ()
72 else:
73 return URLLibSrc ()