durusmail: quixote-users: [PATCH] request.get_path works with negative arg
[PATCH] request.get_path works with negative arg
[PATCH] request.get_path works with negative arg
[PATCH] request.get_path works with negative arg
Mark Bucciarelli
2003-11-08
I wanted to be able to get the path component by counting out from the server.
The enclosed patch makes request.get_path() work with negative args.

For example, if the path = '/bar/baz/', then request.get_path(-1) returns
'/bar'.

--- http_request.py.orig        2003-11-06 15:00:11.000000000 -0500
+++ http_request.py     2003-11-08 14:50:55.000000000 -0500
@@ -230,17 +230,29 @@
           get_path(1) == "/bar/baz"
           get_path(2) == "/bar"

-        Raises ValueError if n is too big for the current path.
-        """
+        If a negative value is passed in, the path components are counted
from
+        the server out.  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


Also a request: could you modify the footer the mailing list manager appends
to include the url to the issue tracker Neil set up for Quixote?  Things have
a way of getting buried in mailing list archives ...

--
Mark Bucciarelli, www.hubcapconsulting.com
  As we enjoy great advantages from inventions of others, we should
be glad of an opportunity to serve others by any invention of ours;
and this we should do freely and generously.  -- Benjamin Franklin

reply