Commit 853bc49d by rfkelly0

include root dir in output of recursepath()

parent 1a46e329
...@@ -74,17 +74,16 @@ def iteratepath(path, numsplits=None): ...@@ -74,17 +74,16 @@ def iteratepath(path, numsplits=None):
def recursepath(path, reverse=False): def recursepath(path, reverse=False):
"""Iterate from root to path, returning intermediate paths""" """Iterate from root to path, returning intermediate paths"""
paths = list(iteratepath(path))
if reverse: if reverse:
paths = [] paths = []
while path.lstrip('/'): path = abspath(path).rstrip("/")
while path:
paths.append(path) paths.append(path)
path = dirname(path) path = dirname(path).rstrip("/")
return paths return paths + ["/"]
else: else:
return [u'/'.join(paths[:i+1]) for i in xrange(len(paths))] paths = [""] + list(iteratepath(path))
return ["/"] + [u'/'.join(paths[:i+1]) for i in xrange(1,len(paths))]
def abspath(path): def abspath(path):
"""Convert the given path to an absolute path. """Convert the given path to an absolute path.
......
...@@ -94,6 +94,14 @@ class TestPathFunctions(unittest.TestCase): ...@@ -94,6 +94,14 @@ class TestPathFunctions(unittest.TestCase):
for path, result in tests: for path, result in tests:
self.assertEqual(fs.pathsplit(path), result) self.assertEqual(fs.pathsplit(path), result)
def test_recursepath(self):
self.assertEquals(recursepath("/"),["/"])
self.assertEquals(recursepath("hello"),["/","/hello"])
self.assertEquals(recursepath("/hello/world/"),["/","/hello","/hello/world"])
self.assertEquals(recursepath("/hello/world/",reverse=True),["/hello/world","/hello","/"])
self.assertEquals(recursepath("hello",reverse=True),["/hello","/"])
self.assertEquals(recursepath("",reverse=True),["/"])
class Test_PathMap(unittest.TestCase): class Test_PathMap(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment