Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/utils/progress.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
from __future__ import division
9
10
import time
11
12
from lib.core.common import dataToStdout
13
from lib.core.convert import getUnicode
14
from lib.core.data import conf
15
from lib.core.data import kb
16
17
class ProgressBar(object):
18
"""
19
This class defines methods to update and draw a progress bar
20
"""
21
22
def __init__(self, minValue=0, maxValue=10, totalWidth=None):
23
self._progBar = "[]"
24
self._min = int(minValue)
25
self._max = int(maxValue)
26
self._span = max(self._max - self._min, 0.001)
27
self._width = totalWidth if totalWidth else conf.progressWidth
28
self._amount = 0
29
self._start = None
30
self.update()
31
32
def _convertSeconds(self, value):
33
seconds = value
34
minutes = seconds // 60
35
seconds = seconds - (minutes * 60)
36
37
return "%.2d:%.2d" % (minutes, seconds)
38
39
def update(self, newAmount=0):
40
"""
41
This method updates the progress bar
42
"""
43
44
if newAmount < self._min:
45
newAmount = self._min
46
elif newAmount > self._max:
47
newAmount = self._max
48
49
self._amount = newAmount
50
51
# Figure out the new percent done, round to an integer
52
diffFromMin = float(self._amount - self._min)
53
percentDone = (diffFromMin / float(self._span)) * 100.0
54
percentDone = round(percentDone)
55
percentDone = min(100, int(percentDone))
56
57
# Figure out how many hash bars the percentage should be
58
allFull = self._width - len("100%% [] %s/%s (ETA 00:00)" % (self._max, self._max))
59
numHashes = (percentDone / 100.0) * allFull
60
numHashes = int(round(numHashes))
61
62
# Build a progress bar with an arrow of equal signs
63
if numHashes == 0:
64
self._progBar = "[>%s]" % (" " * (allFull - 1))
65
elif numHashes == allFull:
66
self._progBar = "[%s]" % ("=" * allFull)
67
else:
68
self._progBar = "[%s>%s]" % ("=" * (numHashes - 1), " " * (allFull - numHashes))
69
70
# Add the percentage at the beginning of the progress bar
71
percentString = getUnicode(percentDone) + "%"
72
self._progBar = "%s %s" % (percentString, self._progBar)
73
74
def progress(self, newAmount):
75
"""
76
This method saves item delta time and shows updated progress bar with calculated eta
77
"""
78
79
if self._start is None or newAmount > self._max:
80
self._start = time.time()
81
eta = None
82
else:
83
delta = time.time() - self._start
84
eta = (self._max - self._min) * (1.0 * delta / newAmount) - delta
85
86
self.update(newAmount)
87
self.draw(eta)
88
89
def draw(self, eta=None):
90
"""
91
This method draws the progress bar if it has changed
92
"""
93
94
dataToStdout("\r%s %d/%d%s" % (self._progBar, self._amount, self._max, (" (ETA %s)" % (self._convertSeconds(int(eta)) if eta is not None else "??:??"))))
95
if self._amount >= self._max:
96
dataToStdout("\r%s\r" % (" " * self._width))
97
kb.prependFlag = False
98
99
def __str__(self):
100
"""
101
This method returns the progress bar string
102
"""
103
104
return getUnicode(self._progBar)
105
106