Author: nascheme
Date: 2003-11-10 13:24:27 -0500 (Mon, 10 Nov 2003)
New Revision: 23114
Modified:
trunk/quixote/http_request.py
Log:
Allow negative values to be passed to get_path().
Modified: trunk/quixote/http_request.py
===================================================================
--- trunk/quixote/http_request.py 2003-11-10 18:08:07 UTC (rev 23113)
+++ trunk/quixote/http_request.py 2003-11-10 18:24:27 UTC (rev 23114)
@@ -234,17 +234,29 @@
get_path(1) == "/bar/baz"
get_path(2) == "/bar"
- Raises ValueError if n is too big for the current path.
- """
+ If 'n' is negative, then components from the left of the path
+ are returned. Continuing the above example,
+ get_path(-1) = "/bar"
+ get_path(-2) = "/bar/baz"
+ get_path(-3) = "/bar/baz/"
+
+ Raises ValueError if absolute value of n is larger than the number of
+ path components."""
+
path_info = self.environ.get('PATH_INFO', '')
path = self.environ['SCRIPT_NAME'] + path_info
if n == 0:
return path
else:
path_comps = path.split('/')
- if n > len(path_comps)-1:
+ if abs(n) > len(path_comps)-1:
raise ValueError, "n=%d too big for path '%s'" % (n, path)
- return '/'.join(path_comps[:-n])
+ if n > 0:
+ return '/'.join(path_comps[:-n])
+ elif n < 0:
+ return '/'.join(path_comps[:-n+1])
+ else:
+ assert 0, "Unexpected value for n (%s)" % n
def get_url (self, n=0):
"""get_url(n : int = 0) -> string