Skip to content

Commit

Permalink
Fix up zope.app for new location of zope.traversing.
Browse files Browse the repository at this point in the history
(also sneaked in some minor cosmetics regarding zapi/ztapi)
  • Loading branch information
philikon committed Apr 5, 2006
1 parent c521b23 commit b8b7c96
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 15 deletions.
4 changes: 2 additions & 2 deletions configure.zcml
Expand Up @@ -6,14 +6,14 @@

<adapter
for="*"
provides="zope.app.traversing.interfaces.IPathAdapter"
provides="zope.traversing.interfaces.IPathAdapter"
factory=".talesapi.ZopeTalesAPI"
name="zope"
/>

<adapter
for="*"
provides="zope.app.traversing.interfaces.IPathAdapter"
provides="zope.traversing.interfaces.IPathAdapter"
factory=".urlquote.URLQuote"
name="url"/>

Expand Down
23 changes: 11 additions & 12 deletions engine.py
Expand Up @@ -21,24 +21,23 @@

import sys

from zope import component
from zope.interface import implements

from zope.tales.expressions import PathExpr, StringExpr, NotExpr, DeferExpr
from zope.tales.expressions import SimpleModuleImporter
from zope.tales.pythonexpr import PythonExpr
from zope.tales.tales import ExpressionEngine, Context

from zope.component.interfaces import ComponentLookupError
from zope.app.traversing.interfaces import TraversalError
from zope.traversing.interfaces import IPathAdapter, ITraversable
from zope.traversing.interfaces import TraversalError
from zope.traversing.adapters import Traverser, traversePathElement
from zope.security.untrustedpython import rcompile
from zope.security.proxy import ProxyFactory
from zope.security.untrustedpython.builtins import SafeBuiltins
from zope.i18n import translate

from zope.app import zapi
from zope.tales.expressions import PathExpr, StringExpr, NotExpr, DeferExpr
from zope.tales.expressions import SimpleModuleImporter
from zope.tales.pythonexpr import PythonExpr
from zope.tales.tales import ExpressionEngine, Context

from zope.app.i18n import ZopeMessageFactory as _
from zope.app.traversing.adapters import Traverser, traversePathElement
from zope.app.traversing.interfaces import IPathAdapter, ITraversable

class InlineCodeError(Exception):
pass
Expand Down Expand Up @@ -126,7 +125,7 @@ def evaluateCode(self, lang, code):
# TODO This is only needed when self.evaluateInlineCode is true,
# so should only be needed for zope.app.pythonpage.
from zope.app.interpreter.interfaces import IInterpreter
interpreter = zapi.queryUtility(IInterpreter, lang)
interpreter = component.queryUtility(IInterpreter, lang)
if interpreter is None:
error = _('No interpreter named "${lang_name}" was found.',
mapping={'lang_name': lang})
Expand Down Expand Up @@ -196,7 +195,7 @@ def __getitem__(self, name):
if namespace is None:
def namespace(object):
try:
return zapi.getAdapter(object, IPathAdapter, name)
return component.getAdapter(object, IPathAdapter, name)
except ComponentLookupError:
raise KeyError(name)

Expand Down
63 changes: 63 additions & 0 deletions tests/test_directives.py
@@ -0,0 +1,63 @@
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Tests to check talesapi zcml configuration
$Id$
"""
import unittest
from cStringIO import StringIO

from zope.configuration.xmlconfig import xmlconfig, XMLConfig
from zope.interface import Interface, implements
from zope.traversing.interfaces import ITraversable

import zope.app.pagetemplate
from zope.app.testing import ztapi
from zope.app.testing.placelesssetup import PlacelessSetup
from zope.app.pagetemplate.engine import Engine

template = """<configure
xmlns='http://namespaces.zope.org/zope'
xmlns:tales='http://namespaces.zope.org/tales'>
%s
</configure>"""


class Handler(object):
pass

class Test(PlacelessSetup, unittest.TestCase):

def setUp(self):
super(Test, self).setUp()
XMLConfig('meta.zcml', zope.app.pagetemplate)()

def testExpressionType(self):
xmlconfig(StringIO(template % (
"""
<tales:expressiontype
name="test"
handler="zope.app.pagetemplate.tests.test_directives.Handler"
/>
"""
)))
self.assert_("test" in Engine.getTypes())
self.assert_(Handler is Engine.getTypes()['test'])

def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
2 changes: 1 addition & 1 deletion tests/test_talesapi.py
Expand Up @@ -19,9 +19,9 @@
from zope.testing.doctestunit import DocTestSuite
from zope.interface import implements
from zope.size.interfaces import ISized
from zope.traversing.interfaces import IPhysicallyLocatable

from zope.app.dublincore.interfaces import IZopeDublinCore
from zope.app.traversing.interfaces import IPhysicallyLocatable
from zope.app.pagetemplate.talesapi import ZopeTalesAPI

class TestObject(object):
Expand Down
91 changes: 91 additions & 0 deletions urlquote.py
@@ -0,0 +1,91 @@
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""URL quoting for ZPT
$Id$
"""
__docformat__ = 'restructuredtext'

import urllib
from zope.interface import implements
from zope.traversing.interfaces import IPathAdapter

class URLQuote(object):
r"""An adapter for URL quoting.
It quotes unicode strings according to the recommendation in RFC 2718.
Before the unicode string gets quoted, it gets encoded with UTF-8.
>>> quoter = URLQuote(u'Roki\u0161kis')
>>> quoter.quote()
'Roki%C5%A1kis'
>>> quoter.quote_plus()
'Roki%C5%A1kis'
And when unquoting, it assumes the unquoted string is encoded with
UTF-8, and tries to convert it to unicode.
>>> quoter = URLQuote('Roki%C5%A1kis')
>>> quoter.unquote()
u'Roki\u0161kis'
>>> quoter.unquote_plus()
u'Roki\u0161kis'
If the unquoted string can't be converted to unicode, the unquoted
string is returned.
>>> quoter = URLQuote('S%F6derk%F6ping')
>>> quoter.unquote()
'S\xf6derk\xf6ping'
>>> quoter.unquote_plus()
'S\xf6derk\xf6ping'
"""

__used_for__ = basestring
implements(IPathAdapter)

def __init__(self, context):
if not isinstance(context, basestring):
context = str(context)
elif isinstance(context, unicode):
context = context.encode('utf-8')
self.context = context

def quote(self):
"""Return the object's URL quote representation."""
return urllib.quote(self.context)

def quote_plus(self):
"""Return the object's URL quote_plus representation."""
return urllib.quote_plus(self.context)

def unquote(self):
"""Return the object's URL unquote representation."""
unquoted = urllib.unquote(self.context)
try:
return unicode(unquoted, 'utf-8')
except UnicodeDecodeError:
return unquoted

def unquote_plus(self):
"""Return the object's URL unquote_plus representation."""
unquoted = urllib.unquote_plus(self.context)
try:
return unicode(unquoted, 'utf-8')
except UnicodeDecodeError:
return unquoted

0 comments on commit b8b7c96

Please sign in to comment.