Commit de965679 authored by Julien Muchembled's avatar Julien Muchembled

Speed up Base.provides accessors by caching the result

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32255 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent f3910e51
##############################################################################
#
# Copyright (c) 2002-2009 Nexedi SARL and Contributors. All Rights Reserved.
# Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from Accessor import Accessor
# Creation of default constructor
class func_code: pass
class Getter(Accessor):
"""
This getter will calls the provides method. This allows
lazy evaluation of the interface list provided by every portal type
"""
_need__name__ = 1
# Generic Definition of Method Object
# This is required to call the method form the Web
# More information at http://www.zope.org/Members/htrd/howto/FunctionTemplate
func_code = func_code()
func_code.co_varnames = ('self', )
func_code.co_argcount = 1
func_defaults = ()
# default values
_value = None
def __init__(self, id, key):
self._id = id
self._key = key
def __call__(self, instance):
return instance.provides(self._key)
...@@ -818,17 +818,17 @@ class Base( CopyContainer, ...@@ -818,17 +818,17 @@ class Base( CopyContainer,
""" """
initializeClassDynamicProperties(self, self.__class__) initializeClassDynamicProperties(self, self.__class__)
security.declareProtected( Permissions.AccessContentsInformation, 'provides' ) security.declarePublic('provides')
def provides(self, interface_name): def provides(cls, interface_name):
""" """
Check if the current class provides a particular interface Check if the current class provides a particular interface
""" """
result = False for interface in implementedBy(cls):
for interface in implementedBy(self.__class__):
if interface.getName() == interface_name: if interface.getName() == interface_name:
result = True return True
break return False
return result provides = classmethod(CachingMethod(provides, 'Base.provides',
cache_factory='erp5_ui_long'))
def _aq_key(self): def _aq_key(self):
return (self.portal_type, self.__class__) return (self.portal_type, self.__class__)
......
...@@ -69,7 +69,6 @@ from Products.ERP5Type import Constraint ...@@ -69,7 +69,6 @@ from Products.ERP5Type import Constraint
from Products.ERP5Type.Accessor.Constant import PropertyGetter as \ from Products.ERP5Type.Accessor.Constant import PropertyGetter as \
PropertyConstantGetter PropertyConstantGetter
from Products.ERP5Type.Accessor.Constant import Getter as ConstantGetter from Products.ERP5Type.Accessor.Constant import Getter as ConstantGetter
from Products.ERP5Type.Accessor.Interface import Getter as InterfaceGetter
from Products.ERP5Type.Cache import getReadOnlyTransactionCache from Products.ERP5Type.Cache import getReadOnlyTransactionCache
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
from zLOG import LOG, BLATHER, PROBLEM, WARNING from zLOG import LOG, BLATHER, PROBLEM, WARNING
...@@ -594,16 +593,12 @@ def importLocalInterface(module_id, path = None, is_erp5_type=False): ...@@ -594,16 +593,12 @@ def importLocalInterface(module_id, path = None, is_erp5_type=False):
module = imp.load_source(class_id, path, f) module = imp.load_source(class_id, path, f)
import Products.ERP5Type.interfaces import Products.ERP5Type.interfaces
setattr(Products.ERP5Type.interfaces, class_id, getattr(module, class_id)) setattr(Products.ERP5Type.interfaces, class_id, getattr(module, class_id))
# Create interface getter
accessor_name = 'provides' + class_id
accessor = InterfaceGetter(accessor_name, class_id)
setattr(BaseClass, accessor_name, accessor)
BaseClass.security.declareProtected(
Permissions.AccessContentsInformation, accessor_name)
finally: finally:
f.close() f.close()
# Create interface getter
accessor_name = 'provides' + class_id
setattr(BaseClass, accessor_name, lambda self: self.provides(class_id))
BaseClass.security.declarePublic(accessor_name)
def importLocalConstraint(class_id, path = None): def importLocalConstraint(class_id, path = None):
import Products.ERP5Type.Constraint import Products.ERP5Type.Constraint
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment