[CMF-checkins] SVN: CMF/branches/tseaver-resync_GenericSetup/ -
replaced note() by getLogger() that returns a PEP 282 style
logger object
Yvo Schubbe
y.2005- at wcm-solutions.de
Thu Nov 17 16:48:34 EST 2005
Log message for revision 40201:
- replaced note() by getLogger() that returns a PEP 282 style logger object
- modified GenericSetup handlers to use the logger instead of returning a message string
Changed:
U CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/content.py
U CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/tests/test_content.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/content.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/context.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/interfaces.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/rolemap.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/common.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_content.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_context.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_tool.py
U CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tool.py
-=-
Modified: CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/content.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/content.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/content.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -145,9 +145,9 @@
object = self._makeInstance(object_id, portal_type,
subdir, import_context)
if object is None:
- message = "Couldn't make instance: %s/%s" % (subdir,
- object_id)
- import_context.note('SFWA', message)
+ logger = import_context.getLogger('SFWA')
+ logger.warning("Couldn't make instance: %s/%s" %
+ (subdir, object_id))
continue
wrapped = context._getOb(object_id)
Modified: CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/tests/test_content.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/tests/test_content.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/CMFCore/exportimport/tests/test_content.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -431,7 +431,7 @@
after = site.objectIds()
self.assertEqual(len(after), 0)
self.assertEqual(len(context._notes), len(ITEM_IDS))
- for component, message in context._notes:
+ for level, component, message in context._notes:
self.assertEqual(component, 'SFWA')
self.failUnless(message.startswith("Couldn't make"))
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/content.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/content.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/content.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -172,9 +172,9 @@
object = self._makeInstance(object_id, type_name,
subdir, import_context)
if object is None:
- message = "Couldn't make instance: %s/%s" % (subdir,
- object_id)
- import_context.note('SFWA', message)
+ logger = import_context.getLogger('SFWA')
+ logger.warning("Couldn't make instance: %s/%s" %
+ (subdir, object_id))
continue
wrapped = context._getOb(object_id)
@@ -268,8 +268,8 @@
cid = self.context.getId()
data = import_context.readDataFile('%s.csv' % cid, subdir)
if data is None:
- import_context.note('CSAFA',
- 'no .csv file for %s/%s' % (subdir, cid))
+ logger = import_context.getLogger('CSAFA')
+ logger.info('no .csv file for %s/%s' % (subdir, cid))
else:
stream = StringIO(data)
self.context.put_csv(stream)
@@ -303,8 +303,8 @@
cid = self.context.getId()
data = import_context.readDataFile('%s.ini' % cid, subdir)
if data is None:
- import_context.note('SGAIFA',
- 'no .ini file for %s/%s' % (subdir, cid))
+ logger = import_context.getLogger('SGAIFA')
+ logger.info('no .ini file for %s/%s' % (subdir, cid))
else:
self.context.put_ini(data)
@@ -393,8 +393,8 @@
cid = self.context.getId()
data = import_context.readDataFile('%s' % cid, subdir)
if data is None:
- import_context.note('SGAIFA',
- 'no .ini file for %s/%s' % (subdir, cid))
+ logger = import_context.getLogger('SGAIFA')
+ logger.info('no .ini file for %s/%s' % (subdir, cid))
else:
request = FauxDAVRequest(BODY=data, BODYFILE=StringIO(data))
response = FauxDAVResponse()
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/context.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/context.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/context.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -17,6 +17,7 @@
$Id$
"""
+import logging
import os
import time
from StringIO import StringIO
@@ -40,8 +41,58 @@
from interfaces import IExportContext
from interfaces import IImportContext
+from interfaces import IWriteLogger
from permissions import ManagePortal
+
+class Logger:
+
+ implements(IWriteLogger)
+
+ def __init__(self, id, messages):
+ """Initialize the logger with a name and an optional level.
+ """
+ self._id = id
+ self._messages = messages
+ self._logger = logging.getLogger('GenericSetup.%s' % id)
+
+ def debug(self, msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'DEBUG'.
+ """
+ self.log(logging.DEBUG, msg, *args, **kwargs)
+
+ def info(self, msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'INFO'.
+ """
+ self.log(logging.INFO, msg, *args, **kwargs)
+
+ def warning(self, msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'WARNING'.
+ """
+ self.log(logging.WARNING, msg, *args, **kwargs)
+
+ def error(self, msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'ERROR'.
+ """
+ self.log(logging.ERROR, msg, *args, **kwargs)
+
+ def exception(self, msg, *args):
+ """Convenience method for logging an ERROR with exception information.
+ """
+ self.error(msg, *args, **{'exc_info': 1})
+
+ def critical(self, msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'CRITICAL'.
+ """
+ self.log(logging.CRITICAL, msg, *args, **kwargs)
+
+ def log(self, level, msg, *args, **kwargs):
+ """Log 'msg % args' with the integer severity 'level'.
+ """
+ self._messages.append((level, self._id, msg))
+ self._logger.log(level, msg, *args, **kwargs)
+
+
class BaseContext( Implicit ):
security = ClassSecurityInfo()
@@ -50,6 +101,7 @@
self._tool = tool
self._site = aq_parent( aq_inner( tool ) )
+ self._loggers = {}
self._messages = []
self._encoding = encoding
@@ -70,18 +122,15 @@
security.declareProtected( ManagePortal, 'getEncoding' )
def getEncoding( self ):
- """ See ISetupContext..
+ """ See ISetupContext.
"""
return self._encoding
- security.declareProtected( ManagePortal, 'notes' )
- def note( self, component, message ):
-
+ security.declareProtected( ManagePortal, 'getLogger' )
+ def getLogger( self, name ):
""" See ISetupContext.
"""
- import zLOG
- zLOG.LOG('GenericSetup', zLOG.INFO, '%s: %s' % (component, message))
- self._messages.append((component, message))
+ return self._loggers.setdefault(name, Logger(name, self._messages))
security.declareProtected( ManagePortal, 'listNotes' )
def listNotes(self):
@@ -97,6 +146,7 @@
"""
self._messages[:] = []
+
class DirectoryImportContext( BaseContext ):
implements(IImportContext)
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/interfaces.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/interfaces.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/interfaces.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -48,28 +48,10 @@
o Return None if the data should not be encoded.
"""
- def note(category, message):
-
- """ Record a logging message from within a handler.
-
- o 'category' is a string defining the source of the message.
-
- o 'message' is the text of the message itself.
-
- o XXX This API may disappear soon, to be replaced by a more
- general annotation.
+ def getLogger(name):
+ """ Get a logger with the specified name, creating it if necessary.
"""
- def note(component, message):
-
- """ Record a message about the state of the operation.
-
- o 'component' is a string identifying the subcomponent recording
- the message.
-
- o 'message' is the message text.
- """
-
def listNotes():
""" Return notes recorded by this context.
@@ -546,6 +528,40 @@
"""
+class IWriteLogger(Interface):
+
+ """Write methods used by the python logging Logger.
+ """
+
+ def debug(msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'DEBUG'.
+ """
+
+ def info(msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'INFO'.
+ """
+
+ def warning(msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'WARNING'.
+ """
+
+ def error(msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'ERROR'.
+ """
+
+ def exception(msg, *args):
+ """Convenience method for logging an ERROR with exception information.
+ """
+
+ def critical(msg, *args, **kwargs):
+ """Log 'msg % args' with severity 'CRITICAL'.
+ """
+
+ def log(level, msg, *args, **kwargs):
+ """Log 'msg % args' with the integer severity 'level'.
+ """
+
+
class INodeExporter(Interface):
"""Node exporter.
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/rolemap.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/rolemap.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/rolemap.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -60,6 +60,7 @@
"""
site = context.getSite()
encoding = context.getEncoding()
+ logger = context.getLogger('rolemap')
if context.shouldPurge():
@@ -102,7 +103,7 @@
, permission[ 'acquire' ]
)
- return 'Role / permission map imported.'
+ logger.info('Role / permission map imported.')
def exportRolemap( context ):
@@ -131,12 +132,14 @@
"""
site = context.getSite()
+ logger = context.getLogger('rolemap')
+
rc = RolemapConfigurator( site ).__of__( site )
text = rc.generateXML()
context.writeDataFile( _FILENAME, text, 'text/xml' )
- return 'Role / permission map exported.'
+ logger.info('Role / permission map exported.')
class RolemapConfigurator(ConfiguratorBase):
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/common.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/common.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/common.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -174,6 +174,19 @@
self._compareDOM( found, data )
+class DummyLogger:
+
+ def __init__(self, id, messages):
+ self._id = id
+ self._messages = messages
+
+ def info(self, msg, *args, **kwargs):
+ self._messages.append((20, self._id, msg))
+
+ def warning(self, msg, *args, **kwargs):
+ self._messages.append((30, self._id, msg))
+
+
class DummyExportContext:
def __init__( self, site, tool=None ):
@@ -188,15 +201,15 @@
def getSetupTool( self ):
return self._tool
+ def getLogger(self, name):
+ return DummyLogger(name, self._notes)
+
def writeDataFile( self, filename, text, content_type, subdir=None ):
if subdir is not None:
filename = '%s/%s' % ( subdir, filename )
self._wrote.append( ( filename, text, content_type ) )
- def note( self, component, message ):
- self._notes.append( ( component, message ) )
-
class DummyImportContext:
def __init__( self, site, purge=True, encoding=None, tool=None ):
@@ -216,6 +229,9 @@
def getEncoding( self ):
return self._encoding
+ def getLogger(self, name):
+ return DummyLogger(name, self._notes)
+
def readDataFile( self, filename, subdir=None ):
if subdir is not None:
@@ -227,10 +243,7 @@
return self._purge
- def note( self, component, message ):
- self._notes.append( ( component, message ) )
-
def dummy_handler( context ):
pass
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_content.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_content.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_content.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -17,6 +17,8 @@
import unittest
import Testing
+import Zope2
+Zope2.startup()
from csv import reader
from ConfigParser import ConfigParser
@@ -576,7 +578,7 @@
after = site.objectIds()
self.assertEqual(len(after), 0)
self.assertEqual(len(context._notes), len(ITEM_IDS))
- for component, message in context._notes:
+ for level, component, message in context._notes:
self.assertEqual(component, 'SFWA')
self.failUnless(message.startswith("Couldn't make"))
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_context.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_context.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_context.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -17,9 +17,8 @@
import unittest
import Testing
-import Zope2
-Zope2.startup()
+import logging
import os
import time
from StringIO import StringIO
@@ -46,7 +45,8 @@
class DummyTool( Folder ):
pass
-
+
+
class DirectoryImportContextTests( FilesystemTestBase
, ConformsToISetupContext
, ConformsToIImportContext
@@ -59,16 +59,18 @@
from Products.GenericSetup.context import DirectoryImportContext
return DirectoryImportContext
- def test_note( self ):
+ def test_getLogger( self ):
site = DummySite( 'site' ).__of__( self.root )
ctx = self._makeOne( site, self._PROFILE_PATH )
self.assertEqual( len( ctx.listNotes() ), 0 )
- ctx.note( 'foo', 'bar' )
+ logger = ctx.getLogger('foo')
+ logger.info('bar')
self.assertEqual( len( ctx.listNotes() ), 1 )
- component, message = ctx.listNotes()[0]
+ level, component, message = ctx.listNotes()[0]
+ self.assertEqual( level, logging.INFO )
self.assertEqual( component, 'foo' )
self.assertEqual( message, 'bar' )
@@ -330,6 +332,7 @@
self.failUnless( 'CVS' in names )
self.failUnless( '.svn' in names )
+
class DirectoryExportContextTests( FilesystemTestBase
, ConformsToISetupContext
, ConformsToIExportContext
@@ -342,16 +345,18 @@
from Products.GenericSetup.context import DirectoryExportContext
return DirectoryExportContext
- def test_note( self ):
+ def test_getLogger( self ):
site = DummySite( 'site' ).__of__( self.root )
ctx = self._makeOne( site, self._PROFILE_PATH )
self.assertEqual( len( ctx.listNotes() ), 0 )
- ctx.note( 'foo', 'bar' )
+ logger = ctx.getLogger('foo')
+ logger.info('bar')
self.assertEqual( len( ctx.listNotes() ), 1 )
- component, message = ctx.listNotes()[0]
+ level, component, message = ctx.listNotes()[0]
+ self.assertEqual( level, logging.INFO )
self.assertEqual( component, 'foo' )
self.assertEqual( message, 'bar' )
@@ -470,15 +475,17 @@
return site, tool, ctx.__of__( tool )
- def test_note( self ):
+ def test_getLogger( self ):
site, tool, ctx = self._makeOne()
self.assertEqual( len( ctx.listNotes() ), 0 )
- ctx.note( 'foo', 'bar' )
+ logger = ctx.getLogger('foo')
+ logger.info('bar')
self.assertEqual( len( ctx.listNotes() ), 1 )
- component, message = ctx.listNotes()[0]
+ level, component, message = ctx.listNotes()[0]
+ self.assertEqual( level, logging.INFO )
self.assertEqual( component, 'foo' )
self.assertEqual( message, 'bar' )
@@ -737,17 +744,19 @@
from Products.GenericSetup.context import TarballExportContext
return TarballExportContext
- def test_note( self ):
+ def test_getLogger( self ):
site = DummySite( 'site' ).__of__( self.root )
ctx = self._getTargetClass()( site )
self.assertEqual( len( ctx.listNotes() ), 0 )
- ctx.note( 'foo', 'bar' )
+ logger = ctx.getLogger('foo')
+ logger.info('bar')
self.assertEqual( len( ctx.listNotes() ), 1 )
- component, message = ctx.listNotes()[0]
+ level, component, message = ctx.listNotes()[0]
+ self.assertEqual( level, logging.INFO )
self.assertEqual( component, 'foo' )
self.assertEqual( message, 'bar' )
@@ -818,7 +827,7 @@
return self._getTargetClass()( *args, **kw )
- def test_note( self ):
+ def test_getLogger( self ):
site = DummySite( 'site' ).__of__( self.root )
site.setup_tool = DummyTool( 'setup_tool' )
@@ -827,10 +836,12 @@
self.assertEqual( len( ctx.listNotes() ), 0 )
- ctx.note( 'foo', 'bar' )
+ logger = ctx.getLogger('foo')
+ logger.info('bar')
self.assertEqual( len( ctx.listNotes() ), 1 )
- component, message = ctx.listNotes()[0]
+ level, component, message = ctx.listNotes()[0]
+ self.assertEqual( level, logging.INFO )
self.assertEqual( component, 'foo' )
self.assertEqual( message, 'bar' )
@@ -1107,17 +1118,19 @@
return folder._getOb( filename )
- def test_note( self ):
+ def test_getLogger( self ):
SNAPSHOT_ID = 'note'
site, tool, ctx = self._makeOne( SNAPSHOT_ID )
self.assertEqual( len( ctx.listNotes() ), 0 )
- ctx.note( 'foo', 'bar' )
+ logger = ctx.getLogger('foo')
+ logger.info('bar')
self.assertEqual( len( ctx.listNotes() ), 1 )
- component, message = ctx.listNotes()[0]
+ level, component, message = ctx.listNotes()[0]
+ self.assertEqual( level, logging.INFO )
self.assertEqual( component, 'foo' )
self.assertEqual( message, 'bar' )
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_tool.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_tool.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tests/test_tool.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -448,7 +448,7 @@
self.assertEqual( len( result[ 'steps' ] ), 1 )
self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
self.assertEqual( result[ 'messages' ][ 'step_registries' ]
- , 'Step registries exported'
+ , None
)
fileish = StringIO( result[ 'tarball' ] )
@@ -473,7 +473,7 @@
self.assertEqual( len( result[ 'steps' ] ), 1 )
self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
self.assertEqual( result[ 'messages' ][ 'step_registries' ]
- , 'Step registries exported'
+ , None
)
fileish = StringIO( result[ 'tarball' ] )
@@ -516,7 +516,7 @@
self.failUnless( 'step_registries' in result[ 'steps' ] )
self.assertEqual( result[ 'messages' ][ 'step_registries' ]
- , 'Step registries exported'
+ , None
)
fileish = StringIO( result[ 'tarball' ] )
@@ -553,7 +553,7 @@
self.assertEqual( len( result[ 'steps' ] ), 1 )
self.assertEqual( result[ 'steps' ][ 0 ], 'step_registries' )
self.assertEqual( result[ 'messages' ][ 'step_registries' ]
- , 'Step registries exported'
+ , None
)
snapshot = result[ 'snapshot' ]
Modified: CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tool.py
===================================================================
--- CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tool.py 2005-11-17 20:56:59 UTC (rev 40200)
+++ CMF/branches/tseaver-resync_GenericSetup/GenericSetup/tool.py 2005-11-17 21:48:34 UTC (rev 40201)
@@ -53,6 +53,7 @@
""" Built-in handler for exporting import / export step registries.
"""
setup_tool = context.getSetupTool()
+ logger = context.getLogger('registries')
import_steps_xml = setup_tool.getImportStepRegistry().generateXML()
context.writeDataFile('import_steps.xml', import_steps_xml, 'text/xml')
@@ -60,7 +61,7 @@
export_steps_xml = setup_tool.getExportStepRegistry().generateXML()
context.writeDataFile('export_steps.xml', export_steps_xml, 'text/xml')
- return 'Step registries exported'
+ logger.info('Step registries exported.')
def importToolset(context):
@@ -68,10 +69,12 @@
"""
site = context.getSite()
encoding = context.getEncoding()
+ logger = context.getLogger('toolset')
xml = context.readDataFile(TOOLSET_XML)
if xml is None:
- return 'Toolset: Nothing to import.'
+ logger.info('Nothing to import.')
+ return
setup_tool = context.getSetupTool()
toolset = setup_tool.getToolsetRegistry()
@@ -108,7 +111,7 @@
site._delObject(tool_id)
site._setObject(tool_id, tool_class())
- return 'Toolset imported.'
+ logger.info('Toolset imported.')
def exportToolset(context):
@@ -116,11 +119,12 @@
"""
setup_tool = context.getSetupTool()
toolset = setup_tool.getToolsetRegistry()
+ logger = context.getLogger('toolset')
xml = toolset.generateXML()
context.writeDataFile(TOOLSET_XML, xml, 'text/xml')
- return 'Toolset exported.'
+ logger.info('Toolset exported.')
class SetupTool(Folder):
@@ -221,7 +225,7 @@
message = self._doRunImportStep(step_id, context)
message_list = filter(None, [message])
- message_list.extend( ['%s: %s' % x for x in context.listNotes()] )
+ message_list.extend( ['%s: %s' % x[1:] for x in context.listNotes()] )
messages[step_id] = '\n'.join(message_list)
steps.append(step_id)
@@ -240,7 +244,8 @@
for step in steps:
message = self._doRunImportStep(step, context)
message_list = filter(None, [message])
- message_list.extend( ['%s: %s' % x for x in context.listNotes()] )
+ message_list.extend( ['%s: %s' % x[1:]
+ for x in context.listNotes()] )
messages[step] = '\n'.join(message_list)
context.clearNotes()
More information about the CMF-checkins
mailing list