Skip to content

Commit

Permalink
Rename a_edit to a_edit_command, add a_edit
Browse files Browse the repository at this point in the history
  • Loading branch information
agateau committed May 14, 2016
1 parent aecefde commit 1ce84fb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
7 changes: 6 additions & 1 deletion yokadi/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion yokadi/tests/aliastestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
28 changes: 24 additions & 4 deletions yokadi/ycli/aliascmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 <alias name>"""
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 <alias name>"""
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"""
Expand Down

0 comments on commit 1ce84fb

Please sign in to comment.