sqlmap/lib/core/progress.py

90 lines
2.9 KiB
Python
Raw Normal View History

2008-10-15 19:38:22 +04:00
#!/usr/bin/env python
"""
2012-07-12 21:38:03 +04:00
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
2008-10-15 19:38:22 +04:00
"""
2010-06-02 16:45:40 +04:00
from lib.core.common import getUnicode
2008-10-15 19:38:22 +04:00
from lib.core.common import dataToStdout
2010-03-12 15:46:26 +03:00
from lib.core.data import conf
2008-10-15 19:38:22 +04:00
class ProgressBar(object):
2008-10-15 19:38:22 +04:00
"""
This class defines methods to update and draw a progress bar
"""
2010-03-12 15:46:26 +03:00
def __init__(self, minValue=0, maxValue=10, totalWidth=None):
self._progBar = "[]"
self._oldProgBar = ""
2012-12-21 18:06:03 +04:00
self._min = int(minValue)
self._max = int(maxValue)
self._span = self._max - self._min
self._width = totalWidth if totalWidth else conf.progressWidth
self._amount = 0
2008-10-15 19:38:22 +04:00
self.update()
def _convertSeconds(self, value):
2008-10-15 19:38:22 +04:00
seconds = value
minutes = seconds / 60
seconds = seconds - (minutes * 60)
return "%.2d:%.2d" % (minutes, seconds)
def update(self, newAmount=0):
"""
This method updates the progress bar
"""
2012-12-21 18:06:03 +04:00
if newAmount < self._min:
newAmount = self._min
elif newAmount > self._max:
newAmount = self._max
2008-10-15 19:38:22 +04:00
2012-12-21 18:06:03 +04:00
self._amount = newAmount
2008-10-15 19:38:22 +04:00
# Figure out the new percent done, round to an integer
2012-12-21 18:06:03 +04:00
diffFromMin = float(self._amount - self._min)
percentDone = (diffFromMin / float(self._span)) * 100.0
2008-10-15 19:38:22 +04:00
percentDone = round(percentDone)
percentDone = int(percentDone)
# Figure out how many hash bars the percentage should be
2012-12-21 18:06:03 +04:00
allFull = self._width - 2
2008-10-15 19:38:22 +04:00
numHashes = (percentDone / 100.0) * allFull
numHashes = int(round(numHashes))
# Build a progress bar with an arrow of equal signs
if numHashes == 0:
self._progBar = "[>%s]" % (" " * (allFull - 1))
2008-10-15 19:38:22 +04:00
elif numHashes == allFull:
self._progBar = "[%s]" % ("=" * allFull)
2008-10-15 19:38:22 +04:00
else:
self._progBar = "[%s>%s]" % ("=" * (numHashes - 1),
2008-10-15 19:38:22 +04:00
" " * (allFull - numHashes))
# Add the percentage at the beginning of the progress bar
2010-06-02 16:45:40 +04:00
percentString = getUnicode(percentDone) + "%"
self._progBar = "%s %s" % (percentString, self._progBar)
2008-10-15 19:38:22 +04:00
def draw(self, eta=0):
"""
This method draws the progress bar if it has changed
"""
if self._progBar != self._oldProgBar:
self._oldProgBar = self._progBar
2008-10-15 19:38:22 +04:00
2012-12-21 18:06:03 +04:00
if eta and self._amount < self._max:
dataToStdout("\r%s %d/%d ETA %s" % (self._progBar, self._amount, self._max, self._convertSeconds(int(eta))))
2008-10-15 19:38:22 +04:00
else:
2012-12-21 18:06:03 +04:00
blank = " " * (80 - len("\r%s %d/%d" % (self._progBar, self._amount, self._max)))
dataToStdout("\r%s %d/%d%s" % (self._progBar, self._amount, self._max, blank))
2008-10-15 19:38:22 +04:00
def __str__(self):
"""
This method returns the progress bar string
"""
return getUnicode(self._progBar)