~juliank/ftpsync/main

« back to all changes in this revision

Viewing changes to ftpsynclib/__init__.py

  • Committer: Julian Andres Klode
  • Date: 2007-06-20 16:37:24 UTC
  • Revision ID: jak@jak-linux.org-20070620163724-fhqyx84z6q2wtg8y
* Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Julian Andres Klode <jak@jak-linux.org>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
'''Core of ftpsync'''
 
18
__version__ = '0.1'
 
19
__copyright__ = 'Copyright (C) 2007 Julian Andres Klode'
 
20
 
 
21
from os.path import getsize as filesize
 
22
from os.path import exists as path_exists
 
23
from sha import new as newsum
 
24
 
 
25
def fsum(filename):
 
26
        '''Return the hex digest of a file without loading it all into memory'''
 
27
        fh = open(filename)
 
28
        digest = newsum()
 
29
        while 1:
 
30
                buf = fh.read(8192)
 
31
                if buf == "":
 
32
                        break
 
33
                digest.update(buf)
 
34
        fh.close()
 
35
        return digest.hexdigest()
 
36
 
 
37
def size(f):
 
38
        i=filesize(f)
 
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"
 
44
        elif i >= 1024: 
 
45
                return str(i / 1024) + "K"
 
46
        else:
 
47
                return str(i) + "B"
 
48
 
 
49
class conf:
 
50
        def __init__(self, fname):
 
51
                if path_exists(fname):
 
52
                        self.obj  = open(fname, 'r+')
 
53
                else:
 
54
                        open(fname, 'w').close()
 
55
                        self.obj  = open(fname, 'r+')
 
56
                self.data = {}
 
57
                self.read()
 
58
        def read(self, fname=None):
 
59
                if fname: 
 
60
                        if not path_exists(fname): return False
 
61
                        obj = file(fname)
 
62
                else:
 
63
                        obj = self.obj
 
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()
 
67
                return True
 
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):
 
73
                self.data[key] = val
 
74
        def __getitem__(self, key):
 
75
                return self.data[key]
 
76
        def items(self):
 
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()
 
81
 
 
82
 
 
83
class progressBar:
 
84
        """Print a progress bar
 
85
        Based on work by Randy Pargman
 
86
        Original Version at:
 
87
                http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639
 
88
        """
 
89
        def __init__(self, minValue = 0, maxValue = 10, totalWidth=24):
 
90
                self.progBar = "[]"   # This holds the progress bar string
 
91
                self.min = minValue
 
92
                self.max = maxValue
 
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
 
97
 
 
98
        def increaseAmount(self, newAmount = 0, text=""):
 
99
                return self.updateAmount(self.amount + newAmount, text)
 
100
 
 
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
 
105
 
 
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)
 
111
 
 
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))
 
116
 
 
117
                # build a progress bar with hashes and spaces
 
118
                self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
 
119
 
 
120
                # figure out where to put the percentage, roughly centered
 
121
                percentPlace = (len(self.progBar) / 2) - len(str(percentDone)) 
 
122
                percentString = str(percentDone) + "%"
 
123
 
 
124
                # slice the percentage into the bar
 
125
                self.progBar = self.progBar[0:percentPlace] + percentString + self.progBar[percentPlace+len(percentString):] + text
 
126
 
 
127
        def __str__(self):
 
128
                return str(self.progBar)