[CMF-checkins] SVN: CMF/branches/tseaver-pkg_resources/ Avoid using __file__ where possible.

Tres Seaver tseaver at palladion.com
Thu Nov 10 14:41:33 EST 2005


Log message for revision 40036:
  Avoid using __file__ where possible.

Changed:
  U   CMF/branches/tseaver-pkg_resources/CMFCore/CookieCrumbler.py
  U   CMF/branches/tseaver-pkg_resources/CMFCore/FSObject.py
  U   CMF/branches/tseaver-pkg_resources/CMFCore/utils.py
  U   CMF/branches/tseaver-pkg_resources/CMFDefault/tests/test_Document.py
  U   CMF/branches/tseaver-pkg_resources/DCWorkflow/WorkflowUIMixin.py
  U   CMF/branches/tseaver-pkg_resources/GenericSetup/tests/test_context.py
  U   CMF/branches/tseaver-pkg_resources/GenericSetup/utils.py

-=-
Modified: CMF/branches/tseaver-pkg_resources/CMFCore/CookieCrumbler.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/CMFCore/CookieCrumbler.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/CMFCore/CookieCrumbler.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -420,13 +420,10 @@
     ''' Create default forms inside ob '''
     import os
     from OFS.DTMLMethod import addDTMLMethod
-    dtmldir = os.path.join(os.path.dirname(__file__), 'dtml')
+    from utils import resource_string
     for fn in ('index_html', 'logged_in', 'logged_out', 'login_form',
                 'standard_login_footer', 'standard_login_header'):
-        filename = os.path.join(dtmldir, fn + '.dtml')
-        f = open(filename, 'rt')
-        try: data = f.read()
-        finally: f.close()
+        data = resource_string('dtml/%s' % fn, globals())
         addDTMLMethod(ob, fn, file=data)
 
 def manage_addCC(dispatcher, id, create_forms=0, REQUEST=None):

Modified: CMF/branches/tseaver-pkg_resources/CMFCore/FSObject.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/CMFCore/FSObject.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/CMFCore/FSObject.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -19,16 +19,6 @@
 import sys
 import time
 
-try:
-    from pkg_resources import resource_string
-    from pkg_resources import get_provider
-    from pkg_resources import ZipProvider
-    _HAS_PKG_RESOURCES = True
-except ImportError:
-    _HAS_PKG_RESOURCES = False
-    class ZipProvider:  # silence!
-        pass
-
 import Globals
 from AccessControl import ClassSecurityInfo
 from AccessControl.Role import RoleManager
@@ -47,6 +37,10 @@
 from permissions import ViewManagementScreens
 from utils import expandpath
 from utils import getToolByName
+from utils import get_provider
+from utils import resource_string
+from utils import ZipProvider
+from utils import _HAS_PKG_RESOURCES
 
 
 class FSObject(Implicit, Item, RoleManager, Cacheable):

Modified: CMF/branches/tseaver-pkg_resources/CMFCore/utils.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/CMFCore/utils.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/CMFCore/utils.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -63,6 +63,39 @@
     from Globals import DTMLResource
     from Products.PageTemplates import PageTemplateResource
 
+try:
+    from pkg_resources import resource_string
+    from pkg_resources import resource_listdir
+    from pkg_resources import resource_isdir
+    from pkg_resources import get_provider
+    from pkg_resources import ZipProvider
+    _HAS_PKG_RESOURCES = True
+
+except ImportError:
+    _HAS_PKG_RESOURCES = False
+
+    class ZipProvider:  # silence!
+        pass
+
+    def resource_string(pkg_globals, relative_path):
+        from Globals import package_home
+        filename = os.path.join(package_home(pkg_globals), relative_path)
+        f = open(filename, 'rb')
+        try:
+            return f.read()
+        finally:
+            f.close()
+
+    def resource_listdir(pkg_globals, relative_path):
+        from Globals import package_home
+        dirname = os.path.join(package_home(pkg_globals), relative_path)
+        return os.listdir(dirname)
+
+    def resource_isdir(pkg_globals, relative_path):
+        from Globals import package_home
+        dirname = os.path.join(package_home(pkg_globals), relative_path)
+        return os.path.isdir(dirname)
+
 security = ModuleSecurityInfo( 'Products.CMFCore.utils' )
 
 #

