/
ProgressHandler.py
111 lines (78 loc) · 3.13 KB
/
ProgressHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""
$Id: ZCatalog.py 25050 2004-05-27 15:06:40Z chrisw $
"""
import time, sys
from zLOG import LOG, INFO
from DateTime.DateTime import DateTime
from Interface import Interface
class IProgressHandler(Interface):
""" A handler to log progress informations for long running
operations.
"""
def init(ident, max):
""" Called at the start of the long running process.
'ident' -- a string identifying the operation
'max' -- maximum number of objects to be processed (int)
"""
def info(text):
""" Log some 'text'"""
def finish():
""" Called up termination """
def report(current, *args, **kw):
""" Called for every iteration.
'current' -- an integer representing the number of objects
processed so far.
"""
def output(text):
""" Log 'text' to some output channel """
class StdoutHandler:
""" A simple progress handler """
__implements__ = IProgressHandler
def __init__(self, steps=100):
self._steps = steps
def init(self, ident, max):
self._ident = ident
self._max = max
self._start = time.time()
self.fp = sys.stdout
self.output('Process started (%d objects to go)' % self._max)
def info(self, text):
self.output(text)
def finish(self):
self.output('Process terminated. Duration: %0.2f seconds' % \
(time.time() -self._start))
def report(self, current, *args, **kw):
if current > 0:
if current % self._steps == 0:
seconds_so_far = time.time() - self._start
seconds_to_go = seconds_so_far / current * (self._max - current)
self.output('%d/%d (%.2f%%) Estimated termination: %s' % \
(current, self._max, (100.0 * current / self._max),
DateTime(time.time() + seconds_to_go).strftime('%Y/%m/%d %H:%M:%Sh')))
def output(self, text):
print >>self.fp, '%s: %s' % (self._ident, text)
class ZLogHandler(StdoutHandler):
""" Use zLOG """
__implements__ = IProgressHandler
def output(self, text):
LOG(self._ident, INFO, text)
class FilelogHandler(StdoutHandler):
""" Use a custom file for logging """
__implements__ = IProgressHandler
def __init__(self, filename, steps=100):
StdoutHandler.__init__(self, steps)
self.filename = filename
def output(self, text):
open(self.filename, 'a').write(text + '\n')