"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/Tardis/StatusBar.py" between
Tardis-1.1.5.tar.gz and Tardis-1.2.1.tar.gz

About: Tardis is a system for making encrypted, incremental backups of filesystems.

StatusBar.py  (Tardis-1.1.5):StatusBar.py  (Tardis-1.2.1)
skipping to change at line 37 skipping to change at line 37
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
import threading import threading
import time import time
import signal import signal
import shutil import shutil
import string import string
import atexit import atexit
import sched
_ansiClearEol = '\x1b[K' _ansiClearEol = '\x1b[K'
_startOfLine = '\r' _startOfLine = '\r'
_hideCursor = '\x1b[?25l' _hideCursor = '\x1b[?25l'
_showCursor = '\x1b[?25h' _showCursor = '\x1b[?25h'
_statusBars = [] _statusBars = []
def fmtSize(num, base=1024, formats = ['bytes','KB','MB','GB', 'TB', 'PB']): def fmtSize(num, base=1024, formats = ['bytes','KB','MB','GB', 'TB', 'PB']):
fmt = "%d %s" fmt = "%d %s"
if num is None: if num is None:
skipping to change at line 87 skipping to change at line 88
def convert_field(self, value, conversion): def convert_field(self, value, conversion):
if conversion == "B": if conversion == "B":
return fmtSize(value) return fmtSize(value)
else: else:
return super().convert_field(value, conversion) return super().convert_field(value, conversion)
def resetCursor(): def resetCursor():
print(_showCursor, end='') print(_showCursor, end='')
class StatusBar(): class StatusBar():
def __init__(self, base, live={}, formatter=None): def __init__(self, base, live={}, formatter=None, delay=0.25, scheduler=None , priority=10):
self.base = base self.base = base
self.live = live self.live = live
self.trailer = None self.trailer = None
self.halt = False self.halt = False
self.values = {} self.values = {}
if formatter: self.delay = delay
self.formatter = formatter self.formatter = formatter if formatter else StatusBarFormatter()
else: self.scheduler = scheduler if scheduler else sched.scheduler()
self.formatter = StatusBarFormatter() self.priority = priority
(width, _) = shutil.get_terminal_size((80, 32)) (width, _) = shutil.get_terminal_size((80, 32))
_statusBars.append(self) _statusBars.append(self)
_handle_resize(None, None) _handle_resize(None, None)
signal.signal(signal.SIGWINCH, _handle_resize) signal.signal(signal.SIGWINCH, _handle_resize)
signal.siginterrupt(signal.SIGWINCH, False) signal.siginterrupt(signal.SIGWINCH, False)
def run(self, delay=0.25): self.event = self.scheduler.enter(self.delay, self.priority, self.printS tatus)
atexit.register(resetCursor) atexit.register(resetCursor)
starttime = time.time()
self.halt = False
while not self.halt:
time.sleep(delay)
self.printStatus()
atexit.unregister(resetCursor)
self.clearStatus()
def start(self, delay=0.25, name="StatusBar"): def start(self, delay=0.25, name="StatusBar"):
self.thread = threading.Thread(name=name, target=self.run, args=(delay,) ) self.thread = threading.Thread(name=name, target=self.scheduler.run)
self.thread.setDaemon(True) self.thread.setDaemon(True)
self.thread.start() self.thread.start()
def shutdown(self): def shutdown(self):
self.scheduler.cancel(self.event)
self.halt = True self.halt = True
self.clearStatus() self.clearStatus()
self.thread.join() atexit.unregister(resetCursor)
pass if self.thread:
self.thread.join()
def pTime(self, seconds): def pTime(self, seconds):
if seconds > 3600: if seconds > 3600:
return time.strftime("%H:%M:%S", time.gmtime(seconds)) return time.strftime("%H:%M:%S", time.gmtime(seconds))
else: else:
return time.strftime("%M:%S", time.gmtime(seconds)) return time.strftime("%M:%S", time.gmtime(seconds))
def setWidth(self, width): def setWidth(self, width):
self.width = width self.width = width
skipping to change at line 153 skipping to change at line 148
self.values[key] = value self.values[key] = value
def setValues(self, values): def setValues(self, values):
self.values.update(values) self.values.update(values)
def processTrailer(self, length, s): def processTrailer(self, length, s):
return s[:length] return s[:length]
def printStatus(self): def printStatus(self):
try: try:
output = self.formatter.format(self.base, **{**self.live, **self.val ues}) output = self.formatter.format(self.base, **{**self.live, **self.val ues}).encode('utf8', 'backslashreplace').decode('utf8')
if self.trailer: if self.trailer:
output += self.processTrailer(self.width - len(output), self.tra iler) output += self.processTrailer(self.width - 2 - len(output), self .trailer)
except KeyError as k: except KeyError as k:
output = "Error generating status message: Missing value for " + str (k) output = "Error generating status message: Missing value for " + str (k)
except Exception as e: except Exception as e:
output = "Error generating status message: " + str(e) output = "Error generating status message: " + str(e)
print(output + _ansiClearEol + _startOfLine + _hideCursor, end='', flush try:
=True) print(output + _ansiClearEol + _startOfLine + _hideCursor, end='', f
lush=True)
except:
print(_ansiClearEol + _startOfLine, end='', flush=True)
self.event = self.scheduler.enter(self.delay, self.priority, self.printS
tatus)
def clearStatus(self): def clearStatus(self):
print(_showCursor + _startOfLine + _ansiClearEol, end='') print(_showCursor + _startOfLine + _ansiClearEol, end='')
if __name__ == "__main__": if __name__ == "__main__":
import os, os.path import os, os.path
myargs = {"files": 0} myargs = {"files": 0}
sb = StatusBar("{__elapsed__} :: Files: {files} Delta: {delta}: {amount!B} { mode} --> ", myargs) sb = StatusBar("{__elapsed__} :: Files: {files} Delta: {delta}: {amount!B} { mode} --> ", myargs)
sb.setValue("mode", "Testing") sb.setValue("mode", "Testing")
sb.start() sb.start()
 End of changes. 12 change blocks. 
21 lines changed or deleted 22 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)