Warn on App.ImageFile.ImageFile deprecated assumption of software_home

parent df1c8297
...@@ -18,6 +18,7 @@ import os ...@@ -18,6 +18,7 @@ import os
import os.path import os.path
import stat import stat
import time import time
import warnings
from AccessControl.SecurityInfo import ClassSecurityInfo from AccessControl.SecurityInfo import ClassSecurityInfo
from Acquisition import Explicit from Acquisition import Explicit
...@@ -34,6 +35,13 @@ PREFIX = os.path.realpath( ...@@ -34,6 +35,13 @@ PREFIX = os.path.realpath(
os.path.join(os.path.dirname(Zope2.__file__), os.path.pardir) 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): class ImageFile(Explicit):
"""Image objects stored in external files.""" """Image objects stored in external files."""
...@@ -43,9 +51,12 @@ class ImageFile(Explicit): ...@@ -43,9 +51,12 @@ class ImageFile(Explicit):
def __init__(self, path, _prefix=None): def __init__(self, path, _prefix=None):
import Globals # for data import Globals # for data
if _prefix is None: 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(''): elif type(_prefix) is not type(''):
_prefix=package_home(_prefix) _prefix=package_home(_prefix)
# _prefix is ignored if path is absolute
path = os.path.join(_prefix, path) path = os.path.join(_prefix, path)
self.path=path self.path=path
if Globals.DevelopmentMode: if Globals.DevelopmentMode:
......
...@@ -36,7 +36,7 @@ def getConfiguration(): ...@@ -36,7 +36,7 @@ def getConfiguration():
def setConfiguration(cfg): def setConfiguration(cfg):
"""Set the global configuration object. """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 reflect the new configuration; this may be removed in some future
version. version.
""" """
......
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),
))
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