Package screenlets :: Package plugins :: Module Loader

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.Loader

     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  #
    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.
    14  #
    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.
    18
    19  import gobject
    20
    21  #try:
    22  #       import gnomevfs
    23  #       use_gnomevfs = True
    24  #except:
    25  import urllib
    26  use_gnomevfs = False
    27
    28
    29 -class GnomeVFSAsyncSrc (object):
    30 -        def __init__ (self):
    31                  self.chunk = 4096
    32
    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
    42
    43                  data += buffer
    44                  handle.read (self.chunk, self.read_cb, (data, callback, args))
    45
    46 -        def open_cb (self, handle, exc_type, (data, callback, args)):
    47                  if exc_type:
    48                          gobject.idle_add (callback, None, *args)
    49                          return
    50
    51                  handle.read (self.chunk, self.read_cb, (data, callback, args))
    52
    53 -        def get_url (self, url, callback, *args):
    54                  gnomevfs.async.open (url, self.open_cb, data=("", callback, args))
    55
    56
    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
    67
    68
    69 -def Loader ():
    70          if use_gnomevfs:
    71                  return GnomeVFSAsyncSrc ()
    72          else:
    73                  return URLLibSrc ()
    74