[Zope3-checkins] CVS: Zope3/src/zope/app/traversing - __init__.py:1.1.2.1 acquirenamespace.py:1.1.2.1 attritemnamespaces.py:1.1.2.1 configure.zcml:1.1.2.1 defaulttraversable.py:1.1.2.1 etcnamespace.py:1.1.2.1 exceptions.py:1.1.2.1 getresource.py:1.1.2.1 meta.zcml:1.1.2.1 modulenamespace.py:1.1.2.1 namespaces.py:1.1.2.1 objectname.py:1.1.2.1 parameterparsing.py:1.1.2.1 physicallocationadapters.py:1.1.2.1 presentationnamespaces.py:1.1.2.1 skinnamespace.py:1.1.2.1 traverser.py:1.1.2.1
Jim Fulton
jim@zope.com
Mon, 23 Dec 2002 14:32:35 -0500
Update of /cvs-repository/Zope3/src/zope/app/traversing
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/app/traversing
Added Files:
Tag: NameGeddon-branch
__init__.py acquirenamespace.py attritemnamespaces.py
configure.zcml defaulttraversable.py etcnamespace.py
exceptions.py getresource.py meta.zcml modulenamespace.py
namespaces.py objectname.py parameterparsing.py
physicallocationadapters.py presentationnamespaces.py
skinnamespace.py traverser.py
Log Message:
Initial renaming before debugging
=== Added File Zope3/src/zope/app/traversing/__init__.py ===
#
# This file is necessary to make this directory a package.
=== Added File Zope3/src/zope/app/traversing/acquirenamespace.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: acquirenamespace.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.app.traversing.namespaces import provideNamespaceHandler
from zope.app.traversing.exceptions import UnexpectedParameters
from zope.exceptions import NotFoundError
from zope.component import queryAdapter
from zope.proxy.context.context import ContextWrapper, getWrapperContext
from zope.app.interfaces.traversing.traversable import ITraversable
class ExcessiveWrapping(NotFoundError):
"""Too many levels of acquisition wrapping. We don't believe them."""
def acquire(name, parameters, pname, ob, request):
if parameters:
raise UnexpectedParameters(parameters)
i = 0
origOb = ob
while i < 200:
i += 1
traversable = queryAdapter(ob, ITraversable, None)
if traversable is not None:
try:
# XXX what do we do if the path gets bigger?
path = []
next = traversable.traverse(name, parameters, pname, path)
if path: continue
except NotFoundError:
pass
else:
return ContextWrapper(next, ob, name=name)
ob = getWrapperContext(ob)
if ob is None:
raise NotFoundError(origOb, pname)
raise ExcessiveWrapping(origOb, pname)
=== Added File Zope3/src/zope/app/traversing/attritemnamespaces.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: attritemnamespaces.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.app.traversing.namespaces import provideNamespaceHandler
from zope.app.traversing.exceptions import UnexpectedParameters
def attr(name, parameters, pname, ob, request):
if parameters:
raise UnexpectedParameters(parameters)
return getattr(ob, name)
def item(name, parameters, pname, ob, request):
if parameters:
raise UnexpectedParameters(parameters)
return ob[name]
=== Added File Zope3/src/zope/app/traversing/configure.zcml ===
<zopeConfigure
xmlns='http://namespaces.zope.org/zope'
xmlns:browser='http://namespaces.zope.org/browser'
>
<adapter factory="zope.app.traversing.traverser.Traverser"
provides="zope.app.interfaces.traversing.traverser.ITraverser"
/>
<!-- Ultimately, this should be registered only for IWrapper, but that
won't work like that just now.
for="zope.proxy.interfaces.context.IWrapper" /> -->
<adapter factory="zope.app.traversing.defaulttraversable.DefaultTraversable"
provides="zope.app.interfaces.traversing.traversable.ITraversable" />
<adapter
factory="zope.app.traversing.objectname.ObjectName"
provides="zope.app.traversing.objectname.IObjectName"
permission='Zope.Public'
/>
<adapter
factory="zope.app.traversing.objectname.SiteObjectName"
provides="zope.app.traversing.objectname.IObjectName"
for="zope.app.content.folder.IRootFolder"
permission='Zope.Public'
/>
<adapter
provides="zope.app.interfaces.traversing.physicallylocatable.IPhysicallyLocatable"
factory="zope.app.traversing.physicallocationadapters.WrapperPhysicallyLocatable"
/>
<adapter
provides="zope.app.interfaces.traversing.physicallylocatable.IPhysicallyLocatable"
for="zope.app.interfaces.traversing.containmentroot.IContainmentRoot"
factory="zope.app.traversing.physicallocationadapters.RootPhysicallyLocatable"
/>
<traversalNamespace name="etc" handler="zope.app.traversing.etcnamespace.etc" />
<traversalNamespace name="view" handler="zope.app.traversing.presentationnamespaces.view" />
<traversalNamespace name="resource"
handler="zope.app.traversing.presentationnamespaces.resource" />
<traversalNamespace name="attribute" handler="zope.app.traversing.attritemnamespaces.attr" />
<traversalNamespace name="item" handler="zope.app.traversing.attritemnamespaces.item" />
<traversalNamespace name="acquire" handler="zope.app.traversing.acquirenamespace.acquire" />
<traversalNamespace name="skin" handler="zope.app.traversing.skinnamespace.skin" />
<traversalNamespace name="module" handler="zope.app.traversing.modulenamespace.module" />
</zopeConfigure>
=== Added File Zope3/src/zope/app/traversing/defaulttraversable.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
from zope.app.interfaces.traversing.traversable import ITraversable
from zope.exceptions import NotFoundError
from zope.app.traversing.exceptions import UnexpectedParameters
_marker = object() # opaque marker that doesn't get security proxied
class DefaultTraversable:
"""Traverses objects via attribute and item lookup"""
__implements__ = ITraversable
def __init__(self, subject):
self._subject = subject
def traverse(self, name, parameters, pname, furtherPath):
if parameters:
raise UnexpectedParameters(parameters)
subject = self._subject
r = getattr(subject, name, _marker)
if r is not _marker:
return r
if hasattr(subject, '__getitem__'):
# Let exceptions propagate.
return self._subject[name]
else:
raise NotFoundError(self._subject, name)
=== Added File Zope3/src/zope/app/traversing/etcnamespace.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: etcnamespace.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.app.applicationcontrol.applicationcontrol \
import applicationController
from zope.app.traversing.namespaces import provideNamespaceHandler
from zope.app.traversing.exceptions import UnexpectedParameters
from zope.exceptions import NotFoundError
from zope.app.content.folder import RootFolder
def etc(name, parameters, pname, ob, request):
# XXX
# This is here now to allow us to get service managers from a
# separate namespace from the content. We add and etc
# namespace to allow us to handle misc objects. We'll apply
# YAGNI for now and hard code this. We'll want something more
# general later. We were thinking of just calling "get"
# methods, but this is probably too magic. In particular, we
# will treat returned objects as sub-objects wrt security and
# not all get methods may satisfy this assumption. It might be
# best to introduce some sort of etc registry.
if parameters:
raise UnexpectedParameters(parameters)
if name == 'ApplicationController' and ob.__class__ == RootFolder:
return applicationController
if name != 'Services':
raise NotFoundError(ob, pname, request)
method_name = "getServiceManager"
method = getattr(ob, method_name, None)
if method is None:
raise NotFoundError(ob, pname, request)
return method()
=== Added File Zope3/src/zope/app/traversing/exceptions.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: exceptions.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.exceptions import NotFoundError
class UnexpectedParameters(NotFoundError):
"""Unexpected namespace parameters were provided.
"""
=== Added File Zope3/src/zope/app/traversing/getresource.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""
$Id: getresource.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.component import getService
from zope.exceptions import NotFoundError
from zope.proxy.context.context import ContextWrapper
def getResource(ob, name, request):
resource = queryResource(ob, name, request)
if resource is None:
raise NotFoundError(ob, name)
return resource
def queryResource(ob, name, request, default=None):
resource_service = getService(ob, 'Resources')
resource = resource_service.queryResource(ob, name, request)
if resource is None:
return default
return ContextWrapper(resource, resource_service, name=name)
=== Added File Zope3/src/zope/app/traversing/meta.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
<directives namespace="http://namespaces.zope.org/zope">
<directive name="traversalNamespace" attributes="name handler"
handler="zope.app.traversing.namespaces.directive" />
</directives>
</zopeConfigure>
=== Added File Zope3/src/zope/app/traversing/modulenamespace.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: modulenamespace.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.app.interfaces.services.service import INameResolver
from zope.component import getServiceManager, getAdapter
from zope.component import queryDefaultViewName
from zope.interface import Interface
def module(name, parameters, pname, ob, request):
"""Used to traverse to a module (in dot notation)"""
servicemanager = getServiceManager(ob)
adapter = getAdapter(servicemanager, INameResolver)
if adapter is not None:
ob = adapter.resolve(name)
if queryDefaultViewName(ob, request) is None:
return Interface
return ob
=== Added File Zope3/src/zope/app/traversing/namespaces.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: namespaces.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.exceptions import NotFoundError
from zope.proxy.context.context import ContextWrapper, getWrapperObject
from zope.configuration.action import Action
_namespace_handlers = {}
def provideNamespaceHandler(ns, handler):
_namespace_handlers[ns] = handler
def directive(_context, name, handler):
handler = _context.resolve(handler)
return [Action(
discriminator=("traversalNamespace", name),
callable=provideNamespaceHandler,
args=(name, handler),
)]
def namespaceLookup(name, ns, qname, parameters, object, request=None):
"""Lookup a value from a namespace
name -- the original name
ns -- The namespace
qname -- The name without any parameters
The resulting object is returned in the context of the original.
This means that the caller should *not* wrap the result.
"""
handler = _namespace_handlers.get(ns)
if handler is None:
raise NotFoundError(name)
new = handler(qname, parameters, name, object, request)
if new is object:
# The handler had a side effect only and didn't look up a
# different object. We want to retain the side-effect name
# for things like URLs.
# But wait, there's more. The object may be wrapped. If the
# object is already wrapped and we return the object in the
# context of itself, the containment context will be wrong,
# because the inner wrapper will be the original object, so
# our added layer with the name we want to preserve will be
# ignored when searching containment.
# For this reason, we'll remove a layer of wrapping from new
# before we put it in context.
new = getWrapperObject(new)
new = ContextWrapper(new, object, name='.', side_effect_name=name)
else:
new = ContextWrapper(new, object, name=name)
return new
=== Added File Zope3/src/zope/app/traversing/objectname.py ===
##############################################################################
#
# 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.0 (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
#
##############################################################################
"""
Revision information:
$Id: objectname.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.proxy.context.context import getInnerWrapperData
from zope.interface import Interface
class IObjectName(Interface):
def __str__():
"""Get a human-readable string representation
"""
def __repr__():
"""Get a string representation
"""
def __call__():
"""Get a string representation
"""
class ObjectName(object):
__implements__ = IObjectName
def __init__(self, context):
self.context = context
def __str__(self):
dict = getInnerWrapperData(self.context)
name = dict and dict.get('name') or None
if name is None:
raise TypeError, \
'Not enough context information to get an object name'
return name
__call__ = __str__
class SiteObjectName(object):
__implements__ = IObjectName
def __init__(self, context):
pass
def __str__(self):
return ''
__call__ = __str__
=== Added File Zope3/src/zope/app/traversing/parameterparsing.py ===
##############################################################################
#
# 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.0 (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
#
##############################################################################
"""
Revision information:
$Id: parameterparsing.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
import re
namespace_pattern = re.compile('[+][+]([a-zA-Z0-9_]+)[+][+]')
def parameterizedNameParse(name):
"""Parse a name with parameters, including namespace parameters.
Return:
- namespace, or None if there isn't one.
- unparameterized name.
- sequence of parameters, as name-value pairs.
"""
ns = ''
if name.startswith('@@'):
ns = 'view'
name = name[2:]
else:
match = namespace_pattern.match(name)
if match:
prefix, ns = match.group(0, 1)
name = name[len(prefix):]
return ns, name, ()
=== Added File Zope3/src/zope/app/traversing/physicallocationadapters.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""XXX short summary goes here.
XXX longer description goes here.
$Id: physicallocationadapters.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
__metaclass__ = type
from zope.app.interfaces.traversing.physicallylocatable import IPhysicallyLocatable
from zope.app.interfaces.traversing.containmentroot import IContainmentRoot
from zope.component import getAdapter
from zope.proxy.context.context import getInnerWrapperData, getWrapperContainer
class WrapperPhysicallyLocatable:
__doc__ = IPhysicallyLocatable.__doc__
__implements__ = IPhysicallyLocatable
def __init__(self, context):
self.context = context
def getPhysicalRoot(self):
"See Zope.App.Traversing.IPhysicallyLocatable.IPhysicallyLocatable"
container = getWrapperContainer(self.context)
if container is None:
raise TypeError("Not enough context to determine location root")
return getAdapter(container, IPhysicallyLocatable).getPhysicalRoot()
def getPhysicalPath(self):
"See Zope.App.Traversing.IPhysicallyLocatable.IPhysicallyLocatable"
context = self.context
container = getWrapperContainer(context)
if container is None:
raise TypeError("Not enough context to determine location")
name = getInnerWrapperData(context)['name']
container = getAdapter(container, IPhysicallyLocatable)
container_path = container.getPhysicalPath()
if name == '.':
# skip
return container_path
return container_path + (name, )
class RootPhysicallyLocatable:
__doc__ = IPhysicallyLocatable.__doc__
__implements__ = IPhysicallyLocatable
__used_for__ = IContainmentRoot
def __init__(self, context):
self.context = context
def getPhysicalPath(self):
"See Zope.App.Traversing.IPhysicallyLocatable.IPhysicallyLocatable"
return ('', )
def getPhysicalRoot(self):
"See Zope.App.Traversing.IPhysicallyLocatable.IPhysicallyLocatable"
return self.context
=== Added File Zope3/src/zope/app/traversing/presentationnamespaces.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""
$Id: presentationnamespaces.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.component import getView
from zope.app.traversing.namespaces import provideNamespaceHandler
from zope.app.traversing.exceptions import UnexpectedParameters
from zope.exceptions import NotFoundError
from zope.proxy.context.context import ContextWrapper
from zope.app.traversing.getresource import queryResource
class NoRequest(NotFoundError):
"""Atempt to access a presentation component outside of a request context
"""
def view(name, parameters, pname, ob, request):
if parameters:
raise UnexpectedParameters(parameters)
if not request:
raise NoRequest(pname)
return getView(ob, name, request)
def resource(name, parameters, pname, ob, request):
if parameters:
raise UnexpectedParameters(parameters)
if not request:
raise NoRequest(pname)
resource = queryResource(ob, name, request)
if resource is None:
raise NotFoundError(ob, pname)
return resource
=== Added File Zope3/src/zope/app/traversing/skinnamespace.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
$Id: skinnamespace.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.app.traversing.namespaces import provideNamespaceHandler
from zope.app.traversing.exceptions import UnexpectedParameters
from zope.exceptions import NotFoundError
class NoRequest(NotFoundError):
"""Atempt to access a presentation component outside of a request context
"""
def skin(name, parameters, pname, ob, request):
if parameters:
raise UnexpectedParameters(parameters)
if not request:
raise NoRequest(pname)
request.setViewSkin(name)
return ob
=== Added File Zope3/src/zope/app/traversing/traverser.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""Default implementation of ITraverser.
$Id: traverser.py,v 1.1.2.1 2002/12/23 19:32:33 jim Exp $
"""
from zope.component import getAdapter
from zope.proxy.context.context import getWrapperContainer
from zope.proxy.context.context import ContextWrapper
from zope.component import queryAdapter
from zope.exceptions import NotFoundError
from zope.app.traversing.namespaces import namespaceLookup
from zope.app.traversing.parameterparsing import parameterizedNameParse
from zope.app.interfaces.traversing.physicallylocatable import IPhysicallyLocatable
from zope.app.interfaces.traversing.traverser import ITraverser
from zope.app.interfaces.traversing.traversable import ITraversable
from types import StringTypes
from __future__ import generators
# A chain generator; let's us walk the wrapper chain down to the root
def WrapperChain(w):
while w is not None:
yield w
w = getWrapperContainer(w)
_marker = object()
class Traverser:
"""Provide traverse features"""
__implements__ = ITraverser
# This adapter can be used for any object.
def __init__(self, wrapper):
self.context = wrapper
def traverse(self, path, default=_marker, request=None):
if not path:
return self.context
if isinstance(path, StringTypes):
path = path.split('/')
if len(path) > 1 and not path[-1]:
# Remove trailing slash
path.pop()
else:
path = list(path)
path.reverse()
pop = path.pop
curr = self.context
if not path[-1]:
# Start at the root
pop()
curr = getAdapter(self.context, IPhysicallyLocatable
).getPhysicalRoot()
try:
while path:
name = pop()
if name == '.':
continue
if name == '..':
# XXX This doesn't look right. Why fall back to curr?
curr = getWrapperContainer(curr) or curr
continue
if name and name[:1] in '@+':
ns, nm, parms = parameterizedNameParse(name)
if ns:
curr = namespaceLookup(name, ns, nm, parms,
curr, request)
continue
else:
parms = ()
nm = name
traversable = queryAdapter(curr, ITraversable, None)
if traversable is None:
raise NotFoundError(
'No traversable adapter found', curr)
next = traversable.traverse(nm, parms, name, path)
curr = ContextWrapper(next, curr, name=name)
return curr
except NotFoundError:
if default == _marker:
raise
return default