class ViewerSession(object): archFiles = None items = None def __init__(self): self.archHandle = None self.lastAccess = time.time() self.pruneAge = 60*120 # in Seconds, prune if no access for 120 minutes def shouldPrune(self): lastChange = time.time() - self.lastAccess if lastChange > self.pruneAge: return True else: return False def checkOpenArchive(self, archPath): if not self.archHandle or self.archHandle.archPath != archPath: self.archHandle = ArchiveReader(archPath) self.lastAccess = time.time() def getItemByInternalPath(self, internalPath): itemContent = self.archHandle.open(internalPath) return itemContent, internalPath def __del__(self): if self.archHandle: del self.archHandle
def checkOpenArchive(self, archPath): if not self.archHandle or self.archHandle.archPath != archPath: self.archHandle = ArchiveReader(archPath) self.buildImageLookupDict() self.lastAccess = time.time()
class ViewerSession(object): archFiles = None items = None def __init__(self): self.archHandle = None self.lastAccess = time.time() self.pruneAge = 60*120 # in Seconds, prune if no access for 120 minutes def shouldPrune(self): lastChange = time.time() - self.lastAccess if lastChange > self.pruneAge: return True else: return False def checkOpenArchive(self, archPath): if not self.archHandle or self.archHandle.archPath != archPath: self.archHandle = ArchiveReader(archPath) self.buildImageLookupDict() self.lastAccess = time.time() def getImageNames(self): if not self.archHandle: raise ValueError() self.archFiles = self.archHandle.getFileList() ret = [] for item in self.archFiles: if nt.isProbablyImage(item): ret.append(item) if not ret: raise ValueError("No Images in archive! \n Archive contents = %s" % "\n ".join(self.archFiles)) return ret def buildImageLookupDict(self): names = self.getImageNames() # Natsorted evaluates '-' to mean a negative number # This breaks sorting (it reverses it) for filenames where the spaces # have been replaced with hyphens. # As a work-around, and since I do not currently anticipate the need to # properly sort negative numbers, use a lambda # to replace all hyphens with spaces for sorting. names = natsorted(names, key=lambda x: x.replace("-", " ")) self.items = dict(zip(range(len(names)), names)) def getKeys(self): try: keys = list(self.items.keys()) return keys except AttributeError: raise ValueError("No Images in archive! \n Archive contents = %s" % "\n ".join(self.archFiles)) def getItemByKey(self, itemKey): if not itemKey in self.items: raise KeyError("Invalid key. Not in archive!") internalPath = self.items[itemKey] itemContent = self.archHandle.open(internalPath) return itemContent, internalPath def __del__(self): if self.archHandle: del self.archHandle
class ViewerSession(object): archFiles = None items = None def __init__(self): self.archHandle = None self.lastAccess = time.time() self.pruneAge = 60 * 120 # in Seconds, prune if no access for 120 minutes def shouldPrune(self): lastChange = time.time() - self.lastAccess if lastChange > self.pruneAge: return True else: return False def checkOpenArchive(self, archPath): if not self.archHandle or self.archHandle.archPath != archPath: self.archHandle = ArchiveReader(archPath) self.buildImageLookupDict() self.lastAccess = time.time() def getImageNames(self): if not self.archHandle: raise ValueError() ret = [] fcnt = 0 for item in self.archHandle.getFileList(): fcnt += 1 if nt.isProbablyImage(item): ret.append(item) if not ret: raise ValueError( "No Images in archive (but %s files)! \n Archive contents = %s" % (fcnt, "\n ".join(self.archHandle.getFileList()))) return ret def buildImageLookupDict(self): names = self.getImageNames() # Natsorted evaluates '-' to mean a negative number # This breaks sorting (it reverses it) for filenames where the spaces # have been replaced with hyphens. # As a work-around, and since I do not currently anticipate the need to # properly sort negative numbers, use a lambda # to replace all hyphens with spaces for sorting. names = natsorted(names, key=lambda x: x.replace("-", " ")) self.items = dict(zip(range(len(names)), names)) def getKeys(self): try: keys = list(self.items.keys()) return keys except AttributeError: raise ValueError("No Images in archive! \n Archive contents = %s" % "\n ".join(self.archHandle.getFileList())) def getItemByKey(self, itemKey): if not itemKey in self.items: raise KeyError("Invalid key. Not in archive!") internalPath = self.items[itemKey] itemContent = self.archHandle.open(internalPath) return itemContent, internalPath def __del__(self): if self.archHandle: del self.archHandle