Commit e8cf6e06 authored by Hanno Schlichting's avatar Hanno Schlichting

Added LAZY_FILE_LOADING constant to PageTemplateFile. When set to True Page...

Added LAZY_FILE_LOADING constant to PageTemplateFile. When set to True Page Template files aren't lo
aded and parsed on Zope startup anymore, but on first access instead. When complex add-ons like Plon
e are installed this can safe up to 30% of the Zope startup time. This gets a ZConfig switch ones I 
figured out how to write one ;)
parent 085d587f
......@@ -71,6 +71,10 @@ Zope Changes
Features added
- Added LAZY_FILE_LOADING constant to PageTemplateFile. When set to True
Page Template files aren't lo aded and parsed on Zope startup anymore,
but on first access instead.
- Testing.ZopeTestCase: Introduced a "ZopeLite" test layer, making it
possible to mix ZTC and non-ZTC tests much more freely.
......
......@@ -31,6 +31,8 @@ from zope.pagetemplate.pagetemplatefile import sniff_type
LOG = getLogger('PageTemplateFile')
LAZY_FILE_LOADING = False
def guess_type(filename, text):
# check for XML ourself since guess_content_type can't
......@@ -86,8 +88,9 @@ class PageTemplateFile(SimpleItem, Script, PageTemplate, Traversable):
self.filename = filename
content = open(filename).read()
self.pt_edit( content, guess_type(filename, content))
if not LAZY_FILE_LOADING:
content = open(filename).read()
self.pt_edit( content, guess_type(filename, content))
def pt_getContext(self):
root = self.getPhysicalRoot()
......
......@@ -8,6 +8,7 @@ import transaction
from Testing.makerequest import makerequest
from Products.PageTemplates import PageTemplateFile as PTF
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
......@@ -192,10 +193,42 @@ class LineEndingsTestCase(unittest.TestCase):
def test_mac(self):
self.assertEqual(self.runPTWithLineEndings('\r'), self.OUTPUT)
class LazyLoadingTestCase(unittest.TestCase):
TEMPFILENAME = tempfile.mktemp(".zpt")
OLD_LAZY = None
def setUp(self):
self.OLD_LAZY = PTF.LAZY_FILE_LOADING
def tearDown(self):
if os.path.exists(self.TEMPFILENAME):
os.unlink(self.TEMPFILENAME)
PTF.LAZY_FILE_LOADING = self.OLD_LAZY
def test_not_lazy(self):
f = open(self.TEMPFILENAME, 'w')
print >> f, 'Lazyness'
f.close()
pt = PageTemplateFile(self.TEMPFILENAME)
self.failUnless(pt._text.startswith('Lazyness'))
self.failUnless(pt._v_program)
def test_lazy(self):
f = open(self.TEMPFILENAME, 'w')
print >> f, 'Lazyness'
f.close()
PTF.LAZY_FILE_LOADING = True
pt = PageTemplateFile(self.TEMPFILENAME)
self.failUnless(not pt._text and not pt._v_program)
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(TypeSniffingTestCase),
unittest.makeSuite(LineEndingsTestCase),
unittest.makeSuite(LazyLoadingTestCase),
))
if __name__ == "__main__":
......
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