Skip to content

Commit

Permalink
demonstrate a view and template loaded directly from a ZIP archive,
Browse files Browse the repository at this point in the history
and make it actually work
  • Loading branch information
freddrake committed Nov 10, 2005
1 parent 5e84ff6 commit ff05a1e
Show file tree
Hide file tree
Showing 8 changed files with 592 additions and 0 deletions.
51 changes: 51 additions & 0 deletions browser/tests/test_zipped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
##############################################################################
#
# Copyright (c) 2005 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test harness for loading pages from packages contained in ZIP archives.
"""
__docformat__ = "reStructuredText"

import os
import sys

from zope.app.testing import functional


#### test setup ####

class Layer(functional.ZCMLLayer):

# This layer still can't be completely torn down due to the base
# class, but tries to avoid doing more damage than it has to.

def setUp(self):
self.__sys_path = sys.path[:]
here = os.path.dirname(__file__)
zipfile = os.path.join(here, "testfiles", "zippedview.zip")
sys.path.append(zipfile)
functional.ZCMLLayer.setUp(self)

def tearDown(self):
sys.path[:] = self.__sys_path
functional.ZCMLLayer.tearDown(self)


ZippedViewLayer = Layer(
os.path.join(os.path.dirname(__file__), "testfiles/zippedview.zcml"),
__name__, "ZippedViewLayer")

def test_suite():
suite = functional.FunctionalDocFileSuite("zipped.txt")
suite.layer = ZippedViewLayer
return suite
22 changes: 22 additions & 0 deletions browser/tests/testfiles/zippedview.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<configure
xmlns="http://namespaces.zope.org/zope"
package="zope.app.publisher.browser.tests"
>

<!-- This is a functional testing layer that adds the `sampleview`
package from a ZIP file to test that browser view defined by
products in zipped packages (such as eggs).
-->

<include package="zope.app"/>
<include package="sampleview"/>

<unauthenticatedPrincipal
id="zope.anybody"
title="Unauthenticated User" />

<securityPolicy
component="zope.security.simplepolicies.PermissiveSecurityPolicy"
/>

</configure>
Binary file added browser/tests/testfiles/zippedview.zip
Binary file not shown.
24 changes: 24 additions & 0 deletions browser/tests/testfiles/zipsource/sampleview/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
##############################################################################
#
# Copyright (c) 2005 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Sample view code.
This is used to test views extracted from ZIP files.
"""


class MyView(object):

def sometext(self):
return u"(This is some text.)"
15 changes: 15 additions & 0 deletions browser/tests/testfiles/zipsource/sampleview/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
>

<browser:page
name="sometext.html"
permission="zope.Public"
for="zope.interface.Interface"
template="myview.pt"
class=".MyView"
/>

</configure>
1 change: 1 addition & 0 deletions browser/tests/testfiles/zipsource/sampleview/myview.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div tal:content="view/sometext"/>
25 changes: 25 additions & 0 deletions browser/tests/zipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
==================================
Publishing views from ZIP archives
==================================

Python packages can be imported from ZIP files. Zope supports using
such packages as extension providers: ZCML can be loaded from them and
they can provide views based on templates or other data contained
inside the archive.

This document demonstrates this facility. The Python harness for this
test (test_zipped.py) has added a ZIP file containing a sample package
to sys.path, and loaded ZCML that loads a view from that package.

Since the ZCML has already been loaded, we can check that our view is
available::

>>> print http("""
... GET /@@sometext.html HTTP/1.1
... """)
HTTP/1.1 200 ...
<div>(This is some text.)</div>

This is the text provided by the view class in in the `sampleview`
package's __init__.py, wrapped in the <div> element from the myview.pt
template also contained in that package.
Loading

0 comments on commit ff05a1e

Please sign in to comment.