Beispiel #1
0
 def key(self, obj):        
     """Return a cache key (relative path to file in cache) for an object"""
     if isnumpy(obj):
         # Key is byte view sha1 hash with .h5 extension           
         byteview = obj.view(numpy.uint8)
         key = str(hashlib.sha1(byteview).hexdigest()) + '.h5' 
     elif isurl(obj):
         # key is URL filename with an appended hash (for uniqueness)
         p = urlparse.urlsplit(obj)
         urlquery = urlparse.urlunsplit([p[0],p[1],p[2],p[3],None])        
         urlpath = urlparse.urlunsplit([p[0],p[1],p[2],None,None])
         urlhash = self._hash(obj)
         (filename, ext) = splitextension(path.basename(urlpath))
         key = str(urlhash) + str(ext)
     elif os.path.isfile(obj):
         # within cache?
         filebase = obj.split(self.root(),1)
         if len(filebase) == 2:
             # key is subpath within cache
             key = filebase[1][1:]
         else:
             # key is filename with unique appended hash
             (head, tail) = os.path.split(obj)
             (filename, ext) = splitextension(tail)                 
             namehash = hashlib.sha1(tail).hexdigest()                 
             key = filename + '_' + str(namehash[0:7]) + ext
     elif (path.isfile(self.abspath(obj)) or path.isdir(self.abspath(obj))):
         key = obj   # Already a cache key
     elif isstring(obj):
         key = obj   # Use arbitrary string if not file or url
     else:
         raise CacheError('[bobo.cache][ERROR]: Unsupported object for constructing key')
     return key
Beispiel #2
0
    def put(self, obj, key=None, timeout=None, sha1=None):
        """Put a URI or numpy object into cache with the provided cache key"""
        if key is None:
            key = self.key(obj)        
        if self.iscached(key):
            raise CacheError('[bobo.cache][Error]: Key collision! Existing object in cache with key "%s"' % key)
            
        # Numpy object - export to file in cache with provided key
        if isnumpy(obj):
            quietprint('[bobo.cache][PUT]: Exporting numpy object to cache with key "' + key + '"')                                                                             
            f = h5py.File(self.abspath(key), 'a')
            f[key] = obj
            f.close()

        # URL - download and save to cache with provided key
        elif isurl(obj):
            quietprint('[bobo.cache][PUT]: "%s" key "%s"' % (obj, key))                                                                                             
            filename = self._download(obj, timeout=timeout)
            shutil.move(filename, self.abspath(key))

            
            
        # Unsupported type!
        else:
            raise CacheError('[bobo.cache][ERROR]: Unsupported object type for PUT')
            
        # Return cache key 
        return key