[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Traversing - ObjectName.py:1.1 __init__.py:1.3 traversing.zcml:1.3
Steve Alexander
steve@cat-box.net
Sat, 15 Jun 2002 16:38:48 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Traversing
In directory cvs.zope.org:/tmp/cvs-serv22871/lib/python/Zope/App/Traversing
Modified Files:
__init__.py traversing.zcml
Added Files:
ObjectName.py
Log Message:
Added a set of convenience functions that you can import from
Zope.App.Traversing.
Fixed a few typos in the ContextWrapper docstrings, and also added
an isWrapper method to Zope/Proxy/IContextWrapper, mainly so that
I can raise nice errors if the convenience functions are passed
an unwrapped object, rather than returning objs or Nones or []s and
having programs fail at tenuously related other places.
=== Added File Zope3/lib/python/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 2002/06/15 20:38:17 stevea Exp $
"""
from Zope.Proxy.ContextWrapper import getWrapperData
from 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 = getWrapperData(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__
=== Zope3/lib/python/Zope/App/Traversing/__init__.py 1.2 => 1.3 ===
Traversing the object tree.
"""
+# being careful not to pollute the namespace unnecessarily...
+from Zope.ComponentArchitecture import getAdapter as _getAdapter
+from ObjectName import IObjectName as _IObjectName
+from ITraverser import ITraverser as _ITraverser
+from Traverser import WrapperChain as _WrapperChain
+from Zope.Proxy.ContextWrapper import getWrapperContext as _getWrapperContext
+from Zope.Proxy.ContextWrapper import isWrapper as _isWrapper
+_marker = object()
+
+# XXX: this probably shouldn't have "request" in its signature, nor
+# in the arguments of the call to traverser.traverse
+def traverse(place, path, default=_marker, request=None):
+ """Traverse 'path' relative to 'place'
+ Raises NotFoundError if path cannot be found
+ Raises TypeError if place is not context wrapped
+ """
+ if not _isWrapper(place):
+ raise TypeError, "Not enough context information to traverse"
+ traverser = _getAdapter(place, _ITraverser)
+ if default is _marker:
+ return traverser.traverse(path, request=request)
+ else:
+ return traverser.traverse(path, default=default, request=request)
+
+def objectName(obj):
+ """Get the name an object was traversed via
+
+ Raises TypeError if the object is not context-wrapped
+ """
+ return _getAdapter(obj, _IObjectName)()
+
+def getParent(obj):
+ """Returns the container the object was traversed via.
+
+ Raises TypeError if the given object is not context wrapped
+ """
+ if not _isWrapper(obj):
+ raise TypeError, "Not enough context information to traverse"
+ return _getWrapperContext(obj)
+
+def getParents(obj):
+ """Returns a list starting with the given object's parent followed by
+ each of its parents.
+
+ Raises TypeError if the given object is not context wrapped
+ """
+ if not _isWrapper(obj):
+ raise TypeError, "Not enough context information to traverse"
+ iterator = _WrapperChain(obj)
+ iterator.next() # send head of chain (current object) to /dev/null
+ return [p for p in iterator]
+
+def getPhysicalPath(obj):
+ """Returns a tuple of names representing the physical path to the
+ given object.
+
+ Raises TypeError if the given object is not context wrapped
+ """
+ if not _isWrapper(obj):
+ raise TypeError, "Not enough context information to traverse"
+
+ return _getAdapter(obj, _ITraverser).getPhysicalPath()
+
+def getPhysicalRoot(obj):
+ """Returns the root of the traversal for the given object.
+
+ Raises TypeError if the given object is not context wrapped
+ """
+ if not _isWrapper(obj):
+ raise TypeError, "Not enough context information to traverse"
+
+ return _getAdapter(obj, _ITraverser).getPhysicalRoot()
=== Zope3/lib/python/Zope/App/Traversing/traversing.zcml 1.2 => 1.3 ===
provides="Zope.App.Traversing.ITraversable." />
+<adapter
+ factory=".ObjectName."
+ provides=".ObjectName.IObjectName"
+ permission='Zope.Public'
+/>
+
+<adapter
+ factory=".ObjectName.SiteObjectName"
+ provides=".ObjectName.IObjectName"
+ for="Zope.App.OFS.Content.Folder.RootFolder.IRootFolder"
+ permission='Zope.Public'
+/>
+
</zopeConfigure>