Modified: CMF/branches/tseaver-pkg_resources/CMFDefault/tests/test_Document.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/CMFDefault/tests/test_Document.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/CMFDefault/tests/test_Document.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -406,6 +406,9 @@
 class DocumentFTPGetTests(RequestTestBase):
 
     def testHTML( self ):
+        from Products.CMFCore.utils import resource_string
+        import Products.CMFDefault
+
         self.REQUEST['BODY']=BASIC_HTML
 
         ttool = self.site._setObject( 'portal_types', TypesTool() )
@@ -415,9 +418,8 @@
 
         zpt = self.site._setObject( 'source_html',
                                     ZopePageTemplate('source_html') )
-        dir = abspath( dirname(utils.__file__) )
-        _file = path_join(dir, 'skins', 'zpt_content', 'source_html.pt')
-        data = open(_file, 'r').read()
+        data = resource_string(Products.CMFDefault.__name__,
+                               'skins/zpt_content/source_html.pt')
         zpt.write(data)
 
         d = self._makeOne('foo')

Modified: CMF/branches/tseaver-pkg_resources/DCWorkflow/WorkflowUIMixin.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/DCWorkflow/WorkflowUIMixin.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/DCWorkflow/WorkflowUIMixin.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -23,6 +23,7 @@
 
 from Products.CMFCore.utils import DTMLResource
 from Products.CMFCore.utils import PageTemplateResource
+from Products.CMFCore.utils import resource_string
 
 from permissions import ManagePortal
 from Guard import Guard
@@ -206,13 +207,7 @@
     def guardExprDocs(self):
         """Returns documentation on guard expressions.
         """
-        here = os.path.dirname(__file__)
-        fn = os.path.join(here, 'doc', 'expressions.stx')
-        f = open(fn, 'rt')
-        try:
-            text = f.read()
-        finally:
-            f.close()
+        text = resource_string('doc/expressions.stx', globals())
         from DocumentTemplate.DT_Var import structured_text
         return structured_text(text)
 

Modified: CMF/branches/tseaver-pkg_resources/GenericSetup/tests/test_context.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/GenericSetup/tests/test_context.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/GenericSetup/tests/test_context.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -757,13 +757,10 @@
     def test_writeDataFile_simple_image( self ):
 
         from OFS.Image import Image
+        from Products.GenericSetup.utils import resource_string
         FILENAME = 'simple.txt'
         CONTENT_TYPE = 'image/png'
-        png_filename = os.path.join( os.path.split( __file__ )[0]
-                                   , 'simple.png' )
-        png_file = open( png_filename, 'rb' )
-        png_data = png_file.read()
-        png_file.close()
+        png_data = resource_string(__name__, 'simple.png')
 
         site = DummySite( 'site' ).__of__( self.root )
         site.setup_tool = DummyTool( 'setup_tool' )

Modified: CMF/branches/tseaver-pkg_resources/GenericSetup/utils.py
===================================================================
--- CMF/branches/tseaver-pkg_resources/GenericSetup/utils.py	2005-11-10 19:36:25 UTC (rev 40035)
+++ CMF/branches/tseaver-pkg_resources/GenericSetup/utils.py	2005-11-10 19:41:31 UTC (rev 40036)
@@ -43,6 +43,18 @@
     from Globals import DTMLResource
     from Products.PageTemplates import PageTemplateResource
 
+try:
+    from pkg_resources import resource_string
+except ImportError:
+    def resource_string(pkg_name, relative_path):
+        from Globals import package_home
+        filename = os.path.join(package_home(pkg_globals), relative_path)
+        f = open(filename, 'rb')
+        try:
+            return f.read()
+        finally:
+            f.close()
+
 from exceptions import BadRequest
 from interfaces import INodeExporter
 from interfaces import INodeImporter



More information about the CMF-checkins mailing list