1
# Copyright (C) 2007 Julian Andres Klode <jak@jak-linux.org>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
__copyright__ = 'Copyright (C) 2007 Julian Andres Klode'
21
from os.path import getsize as filesize
22
from os.path import exists as path_exists
23
from sha import new as newsum
26
'''Return the hex digest of a file without loading it all into memory'''
35
return digest.hexdigest()
39
'''Return a human readable size'''
40
if i >= (1024*1024*1024):
41
return str(i / 1024) + "G"
42
elif i >= (1024*1024):
43
return str(i / 1024) + "M"
45
return str(i / 1024) + "K"
50
def __init__(self, fname):
51
if path_exists(fname):
52
self.obj = open(fname, 'r+')
54
open(fname, 'w').close()
55
self.obj = open(fname, 'r+')
58
def read(self, fname=None):
60
if not path_exists(fname): return False
64
for key,val in (line.strip().split(':', 1) for line in obj):
65
self.data[key.strip()] = val.strip()
66
if obj != self.obj: obj.close()
68
def write(self, data=None):
69
if data: self.append(data)
70
for key,val in sorted(self.data.items()):
71
self.obj.write('%s: %s\n' % (key, val))
72
def __setitem__(self, key, val):
74
def __getitem__(self, key):
77
return self.data.items()
78
def append(self, data):
79
for k,v in data.items():
80
if v: self.data[k] = str(v).strip()
84
"""Print a progress bar
85
Based on work by Randy Pargman
87
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639
89
def __init__(self, minValue = 0, maxValue = 10, totalWidth=24):
90
self.progBar = "[]" # This holds the progress bar string
93
self.span = maxValue - minValue
94
self.width = totalWidth
95
self.amount = 0 # When amount == max, we are 100% done
96
self.updateAmount(0) # Build progress bar string
98
def increaseAmount(self, newAmount = 0, text=""):
99
return self.updateAmount(self.amount + newAmount, text)
101
def updateAmount(self, newAmount = 0,text=""):
102
if newAmount < self.min: newAmount = self.min
103
if newAmount > self.max: newAmount = self.max
104
self.amount = newAmount
106
# Figure out the new percent done, round to an integer
107
diffFromMin = float(self.amount - self.min)
108
percentDone = (diffFromMin / float(self.span)) * 100.0
109
percentDone = round(percentDone)
110
percentDone = int(percentDone)
112
# Figure out how many hash bars the percentage should be
113
allFull = self.width - 2
114
numHashes = (percentDone / 100.0) * allFull
115
numHashes = int(round(numHashes))
117
# build a progress bar with hashes and spaces
118
self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
120
# figure out where to put the percentage, roughly centered
121
percentPlace = (len(self.progBar) / 2) - len(str(percentDone))
122
percentString = str(percentDone) + "%"
124
# slice the percentage into the bar
125
self.progBar = self.progBar[0:percentPlace] + percentString + self.progBar[percentPlace+len(percentString):] + text
128
return str(self.progBar)