progress.py
来自「Harvestman-最新版本」· Python 代码 · 共 499 行 · 第 1/2 页
PY
499 行
# -- coding: utf-8# progress.py - Module which provides a generic Text Progress bar# and a Progress bar class.## Created: Anand B Pillai Mar 9 2007 by copying and merging# Progress/TextProgress classes from S.M.A.R.T version 0.5.# with some modifications for HarvestMan.## This module is part of the HarvestMan program.# For licensing information see the file LICENSE.txt that# is included in this distribution.##----------Original copyright/license information-------------------------## Copyright (c) 2005 Canonical# Copyright (c) 2004 Conectiva, Inc.## Written by Gustavo Niemeyer <niemeyer@conectiva.com>## This file is part of Smart Package Manager.## Smart Package Manager is free software; you can redistribute it and/or# modify it under the terms of the GNU General Public License as published# by the Free Software Foundation; either version 2 of the License, or (at# your option) any later version.## Smart Package Manager is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# General Public License for more details.## You should have received a copy of the GNU General Public License# along with Smart Package Manager; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#-------------- End of original copyright ---------------------------------## Copyright (C) 2007, Anand B Pillai#__version__ = '2.0 b1'__author__ = 'Anand B Pillai'import posixpathimport timeimport sysimport structimport osif os.name == 'posix': import fcntl import termiosimport threadimport timeimport sysINTERVAL = 0.1class Progress(object): def __init__(self): self.__topic = "" self.__progress = (0, 0, {}) # (current, total, data) self.__lastshown = None self.__done = False self.__subtopic = {} self.__subprogress = {} # (subcurrent, subtotal, fragment, subdata) self.__sublastshown = {} self.__subdone = {} self.__lasttime = 0 self.__lock = thread.allocate_lock() self.__hassub = False def getScreenWidth(self): if os.name == 'posix': s = struct.pack('HHHH', 0, 0, 0, 0) try: x = fcntl.ioctl(1, termios.TIOCGWINSZ, s) except IOError: return 80 return struct.unpack('HHHH', x)[1] else: return 80 def lock(self): self.__lock.acquire() def unlock(self): self.__lock.release() def start(self): pass def stop(self): self.__topic = "" self.__progress = (0, 0, {}) self.__lastshown = None self.__done = False self.__subtopic.clear() self.__subprogress.clear() self.__sublastshown.clear() self.__subdone.clear() self.__lasttime = 0 self.__hassub = False def setHasSub(self, flag): self.__hassub = flag def getHasSub(self): return self.__hassub def getSubCount(self): return len(self.__subprogress) def show(self): now = time.time() if self.__lasttime > now-INTERVAL: return self.__lock.acquire() try: self.__lasttime = now current, total, data = self.__progress subexpose = [] for subkey in self.__subprogress.keys(): sub = self.__subprogress[subkey] subcurrent, subtotal, fragment, subdata = sub subpercent = int(100*float(subcurrent)/(subtotal or 1)) if fragment: current += int(fragment*float(subpercent)/100) subtopic = self.__subtopic.get(subkey) if (subkey not in self.__subdone and sub == self.__sublastshown.get(subkey)): continue self.__sublastshown[subkey] = sub subdone = False if subpercent == 100: self.__subdone[subkey] = True subdone = True if fragment: _current, _total, _data = self.__progress self.__progress = (_current+fragment, _total, _data) if _current == _total: self.__lasttime = 0 elif subkey in self.__subdone: subdone = subkey in self.__subdone subexpose.append((subkey, subtopic, subpercent, subdata, subdone)) topic = self.__topic percent = int(100*float(current)/(total or 1)) if subexpose: for info in subexpose: self.expose(topic, percent, *info) if info[-1]: subkey = info[0] del self.__subprogress[subkey] del self.__sublastshown[subkey] del self.__subtopic[subkey] if percent == 100 and len(self.__subprogress) == 0: self.__done = True self.expose(topic, percent, None, None, None, data, self.__done) elif (topic, percent) != self.__lastshown: if percent == 100 and len(self.__subprogress) == 0: self.__done = True self.expose(topic, percent, None, None, None, data, self.__done) finally: self.__lock.release() def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): pass def setTopic(self, topic): self.__topic = topic def get(self): return self.__progress def set(self, current, total, data={}): self.__lock.acquire() try: if self.__done: return if current > total: current = total self.__progress = (current, total, data) if current == total: self.__lasttime = 0 finally: self.__lock.release() def add(self, value): self.__lock.acquire() try: if self.__done: return current, total, data = self.__progress current += value if current > total: current = total self.__progress = (current, total, data) if current == total: self.__lasttime = 0 finally: self.__lock.release() def addTotal(self, value): self.__lock.acquire() try: if self.__done: return current, total, data = self.__progress self.__progress = (current, total+value, data) finally: self.__lock.release() def setSubTopic(self, subkey, subtopic): self.__lock.acquire() try: if subkey not in self.__subtopic: self.__lasttime = 0 self.__subtopic[subkey] = subtopic finally: self.__lock.release() def getSub(self, subkey): return self.__subprogress.get(subkey) def getSubData(self, subkey, _none=[None]): return self.__subprogress.get(subkey, _none)[-1] def setSub(self, subkey, subcurrent, subtotal, fragment=0, subdata={}): self.__lock.acquire() try: if self.__done or subkey in self.__subdone: return if subkey not in self.__subtopic: self.__subtopic[subkey] = "" self.__lasttime = 0 if subcurrent > subtotal: subcurrent = subtotal if subcurrent == subtotal: self.__lasttime = 0 self.__subprogress[subkey] = (subcurrent, subtotal, fragment, subdata) finally: self.__lock.release() def addSub(self, subkey, value):
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?