diff --git twisted/python/test/test_urlpath.py twisted/python/test/test_urlpath.py index 4919cdc..886fd37 100644 --- twisted/python/test/test_urlpath.py +++ twisted/python/test/test_urlpath.py @@ -42,3 +42,21 @@ class URLPathTestCase(unittest.TestCase): self.assertEqual(str(self.path.here()), 'http://example.com/foo/') self.assertEqual(str(self.path.child('').here()), 'http://example.com/foo/bar/') + def testMe(self): + """ + Should return the current path without the query or fragment or goofy + slash changes (I'm looking at you, C{here}) + """ + self.assertEqual(str(self.path.me()), 'http://example.com/foo/bar') + + def testDirname(self): + """ + Since, due to backward compatibility, C{parent} can't be the inverse of + C{child}, C{dirname} should be the inverse of C{child}. + """ + self.assertEqual(str(self.path.child('foo').dirname()), str(self.path.me())) + self.assertEqual(str(self.path.dirname().dirname().dirname().dirname()), + 'http://example.com/') + self.assertEqual(str(urlpath.URLPath.fromString('http://example.com/foo/').dirname()), + 'http://example.com/foo') + diff --git twisted/python/urlpath.py twisted/python/urlpath.py index 1c15f09..651d94b 100644 --- twisted/python/urlpath.py +++ twisted/python/urlpath.py @@ -78,6 +78,31 @@ class URLPath: l[-1] = '' return self._pathMod(l, keepQuery) + def me(self, keepQuery=0): + """ + Get me. + + C{URLPath('http://example.com/foo/bar?hey=ho').me()} is equivalent + to C{URLPath('http://example.com/foo/bar')}. + + @param keepQuery: If C{True} then retain the query portion. + + @return: A L{URLPath} identical to me (but without the query portion + depending on C{keepQuery}) + """ + return self._pathMod(self.pathList(), keepQuery) + + def dirname(self, keepQuery=0): + """ + Inverse of L{child}. + + The dirname of C{http://example.com/foo/bar} is + C{http://example.com/foo}. + """ + l = self.pathList() + del l[-1] + return self._pathMod(l, keepQuery) + def here(self, keepQuery=0): l = self.pathList() if l[-1] != '':