[Zope-Checkins] SVN: Zope/branches/2.12/src/App/ Warn on App.ImageFile.ImageFile deprecated assumption of software_home

Leonardo Rochael Almeida leorochael at gmail.com
Wed Jul 14 11:07:12 EDT 2010


Log message for revision 114749:
  Warn on App.ImageFile.ImageFile deprecated assumption of software_home

Changed:
  U   Zope/branches/2.12/src/App/ImageFile.py
  U   Zope/branches/2.12/src/App/config.py
  A   Zope/branches/2.12/src/App/tests/testImageFile.py

-=-
Modified: Zope/branches/2.12/src/App/ImageFile.py
===================================================================
--- Zope/branches/2.12/src/App/ImageFile.py	2010-07-14 14:58:01 UTC (rev 114748)
+++ Zope/branches/2.12/src/App/ImageFile.py	2010-07-14 15:07:11 UTC (rev 114749)
@@ -18,6 +18,7 @@
 import os.path
 import stat
 import time
+import warnings
 
 from AccessControl.SecurityInfo import ClassSecurityInfo
 from Acquisition import Explicit
@@ -34,6 +35,13 @@
     os.path.join(os.path.dirname(Zope2.__file__), os.path.pardir)
     )
 
+NON_PREFIX_WARNING = ('Assuming image location to be present in the Zope2 '
+                      'distribution. This is deprecated and might lead to ' 
+                      'broken code if the directory in question is moved ' 
+                      'to another distribution. Please provide either an '
+                      'absolute file system path or a prefix. Support for ' 
+                      'relative filenames without a prefix might be '
+                      'dropped in a future Zope2 release.')
 
 class ImageFile(Explicit):
     """Image objects stored in external files."""
@@ -43,9 +51,12 @@
     def __init__(self, path, _prefix=None):
         import Globals  # for data
         if _prefix is None:
-            _prefix=getattr(getConfiguration(), 'softwarehome', PREFIX)
+            _prefix=getattr(getConfiguration(), 'softwarehome', None) or PREFIX
+            if not os.path.isabs(path):
+                warnings.warn(NON_PREFIX_WARNING, UserWarning, 2 )
         elif type(_prefix) is not type(''):
             _prefix=package_home(_prefix)
+        # _prefix is ignored if path is absolute
         path = os.path.join(_prefix, path)
         self.path=path
         if Globals.DevelopmentMode:

Modified: Zope/branches/2.12/src/App/config.py
===================================================================
--- Zope/branches/2.12/src/App/config.py	2010-07-14 14:58:01 UTC (rev 114748)
+++ Zope/branches/2.12/src/App/config.py	2010-07-14 15:07:11 UTC (rev 114749)
@@ -36,7 +36,7 @@
 def setConfiguration(cfg):
     """Set the global configuration object.
 
-    Legacy sources of common configuraiton values are updated to
+    Legacy sources of common configuration values are updated to
     reflect the new configuration; this may be removed in some future
     version.
     """

Added: Zope/branches/2.12/src/App/tests/testImageFile.py
===================================================================
--- Zope/branches/2.12/src/App/tests/testImageFile.py	                        (rev 0)
+++ Zope/branches/2.12/src/App/tests/testImageFile.py	2010-07-14 15:07:11 UTC (rev 114749)
@@ -0,0 +1,45 @@
+import unittest
+import os.path
+import App
+from Testing.ZopeTestCase.warnhook import WarningsHook
+
+
+class TestImageFile(unittest.TestCase):
+
+    def setUp(self):
+        # ugly: need to save the old App.config configuration value since
+        # ImageFile might read it and trigger setting it to the default value 
+        self.oldcfg = App.config._config
+        self.warningshook = WarningsHook()
+        self.warningshook.install()
+
+    def tearDown(self):
+        self.warningshook.uninstall()
+        # ugly: need to restore configuration, or lack thereof
+        App.config._config = self.oldcfg
+
+    def test_warn_on_software_home_default(self):
+        App.ImageFile.ImageFile('App/www/zopelogo.jpg')
+        self.assertEquals(self.warningshook.warnings.pop()[0],
+                          App.ImageFile.NON_PREFIX_WARNING)
+
+    def test_no_warn_on_absolute_path(self):
+        path = os.path.join(os.path.dirname(App.__file__),
+                            'www','zopelogo.jpg')
+        App.ImageFile.ImageFile(path)
+        self.failIf(self.warningshook.warnings)
+
+    def test_no_warn_on_path_as_prefix(self):
+        prefix = os.path.dirname(App.__file__)
+        App.ImageFile.ImageFile('www/zopelogo.jpg', prefix)
+        self.failIf(self.warningshook.warnings)
+
+    def test_no_warn_on_namespace_as_prefix(self):
+        prefix = App.__dict__ # same as calling globals() inside the App module
+        App.ImageFile.ImageFile('www/zopelogo.jpg', prefix)
+        self.failIf(self.warningshook.warnings)
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(TestImageFile),
+        ))



More information about the Zope-Checkins mailing list