Commit dc6fc88e authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Apply changes to history_cli

parent 436bd910
......@@ -35,7 +35,7 @@ from traceback import print_stack, print_exc
import logging
from kamaki.clients import ClientError
from kamaki.cli.errors import CLIError, raiseCLIError
from kamaki.cli.errors import CLIError, raiseCLIError, CLISyntaxError
from kamaki.cli import _debug, kloger
sendlog = logging.getLogger('clients.send')
......@@ -103,3 +103,26 @@ class astakos(object):
self._raise = foo
return r
return _raise
class history(object):
@classmethod
def init(this, foo):
@generic.all
def _raise(self, *args, **kwargs):
r = foo(self, *args, **kwargs)
if not hasattr(self, 'history'):
raise CLIError('Failed to load history', importance=2)
return r
return _raise
@classmethod
def _get_cmd_ids(this, foo):
@generic.all
def _raise(self, cmd_ids, *args, **kwargs):
if not cmd_ids:
raise CLISyntaxError('Usage: <id1|id1-id2> [id3|id3-id4] ...',
details=self.__doc__.split('\n'))
return foo(self, cmd_ids, *args, **kwargs)
return _raise
......@@ -38,9 +38,9 @@ from kamaki.cli.argument import IntArgument, ValueArgument
from kamaki.cli.argument import ArgumentParseManager
from kamaki.cli.history import History
from kamaki.cli import command
from kamaki.cli.commands import _command_init
from kamaki.cli.commands import _command_init, errors
from kamaki.cli import exec_cmd, print_error_message
from kamaki.cli.errors import CLIError, CLISyntaxError, raiseCLIError
from kamaki.cli.errors import CLIError, raiseCLIError
from kamaki.cli.utils import split_input
from kamaki.clients import ClientError
......@@ -72,9 +72,15 @@ def _get_num_list(num_str):
class _init_history(_command_init):
def main(self):
@errors.history.init
def _run(self):
self.history = History(self.config.get('history', 'file'))
def main(self):
print('DU FUCK')
self._run()
@command(history_cmds)
class history_show(_init_history):
......@@ -98,8 +104,8 @@ class history_show(_init_history):
match=ValueArgument('show lines that match given terms', '--match')
)
def main(self, *cmd_ids):
super(self.__class__, self).main()
@errors.generic.all
def _run(self, *cmd_ids):
ret = self.history.get(match_terms=self['match'], limit=self['limit'])
if not cmd_ids:
......@@ -116,18 +122,25 @@ class history_show(_init_history):
if cur_id:
print(ret[cur_id - (1 if cur_id > 0 else 0)][:-1])
except IndexError as e2:
print('LA %s LA' % self.__doc__)
raiseCLIError(e2, 'Command id out of 1-%s range' % len(ret))
def main(self, *cmd_ids):
super(self.__class__, self)._run()
self._run(*cmd_ids)
@command(history_cmds)
class history_clean(_init_history):
"""Clean up history (permanent)"""
def main(self):
super(self.__class__, self).main()
@errors.generic.all
def _run(self):
self.history.clean()
def main(self):
super(self.__class__, self)._run()
self._run()
@command(history_cmds)
class history_run(_init_history):
......@@ -149,6 +162,7 @@ class history_run(_init_history):
super(self.__class__, self).__init__(arguments)
self._cmd_tree = cmd_tree
@errors.generic.all
def _run_from_line(self, line):
terms = split_input(line)
cmd, args = self._cmd_tree.find_best_match(terms)
......@@ -169,17 +183,15 @@ class history_run(_init_history):
print('Execution of [ %s ] failed' % line)
print('\t%s' % e)
@errors.history._get_cmd_ids
def _get_cmd_ids(self, cmd_ids):
if not cmd_ids:
raise CLISyntaxError('Usage: <id1|id1-id2> [id3|id3-id4] ...',
details=self.__doc__.split('\n'))
cmd_id_list = []
for cmd_str in cmd_ids:
cmd_id_list += _get_num_list(cmd_str)
return cmd_id_list
def main(self, *command_ids):
super(self.__class__, self).main()
@errors.generic.all
def _run(self, *command_ids):
cmd_list = self._get_cmd_ids(command_ids)
for cmd_id in cmd_list:
r = self.history.retrieve(cmd_id)
......@@ -190,3 +202,7 @@ class history_run(_init_history):
if self._cmd_tree:
r = r[len('kamaki '):-1] if r.startswith('kamaki ') else r[:-1]
self._run_from_line(r)
def main(self, *command_ids):
super(self.__class__, self)._run()
self._run(*command_ids)
......@@ -114,7 +114,7 @@ def raiseCLIError(err, message='', importance=0, details=[]):
message = unicode(message) if message else unicode(origerr)
try:
status = err.status
status = err.status or err.errno
except AttributeError:
status = None
......
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