Commit 6c79cd9d authored by Fred Drake's avatar Fred Drake

- avoid re-reading more data than necessary

- remove unused imports
- misc. cleanup
parent 64db138b
...@@ -15,19 +15,18 @@ ...@@ -15,19 +15,18 @@
Zope object encapsulating a Page Template from the filesystem. Zope object encapsulating a Page Template from the filesystem.
""" """
__version__='$Revision: 1.25 $'[11:-2] __version__='$Revision: 1.26 $'[11:-2]
import os, AccessControl, Acquisition, sys import os, AccessControl
from Globals import package_home, DevelopmentMode from Globals import package_home, DevelopmentMode
from zLOG import LOG, ERROR, INFO from zLOG import LOG, ERROR
from Shared.DC.Scripts.Script import Script, BindingsUI from Shared.DC.Scripts.Script import Script
from Shared.DC.Scripts.Signature import FuncCode from Shared.DC.Scripts.Signature import FuncCode
from AccessControl import getSecurityManager from AccessControl import getSecurityManager
from OFS.Traversable import Traversable from OFS.Traversable import Traversable
from PageTemplate import PageTemplate from PageTemplate import PageTemplate
from Expressions import SecureModuleImporter from Expressions import SecureModuleImporter
from ComputedAttribute import ComputedAttribute from ComputedAttribute import ComputedAttribute
from ExtensionClass import Base
from Acquisition import aq_parent, aq_inner from Acquisition import aq_parent, aq_inner
from App.config import getConfiguration from App.config import getConfiguration
...@@ -51,7 +50,7 @@ class PageTemplateFile(Script, PageTemplate, Traversable): ...@@ -51,7 +50,7 @@ class PageTemplateFile(Script, PageTemplate, Traversable):
self.ZBindings_edit(self._default_bindings) self.ZBindings_edit(self._default_bindings)
if _prefix is None: if _prefix is None:
_prefix = getConfiguration().softwarehome _prefix = getConfiguration().softwarehome
elif type(_prefix) is not type(''): elif not isinstance(_prefix, str):
_prefix = package_home(_prefix) _prefix = package_home(_prefix)
name = kw.get('__name__') name = kw.get('__name__')
if name: if name:
...@@ -91,7 +90,7 @@ class PageTemplateFile(Script, PageTemplate, Traversable): ...@@ -91,7 +90,7 @@ class PageTemplateFile(Script, PageTemplate, Traversable):
pass pass
# Execute the template in a new security context. # Execute the template in a new security context.
security=getSecurityManager() security = getSecurityManager()
bound_names['user'] = security.getUser() bound_names['user'] = security.getUser()
security.addContext(self) security.addContext(self)
try: try:
...@@ -119,15 +118,18 @@ class PageTemplateFile(Script, PageTemplate, Traversable): ...@@ -119,15 +118,18 @@ class PageTemplateFile(Script, PageTemplate, Traversable):
return return
f = open(self.filename, "rb") f = open(self.filename, "rb")
try: try:
text = f.read() text = f.read(XML_PREFIX_MAX_LENGTH)
finally: except:
f.close() f.close()
raise
t = sniff_type(text) t = sniff_type(text)
if t != "text/xml" and "\r" in text: if t != "text/xml":
# For HTML, we really want the file read in text mode: # For HTML, we really want the file read in text mode:
f = open(self.filename)
text = f.read()
f.close() f.close()
f = open(self.filename)
text = ''
text += f.read()
f.close()
self.pt_edit(text, t) self.pt_edit(text, t)
self._cook() self._cook()
if self._v_errors: if self._v_errors:
...@@ -180,6 +182,8 @@ XML_PREFIXES = [ ...@@ -180,6 +182,8 @@ XML_PREFIXES = [
"\xff\xfe<\0?\0x\0m\0l\0", # utf-16 little endian w/ byte order mark "\xff\xfe<\0?\0x\0m\0l\0", # utf-16 little endian w/ byte order mark
] ]
XML_PREFIX_MAX_LENGTH = max(map(len, XML_PREFIXES))
def sniff_type(text): def sniff_type(text):
for prefix in XML_PREFIXES: for prefix in XML_PREFIXES:
if text.startswith(prefix): if text.startswith(prefix):
......
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