Back to index

plone3  3.1.7
patch.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # PlonePAS - Adapt PluggableAuthService for use in Plone
00004 # Copyright (C) 2005 Enfold Systems, Kapil Thangavelu, et al
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """
00016 """
00017 import logging
00018 
00019 logger = logging.getLogger('Plone')
00020 
00021 
00022 PATTERN = '__PlonePAS_%s__'
00023 def call(self, __name__, *args, **kw):
00024     return getattr(self, PATTERN % __name__)(*args, **kw)
00025 
00026 WRAPPER = '__PlonePAS_is_wrapper_method__'
00027 ORIG_NAME = '__PlonePAS_original_method_name__'
00028 def isWrapperMethod(meth):
00029     return getattr(meth, WRAPPER, False)
00030 
00031 def wrap_method(klass, name, method, pattern=PATTERN):
00032     old_method = getattr(klass, name)
00033     if isWrapperMethod(old_method):
00034         logger.info('PlonePAS: *NOT* wrapping already wrapped method at %s.%s',
00035             klass.__name__, name)
00036         return
00037     else:
00038         logger.info('PlonePAS: Wrapping method at %s.%s', klass.__name__, name)
00039     new_name = pattern % name
00040     setattr(klass, new_name, old_method)
00041     setattr(method, ORIG_NAME, new_name)
00042     setattr(method, WRAPPER, True)
00043     setattr(klass, name, method)
00044 
00045 def unwrap_method(klass, name):
00046     old_method = getattr(klass, name)
00047     if not isWrapperMethod(old_method):
00048         raise ValueError, ('Trying to unwrap non-wrapped '
00049                            'method at %s.%s' % (klass.__name__, name))
00050     orig_name = getattr(old_method, ORIG_NAME)
00051     new_method = getattr(klass, orig_name)
00052     delattr(klass, orig_name)
00053     setattr(klass, name, new_method)