Logo Search packages:      
Sourcecode: sabayon version File versions  Download package

def lib::cache::cacheRepository::get_resource (   self,
  URL 
)

Get a resource from the cache. It may fetch it from the network
 or use a local copy. It returns a Python file liek open() would.
 If passed a filename it will accept it if absolute.
 The return value is an absolute path to a local file.

Definition at line 209 of file cache.py.

00209                                :
        """Get a resource from the cache. It may fetch it from the network
         or use a local copy. It returns a Python file liek open() would.
         If passed a filename it will accept it if absolute.
         The return value is an absolute path to a local file."""
      file = None
      try:
          decomp = urlparse.urlparse(URL)
          if decomp[2] == URL:
              file = URL
      except:
          file = URL
      if file != None:
          if file[0] != '/':
              return None
          try:
              return file
          except:
              dprint("Failed to read %s", file)
              return None
      else:
          self.__check_directory()
          filename = self.directory + "/" + self.__URL_mapping(URL)
          timestamp = self.__catalog_lookup(URL)
          last_modified = None
          try:
              request = urllib2.Request(URL)
            if timestamp != None and timestamp != "":
                request.add_header('If-Modified-Since', timestamp)
            except:
              dprint("Failed to create request for %s", URL)
              return None
          try:
              opener = urllib2.build_opener()
            # TODO handle time outs there ....
            datastream = opener.open(request)
            try:
                last_modified = datastream.headers.dict['last-modified']
            except:
                last_modified = None
              data = datastream.read()
            datastream.close()
          except:
              dprint("Resource not available or older using cache")
            try:
                info = os.stat(filename)
                return filename
            except:
                dprint("Failed to find cache file %s", filename)
                return None
            try:
              fd = open(filename, "w")
            fd.write(data)
            fd.close()
            self.__update_catalog(URL, last_modified)
          except:
              dprint("Failed to write cache file %s", filename)
            return None
          return filename

default_cache = None


Generated by  Doxygen 1.6.0   Back to index