~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/recordcounter.py

  • Committer: Parth Malwankar
  • Date: 2010-05-13 14:02:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5241.
  • Revision ID: parth.malwankar@gmail.com-20100513140228-y64pg0olfzvagujc
Documentation and cleanup for RecordCounter

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Record counting support for showing progress of revision fetch"""
18
 
 
19
 
def _gen_printer(stream):
20
 
    count = 0
21
 
    for s in stream:
22
 
        count = count + 1
23
 
        yield s
24
 
    print "count is:", count, "###########"
 
16
"""Record counting support for showing progress of revision fetch."""
25
17
 
26
18
class RecordCounter(object):
 
19
    """Container for maintains estimates of work requires for fetch.
 
20
 
 
21
    Instance of this class is used along with a progress bar to provide
 
22
    the user an estimate of the amount of work pending for a fetch (push,
 
23
    pull, branch, checkout) operation.
 
24
    """
27
25
    def __init__(self):
28
26
        self.initialized = False
29
27
        self.current = 0
34
32
    def is_initialized(self):
35
33
        return self.initialized
36
34
 
 
35
    def _estimate_max(self, key_count):
 
36
        """Estimate the maximum amount of 'inserting stream' work.
 
37
 
 
38
        This is just an estimate.
 
39
        """
 
40
        # Note: The magic number below is based of empirical data
 
41
        # based on 3 seperate projects. Estimatation can probably
 
42
        # be improved but this should work well for most cases.
 
43
        return int(key_count * 10.3)
 
44
 
37
45
    def setup(self, key_count, current):
 
46
        """Setup RecordCounter with basic estimate of work pending.
 
47
 
 
48
        Setup self.max and self.current to reflect the amount of work
 
49
        pending for a fetch.
 
50
        """
38
51
        self.current = current
39
52
        self.key_count = key_count
40
 
        self.max = key_count * 10.3
 
53
        self.max = self._estimate_max(key_count)
41
54
        self.initialized = True
42
55
 
43
56
    def increment(self, count):
 
57
        """Increment self.current by count.
 
58
 
 
59
        Apart from incrementing self.current by count, also ensure
 
60
        that self.max > self.current.
 
61
        """
44
62
        self.current += count
45
63
        if self.current > self.max:
46
64
            self.max += self.key_count