progress.py
来自「Harvestman-最新版本」· Python 代码 · 共 499 行 · 第 1/2 页
PY
499 行
self.__lock.acquire() try: if self.__done or subkey in self.__subdone: return (subcurrent, subtotal, fragment, subdata) = self.__subprogress[subkey] subcurrent += value if subcurrent > subtotal: subcurrent = subtotal self.__subprogress[subkey] = (subcurrent, subtotal, fragment, subdata) if subcurrent == subtotal: self.__lasttime = 0 finally: self.__lock.release() def addSubTotal(self, subkey, value): self.__lock.acquire() try: if self.__done or subkey in self.__subdone: return (subcurrent, subtotal, fragment, subdata) = self.__subprogress[subkey] self.__subprogress[subkey] = (subcurrent, subtotal+value, fragment, subdata) finally: self.__lock.release() def setDone(self): self.__lock.acquire() try: current, total, data = self.__progress self.__progress = (total, total, data) self.__lasttime = 0 finally: self.__lock.release() def setSubDone(self, subkey): self.__lock.acquire() try: if subkey in self.__subdone: return (subcurrent, subtotal, fragment, subdata) = self.__subprogress[subkey] if subcurrent != subtotal: self.__subprogress[subkey] = (subtotal, subtotal, fragment, subdata) self.__lasttime = 0 finally: self.__lock.release() def setStopped(self): self.__lock.acquire() self.__done = True self.__lasttime = 0 self.__lock.release() def setSubStopped(self, subkey): self.__lock.acquire() self.__subdone[subkey] = True self.__lasttime = 0 self.__lock.release() def resetSub(self, subkey): self.__lock.acquire() try: if subkey in self.__subdone: del self.__subdone[subkey] if subkey in self.__subprogress: (subcurrent, subtotal, fragment, subdata) = self.__subprogress[subkey] self.__subprogress[subkey] = (0, subtotal, fragment, {}) self.__lasttime = 0 finally: self.__lock.release()class TextProgress(Progress): def __init__(self): Progress.__init__(self) self._lasttopic = None self._lastsubkey = None self._lastsubkeystart = 0 self._fetchermode = False self._nolengthmode = False # Cursor direction for no length mode self._direction = 0 self._current = 0.0 self._seentopics = {} self._addline = False self.setScreenWidth(self.getScreenWidth()) #signal.signal(signal.SIGWINCH, self.handleScreenWidth) def setScreenWidth(self, width): self._screenwidth = width self._topicwidth = int(width*0.4) self._hashwidth = int(width-self._topicwidth-1) self._topicmask = "%%-%d.%ds" % (self._topicwidth, self._topicwidth) self._topicmaskn = "%%4d:%%-%d.%ds" % (self._topicwidth-5, self._topicwidth-5) def handleScreenWidth(self, signum, frame): self.setScreenWidth(self.getScreenWidth()) def setFetcherMode(self, flag): self._fetchermode = flag def setNoLengthMode(self, flag): self._nolengthmode = flag def stop(self): Progress.stop(self) print def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): out = sys.stdout if not out.isatty() and not done: return if self.getHasSub(): if topic != self._lasttopic: self._lasttopic = topic out.write(" "*(self._screenwidth-1)+"\r") if self._addline: print else: self._addline = True if not subkey: return if not done: now = time.time() if subkey == self._lastsubkey: if (self._lastsubkeystart+2 < now and self.getSubCount() > 1): return else: if (self._lastsubkeystart+2 > now and self.getSubCount() > 1): return self._lastsubkey = subkey self._lastsubkeystart = now elif subkey == self._lastsubkey: self._lastsubkeystart = 0 current = subpercent topic = subtopic else: current = percent n = data.get("item-number") if n: if len(topic) > self._topicwidth-6: topic = topic[:self._topicwidth-8]+".." out.write(self._topicmaskn % (n, topic)) else: if len(topic) > self._topicwidth-1: topic = topic[:self._topicwidth-3]+".." out.write(self._topicmask % topic) if not done: speed = data.get("speed") if speed: suffix = "(%s%% %s %s) \r" % (str(current), speed, data.get("eta")) else: suffix = "(%3d%%) \r" % current elif subpercent is None: suffix = "[%3d%%] \n" % current else: suffix = "[%3d%%] \n" % percent if self._nolengthmode: hashwidth = self._hashwidth - 8 hashes = int(hashwidth*current/100) suffix = "(%3s) \r" % '...' out.write("[") #if self._direction==0: leftwidth = hashes - 1 rightwidth = hashwidth - hashes -3 #elif self._direction==1: # leftwidth = hashwidth - hashes - 3 # rightwidth = hashes # print leftwidth, rightwidth, hashwidth #if rightwidth==0: # # Switch direction # self._direction = 1 #elif leftwidth==0: # self._direction = 0 if leftwidth >=0: out.write(" "*leftwidth) out.write("<=>") if rightwidth >=0: out.write(" "*(rightwidth)) out.write("]") out.write(suffix) out.flush() # Sleep for some time time.sleep(0.1) else: hashwidth = self._hashwidth -len(suffix) hashes = int(hashwidth*current/100) out.write("[") out.write("="*(hashes-1)) out.write(">") out.write(" "*(hashwidth-hashes-1)) out.write("]") out.write(suffix) out.flush() def test(): prog = TextProgress() data = {"item-number": 0} total, subtotal = 10, 10 prog.setHasSub(True) prog.start() prog.setTopic("Installing packages..") #data["item-number"] = 1 for n in range(1, total+1): prog.set(n, total) for i in range(0,subtotal+1): prog.setSubTopic(n, "package-name%d" % n) prog.setSub(n, i, subtotal) #, subdata=data) prog.show() time.sleep(0.1) prog.stop()def test2(): prog = TextProgress() data = {"item-number": 0} total, subtotal = 10, 100 prog.setFetcherMode(True) prog.setHasSub(True) prog.start() prog.setTopic("Installing packages...") for n in range(1,total+1): data["item-number"] = n prog.set(n, total) prog.setSubTopic(n, "package-name%d" % n) for i in range(0,subtotal+1): prog.setSub(n, i, subtotal, subdata=data) prog.show() # time.sleep(0.1) prog.stop()if __name__ == "__main__": test()# vim:ts=4:sw=4:et
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?