add group renaming

This commit is contained in:
Damien Elmes 2011-10-22 20:41:33 +09:00
parent 3068e79b42
commit b9dc5764a3
2 changed files with 39 additions and 0 deletions

View file

@ -169,6 +169,23 @@ class GroupManager(object):
# mark registry changed, but don't bump mod time # mark registry changed, but don't bump mod time
self.save() self.save()
def rename(self, g, newName):
"Rename group prefix to NAME if not exists. Updates children."
# make sure target node doesn't already exist
if newName in self.allNames():
raise Exception("Group exists")
# rename children
for grp in self.all():
if grp['name'].startswith(g['name'] + "::"):
grp['name'] = grp['name'].replace(g['name']+ "::",
newName + "::")
self.save(grp)
# then update provided group
g['name'] = newName
self.save(g)
# finally, ensure we have parents
self._ensureParents(newName)
def _ensureParents(self, name): def _ensureParents(self, name):
path = name.split("::") path = name.split("::")
s = "" s = ""

View file

@ -63,3 +63,25 @@ def test_remove():
assert deck.cardCount() == 0 assert deck.cardCount() == 0
assert deck.factCount() == 0 assert deck.factCount() == 0
def test_rename():
d = getEmptyDeck()
id = d.groups.id("hello::world")
# should be able to rename into a completely different branch, creating
# parents as necessary
d.groups.rename(d.groups.get(id), "foo::bar")
assert "foo" in d.groups.allNames()
assert "foo::bar" in d.groups.allNames()
assert "hello::world" not in d.groups.allNames()
# create another group
id = d.groups.id("tmp")
# we can't rename it if it conflicts
assertException(
Exception, lambda: d.groups.rename(d.groups.get(id), "foo"))
# when renaming, the children should be renamed too
d.groups.id("one::two::three")
id = d.groups.id("one")
d.groups.rename(d.groups.get(id), "yo")
for n in "yo", "yo::two", "yo::two::three":
assert n in d.groups.allNames()