Commit aab21a9f by willmcgugan

Enhancements to objecttree, and added objecttree test suite

parent 82f7a054
...@@ -12,6 +12,12 @@ class ObjectTree(object): ...@@ -12,6 +12,12 @@ class ObjectTree(object):
def __init__(self): def __init__(self):
self.root = _ObjectDict() self.root = _ObjectDict()
def _split(self, path):
if '/' not in path:
return "", path
else:
return path.rsplit('/', 1)
def _splitpath(self, path): def _splitpath(self, path):
return [p for p in path.split('/') if p] return [p for p in path.split('/') if p]
...@@ -30,7 +36,7 @@ class ObjectTree(object): ...@@ -30,7 +36,7 @@ class ObjectTree(object):
if not path: if not path:
raise IndexError("No path supplied") raise IndexError("No path supplied")
current = self.root current = self.root
path, name = path.rsplit('/', 1) path, name = self._split(path)
for path_component in self._splitpath(path): for path_component in self._splitpath(path):
node = current.get(path_component, None) node = current.get(path_component, None)
if type(node) is not _ObjectDict: if type(node) is not _ObjectDict:
...@@ -48,7 +54,7 @@ class ObjectTree(object): ...@@ -48,7 +54,7 @@ class ObjectTree(object):
return node return node
def __delitem__(self, path): def __delitem__(self, path):
path, name = path.rsplit('/', 1) path, name = self._split(path)
node = self._locate(path) node = self._locate(path)
if node is None or type(node) is not _ObjectDict: if node is None or type(node) is not _ObjectDict:
raise IndexError("Path does not exist") raise IndexError("Path does not exist")
...@@ -98,6 +104,7 @@ class ObjectTree(object): ...@@ -98,6 +104,7 @@ class ObjectTree(object):
def iteritems(self): def iteritems(self):
return self.root.iteritems() return self.root.iteritems()
if __name__ == "__main__": if __name__ == "__main__":
ot = ObjectTree() ot = ObjectTree()
......
...@@ -101,6 +101,45 @@ class TestHelpers(unittest.TestCase): ...@@ -101,6 +101,45 @@ class TestHelpers(unittest.TestCase):
self.assertEqual(fs.pathsplit(path), result) self.assertEqual(fs.pathsplit(path), result)
import objecttree
class TestObjectTree(unittest.TestCase):
def test_getset(self):
"""objecttree.ObjectTree tests"""
ot = objecttree.ObjectTree()
ot['foo'] = "bar"
self.assertEqual(ot['foo'], 'bar')
ot = objecttree.ObjectTree()
ot['foo/bar'] = "baz"
self.assertEqual(ot['foo'], {'bar':'baz'})
self.assertEqual(ot['foo/bar'], 'baz')
del ot['foo/bar']
self.assertEqual(ot['foo'], {})
ot = objecttree.ObjectTree()
ot['a/b/c'] = "A"
ot['a/b/d'] = "B"
ot['a/b/e'] = "C"
ot['a/b/f'] = "D"
self.assertEqual(sorted(ot['a/b'].values()), ['A', 'B', 'C', 'D'])
self.assert_(ot.get('a/b/x', None) is None)
self.assert_('a/b/c' in ot)
self.assert_('a/b/x' not in ot)
self.assert_(ot.isobject('a/b/c'))
self.assert_(ot.isobject('a/b/d'))
self.assert_(not ot.isobject('a/b'))
left, object, right = ot.partialget('a/b/e/f/g')
self.assertEqual(left, "a/b/e")
self.assertEqual(object, "C")
self.assertEqual(right, "f/g")
import tempfile import tempfile
import osfs import osfs
import os import os
......
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