[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Traversing - __init__.py:1.6
Steve Alexander
steve@cat-box.net
Tue, 25 Jun 2002 08:38:13 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Traversing
In directory cvs.zope.org:/tmp/cvs-serv21908/lib/python/Zope/App/Traversing
Modified Files:
__init__.py
Log Message:
Added convenience functions to Zope.App.Traversing to get a canonical
location from a tuple of strings or a string.
A canonical location is a slash-delimited unicode string, or a
tuple of unicode strings.
Location strings must not end in a slash (except the root), nor contain
more than one consecutive slashes, nor be of zero length.
Location tuples must not end with an empty string (except the root), nor
contain more than one consecutive empty string, nor be of zero length.
The root location is given as u'/' or (u'',)
=== Zope3/lib/python/Zope/App/Traversing/__init__.py 1.5 => 1.6 ===
from Zope.Proxy.ContextWrapper import getWrapperContext as _getWrapperContext
from Zope.Proxy.ContextWrapper import isWrapper as _isWrapper
+from types import StringTypes
_marker = object()
# XXX: this probably shouldn't have "request" in its signature, nor
@@ -111,3 +112,50 @@
return _getAdapter(obj, _ITraverser).getPhysicalRoot()
+def locationAsTuple(location):
+ """Given a location as a unicode or ascii string or as a tuple of
+ unicode or ascii strings, returns the location as a tuple of
+ unicode strings.
+
+ Raises a ValueError if a poorly formed location is given.
+ """
+ if not location:
+ raise ValueError, "location must be non-empty."
+ if isinstance(location, tuple):
+ t = tuple(map(unicode, location))
+ elif isinstance(location, StringTypes):
+ if location == u'/': # matches '/' or u'/'
+ return (u'',)
+ t = tuple(location.split(u'/'))
+ else:
+ raise ValueError, \
+ "location %s must be a string or a tuple of strings." % (location,)
+
+ if t[-1] == u'': # matches '' or u''
+ raise ValueError, \
+ "location tuple %s must not end with empty string." % (t,)
+ # don't usually need this, so just an assertion rather than a value error
+ assert '' not in t[1:]
+ return t
+
+def locationAsUnicode(location):
+ """Given a location as a unicode or ascii string or as a tuple of
+ unicode or ascii strings, returns the location as a slash-separated
+ unicode string.
+
+ Raises ValueError if a poorly formed location is given.
+ """
+ if not location:
+ raise ValueError, "location must be non-empty."
+ if isinstance(location, tuple):
+ u = u'/'.join(location)
+ elif isinstance(location, StringTypes):
+ u = unicode(location)
+ else:
+ raise ValueError, \
+ "location %s must be a string or a tuple of strings." % (location,)
+ if u != '/' and u[-1] == u'/':
+ raise ValueError, "location %s must not end with a slash." % u
+ # don't usually need this, so just an assertion rather than a value error
+ assert u.find(u'//') == -1
+ return u