[Erp5-report] r28367 - in /erp5/trunk/products: ERP5/Document/ ERP5OOo/Document/

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Aug 13 16:14:19 CEST 2009


Author: kazuhiko
Date: Thu Aug 13 16:14:18 2009
New Revision: 28367

URL: http://svn.erp5.org?rev=28367&view=rev
Log:
no need to call hasConversion() before getConversion() because it does mostly the same thing and waste of time. use try ... except KeyError instead.

Modified:
    erp5/trunk/products/ERP5/Document/Document.py
    erp5/trunk/products/ERP5OOo/Document/OOoDocument.py

Modified: erp5/trunk/products/ERP5/Document/Document.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/Document.py?rev=28367&r1=28366&r2=28367&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Document/Document.py [utf8] (original)
+++ erp5/trunk/products/ERP5/Document/Document.py [utf8] Thu Aug 13 16:14:18 2009
@@ -171,22 +171,28 @@
   security.declareProtected(Permissions.View, 'hasConversion')
   def hasConversion(self, **kw):
     """
-    """
-    self.updateConversionCache()
-    cache_id = self.generateCacheId(**kw)
-    if self.isTempObject():
-      temp_conversion_dict = getattr(aq_base(self), 'temp_conversion_data')
-      return temp_conversion_dict.has_key(cache_id)
-    cache_factory = self._getCacheFactory()
-    plugin_list = cache_factory.getCachePluginList()
-    #If there is no plugin list return False OR one them is doesn't contain
-    #cache_id for givent scope, return False
-    for cache_plugin in plugin_list:
-      if cache_plugin.has_key(self.getPath(), DEFAULT_CACHE_SCOPE):
-        cache_entry = cache_plugin.get(self.getPath(), DEFAULT_CACHE_SCOPE)
-        if cache_entry.getValue().has_key(cache_id):
-          return True
-    return False
+    If you want to get conversion cache value if exists, please write
+    the code like:
+
+      try:
+        mime, data = getConversion(**kw)
+      except KeyError:
+        ...
+
+    instead of:
+
+      if self.hasConversion(**kw):
+        mime, data = self.getConversion(**kw)
+      else:
+        ...
+
+    for better performance.
+    """
+    try:
+      self.getConversion(**kw)
+      return True
+    except KeyError:
+      return False
 
   security.declareProtected(Permissions.ModifyPortalContent, 'setConversion')
   def setConversion(self, data, mime=None, calculation_time=None, **kw):
@@ -232,9 +238,10 @@
   def getConversionSize(self, **kw):
     """
     """
-    if self.hasConversion(**kw):
+    try:
       return len(self.getConversion(**kw))
-    return 0
+    except KeyError:
+      return 0
 
   def generateCacheId(self, **kw):
     """Generate proper cache id based on **kw.
@@ -1149,13 +1156,14 @@
     """
     if not self.hasBaseData():
       raise ConversionError('This document has not been processed yet.')
-    if self.hasConversion(format='base-html'):
+    try:
       # FIXME: no substitution may occur in this case.
       mime, data = self.getConversion(format='base-html')
       return data
-    kw['format'] = 'html'
-    mime, html = self.convert(**kw)
-    return html
+    except KeyError:
+      kw['format'] = 'html'
+      mime, html = self.convert(**kw)
+      return html
 
   security.declareProtected(Permissions.View, 'asStrippedHTML')
   def asStrippedHTML(self, **kw):
@@ -1166,13 +1174,14 @@
     """
     if not self.hasBaseData():
       return ''
-    if self.hasConversion(format='stripped-html'): # XXX this is redundant since we never set it
+    try:
       # FIXME: no substitution may occur in this case.
       mime, data = self.getConversion(format='stripped-html')
       return data
-    kw['format'] = 'html'
-    mime, html = self.convert(**kw)
-    return self._stripHTML(str(html))
+    except KeyError:
+      kw['format'] = 'html'
+      mime, html = self.convert(**kw)
+      return self._stripHTML(str(html))
 
   def _guessEncoding(self, string):
     """

Modified: erp5/trunk/products/ERP5OOo/Document/OOoDocument.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/Document/OOoDocument.py?rev=28367&r1=28366&r2=28367&view=diff
==============================================================================
--- erp5/trunk/products/ERP5OOo/Document/OOoDocument.py [utf8] (original)
+++ erp5/trunk/products/ERP5OOo/Document/OOoDocument.py [utf8] Thu Aug 13 16:14:18 2009
@@ -500,10 +500,11 @@
       archive_file.close()
 
   def _getExtensibleContent(self, request, name):
-    if self.hasConversion(format='_embedded', file_name=name):
+    try:
       mime, data = self.getConversion(format='_embedded', file_name=name)
       return OFSFile(name, name, data, content_type=mime).__of__(self.aq_parent)
-    return PermanentURLMixIn._getExtensibleContent(self, request, name)
+    except KeyError:
+      return PermanentURLMixIn._getExtensibleContent(self, request, name)
 
   # Base format implementation
   security.declareProtected(Permissions.AccessContentsInformation, 'hasBaseData')




More information about the Erp5-report mailing list