Skip to content

Commit

Permalink
added FilelogHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
zopyx committed Sep 16, 2005
1 parent 9cebd8d commit 323946c
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions ProgressHandler.py
@@ -0,0 +1,111 @@
##############################################################################
#
# 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')



0 comments on commit 323946c

Please sign in to comment.