Ejemplo n.º 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
Ejemplo n.º 2
0
 def _hash(self, url, prettyhash=_prettyhash):
     """Compute a SHA1 hash of a url to generate a unique cache filename for a given url"""
     p = urlparse.urlsplit(url)
     urlquery = urlparse.urlunsplit([p[0],p[1],p[2],p[3],None])
     urlpath = urlparse.urlunsplit([p[0],p[1],p[2],None,None])        
     (filename, ext) = splitextension(urlpath)
     #urlopt = self._url_fragment_options(url)
     urlhash = hashlib.sha1(urlquery).hexdigest()
     if prettyhash:    
         return path.basename(filename) + '_' + urlhash[0:7]
     else:
         return urlhash