diff --git a/yokadi/core/db.py b/yokadi/core/db.py index ea48bb83..c9429ecf 100644 --- a/yokadi/core/db.py +++ b/yokadi/core/db.py @@ -237,7 +237,12 @@ def add(session, name, command): session.add(alias) @staticmethod - def update(session, name, command): + def rename(session, name, newName): + alias = session.query(Alias).filter_by(name=name).one() + alias.name = newName + + @staticmethod + def setCommand(session, name, command): alias = session.query(Alias).filter_by(name=name).one() alias.command = command diff --git a/yokadi/tests/aliastestcase.py b/yokadi/tests/aliastestcase.py index cc598dcd..9237085f 100644 --- a/yokadi/tests/aliastestcase.py +++ b/yokadi/tests/aliastestcase.py @@ -55,8 +55,17 @@ def testAdd(self): def testEdit(self): self.cmd.do_a_add("l t_list") - tui.addInputAnswers("foo") + tui.addInputAnswers("ls") self.cmd.do_a_edit("l") + aliases = Alias.getAsDict(self.session) + self.assertEqual(aliases["ls"], "t_list") + + def testEditCommand(self): + self.cmd.do_a_add("l t_list") + + tui.addInputAnswers("foo") + self.cmd.do_a_edit_command("l") + aliases = Alias.getAsDict(self.session) self.assertEqual(aliases["l"], "foo") diff --git a/yokadi/ycli/aliascmd.py b/yokadi/ycli/aliascmd.py index 86b1cc02..35b2d2dc 100644 --- a/yokadi/ycli/aliascmd.py +++ b/yokadi/ycli/aliascmd.py @@ -13,6 +13,9 @@ class AliasCmd(object): def __init__(self): + self._updateAliasDict() + + def _updateAliasDict(self): self.aliases = db.Alias.getAsDict(db.getSession()) def do_a_list(self, line): @@ -37,24 +40,41 @@ def do_a_add(self, line): command = " ".join(tokens[1:]) session = db.getSession() - self.aliases.update({name: command}) db.Alias.add(session, name, command) session.commit() + self._updateAliasDict() def do_a_edit(self, line): - """Edit an alias. + """Edit the name of an alias. a_edit """ session = db.getSession() name = line if not name in self.aliases: raise YokadiException("There is no alias named {}".format(name)) + newName = tui.editLine(self.aliases[name]) + if newName in self.aliases: + raise YokadiException("There is already an alias named {}.".format(newName)) + + session = db.getSession() + db.Alias.rename(session, name, newName) + session.commit() + self._updateAliasDict() + + def do_a_edit_command(self, line): + """Edit the command of an alias. + a_edit_command """ + session = db.getSession() + name = line + if not name in self.aliases: + raise YokadiException("There is no alias named {}".format(name)) + command = tui.editLine(self.aliases[name]) session = db.getSession() - self.aliases.update({name: command}) - db.Alias.update(session, name, command) + db.Alias.setCommand(session, name, command) session.commit() + self._updateAliasDict() def do_a_remove(self, line): """Remove an alias"""