Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
fe148408
Commit
fe148408
authored
Mar 21, 2003
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backport the type-sniffing code from Zope 3 to support XML page templates
from the filesystem.
parent
9a595cb2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
2 deletions
+149
-2
lib/python/Products/PageTemplates/PageTemplateFile.py
lib/python/Products/PageTemplates/PageTemplateFile.py
+23
-2
lib/python/Products/PageTemplates/tests/test_ptfile.py
lib/python/Products/PageTemplates/tests/test_ptfile.py
+126
-0
No files found.
lib/python/Products/PageTemplates/PageTemplateFile.py
View file @
fe148408
...
...
@@ -15,7 +15,7 @@
Zope object encapsulating a Page Template from the filesystem.
"""
__version__
=
'$Revision: 1.2
3
$'
[
11
:
-
2
]
__version__
=
'$Revision: 1.2
4
$'
[
11
:
-
2
]
import
os
,
AccessControl
,
Acquisition
,
sys
from
Globals
import
package_home
,
DevelopmentMode
...
...
@@ -117,7 +117,12 @@ class PageTemplateFile(Script, PageTemplate, Traversable):
mtime
=
0
if
self
.
_v_program
is
not
None
and
mtime
==
self
.
_v_last_read
:
return
self
.
pt_edit
(
open
(
self
.
filename
),
None
)
f
=
open
(
self
.
filename
,
"rb"
)
try
:
text
=
f
.
read
()
finally
:
f
.
close
()
self
.
pt_edit
(
text
,
sniff_type
(
text
))
self
.
_cook
()
if
self
.
_v_errors
:
LOG
(
'PageTemplateFile'
,
ERROR
,
'Error in template'
,
...
...
@@ -154,3 +159,19 @@ class PageTemplateFile(Script, PageTemplate, Traversable):
from
ZODB.POSException
import
StorageError
raise
StorageError
,
(
"Instance of AntiPersistent class %s "
"cannot be stored."
%
self
.
__class__
.
__name__
)
XML_PREFIXES
=
[
"<?xml"
,
# ascii, utf-8
"
\
xef
\
xbb
\
xbf
<?xml"
,
# utf-8 w/ byte order mark
"
\
0
<
\
0
?
\
0
x
\
0
m
\
0
l"
,
# utf-16 big endian
"<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
"
,
# utf-16 little endian
"
\
xfe
\
xff
\
0
<
\
0
?
\
0
x
\
0
m
\
0
l"
,
# utf-16 big endian w/ byte order mark
"
\
xff
\
xfe
<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
"
,
# utf-16 little endian w/ byte order mark
]
def
sniff_type
(
text
):
for
prefix
in
XML_PREFIXES
:
if
text
.
startswith
(
prefix
):
return
"text/xml"
return
None
lib/python/Products/PageTemplates/tests/test_ptfile.py
0 → 100644
View file @
fe148408
"""Tests of PageTemplateFile."""
import
os
import
tempfile
import
unittest
from
Products.PageTemplates.PageTemplateFile
import
PageTemplateFile
class
TypeSniffingTestCase
(
unittest
.
TestCase
):
TEMPFILENAME
=
tempfile
.
mktemp
()
def
tearDown
(
self
):
if
os
.
path
.
exists
(
self
.
TEMPFILENAME
):
os
.
unlink
(
self
.
TEMPFILENAME
)
def
check_content_type
(
self
,
text
,
expected_type
):
f
=
open
(
self
.
TEMPFILENAME
,
"wb"
)
f
.
write
(
text
)
f
.
close
()
pt
=
PageTemplateFile
(
self
.
TEMPFILENAME
)
pt
.
read
()
self
.
assertEqual
(
pt
.
content_type
,
expected_type
)
def
test_sniffer_xml_ascii
(
self
):
self
.
check_content_type
(
"<?xml version='1.0' encoding='ascii'?><doc/>"
,
"text/xml"
)
self
.
check_content_type
(
"<?xml
\
t
version='1.0' encoding='ascii'?><doc/>"
,
"text/xml"
)
def
test_sniffer_xml_utf8
(
self
):
# w/out byte order mark
self
.
check_content_type
(
"<?xml version='1.0' encoding='utf-8'?><doc/>"
,
"text/xml"
)
self
.
check_content_type
(
"<?xml
\
t
version='1.0' encoding='utf-8'?><doc/>"
,
"text/xml"
)
# with byte order mark
self
.
check_content_type
(
"
\
xef
\
xbb
\
xbf
<?xml version='1.0' encoding='utf-8'?><doc/>"
,
"text/xml"
)
self
.
check_content_type
(
"
\
xef
\
xbb
\
xbf
<?xml
\
t
version='1.0' encoding='utf-8'?><doc/>"
,
"text/xml"
)
def
test_sniffer_xml_utf16_be
(
self
):
# w/out byte order mark
self
.
check_content_type
(
"
\
0
<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'"
"
\
0
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>"
"
\
0
<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>"
,
"text/xml"
)
self
.
check_content_type
(
"
\
0
<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
t
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'"
"
\
0
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>"
"
\
0
<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>"
,
"text/xml"
)
# with byte order mark
self
.
check_content_type
(
"
\
xfe
\
xff
"
"
\
0
<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'"
"
\
0
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>"
"
\
0
<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>"
,
"text/xml"
)
self
.
check_content_type
(
"
\
xfe
\
xff
"
"
\
0
<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
t
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'"
"
\
0
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>"
"
\
0
<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>"
,
"text/xml"
)
def
test_sniffer_xml_utf16_le
(
self
):
# w/out byte order mark
self
.
check_content_type
(
"<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'
\
0
"
"
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>
\
0
"
"<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>
\
n
"
,
"text/xml"
)
self
.
check_content_type
(
"<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
t
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'
\
0
"
"
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>
\
0
"
"<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>
\
0
"
,
"text/xml"
)
# with byte order mark
self
.
check_content_type
(
"
\
xff
\
xfe
"
"<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'
\
0
"
"
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>
\
0
"
"<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>
\
0
"
,
"text/xml"
)
self
.
check_content_type
(
"
\
xff
\
xfe
"
"<
\
0
?
\
0
x
\
0
m
\
0
l
\
0
\
t
\
0
v
\
0
e
\
0
r
\
0
s
\
0
i
\
0
o
\
0
n
\
0
=
\
0
'
\
01
\
0
.
\
000
0
\
0
'
\
0
"
"
\
0
e
\
0
n
\
0
c
\
0
o
\
0
d
\
0
i
\
0
n
\
0
g
\
0
=
\
0
'
\
0
u
\
0
t
\
0
f
\
0
-
\
0
8
\
0
'
\
0
?
\
0
>
\
0
"
"<
\
0
d
\
0
o
\
0
c
\
0
/
\
0
>
\
0
"
,
"text/xml"
)
HTML_PUBLIC_ID
=
"-//W3C//DTD HTML 4.01 Transitional//EN"
HTML_SYSTEM_ID
=
"http://www.w3.org/TR/html4/loose.dtd"
def
test_sniffer_html_ascii
(
self
):
self
.
check_content_type
(
"<!DOCTYPE html [ SYSTEM '%s' ]><html></html>"
%
self
.
HTML_SYSTEM_ID
,
"text/html"
)
self
.
check_content_type
(
"<html><head><title>sample document</title></head></html>"
,
"text/html"
)
# XXX This reflects a case that simply isn't handled by the
# sniffer; there are many, but it gets it right more often than
# before.
def
donttest_sniffer_xml_simple
(
self
):
self
.
check_content_type
(
"<doc><element/></doc>"
,
"text/xml"
)
def
test_suite
():
return
unittest
.
makeSuite
(
TypeSniffingTestCase
)
if
__name__
==
"__main__"
:
unittest
.
main
(
defaultTest
=
"test_suite"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment