Commit 50a32c37 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Only two cmds in history, use history_limit

Refs: #4479
parent 5e383dd4
......@@ -557,14 +557,13 @@ def run_one_cmd(exe, parser):
cloud = _init_session(parser.arguments, is_non_API(parser))
if parser.unparsed:
global _history
cnf = parser.arguments['config']
try:
token = parser.arguments['config'].get_cloud(
cloud, 'token').split()[0]
token = cnf.get_cloud(cloud, 'token').split()[0]
except Exception:
token = None
_history = History(
parser.arguments['config'].get('global', 'history_file'),
token=token)
_history = History(cnf.get('global', 'history_file'), token=token)
_history.limit = cnf.get('global', 'history_limit')
_history.add(' '.join([exe] + argv[1:]))
from kamaki.cli import one_command
one_command.run(cloud, parser, _help)
......
......@@ -307,8 +307,9 @@ class Shell(Cmd):
self.auth_base = auth_base
self.cloud = cloud
self._parser = parser
self._history = History(
parser.arguments['config'].get('global', 'history_file'))
cnf = parser.arguments['config']
self._history = History(cnf.get('global', 'history_file'))
self._history.limit = cnf.get('global', 'history_limit')
if path:
cmd = self.cmd_tree.get_command(path)
intro = cmd.path.replace('_', ' ')
......
......@@ -49,6 +49,7 @@ class _init_history(_command_init):
@errors.history.init
def _run(self):
self.history = History(self.config.get('global', 'history_file'))
self.history.limit = self.config.get('global', 'history_limit')
def main(self):
self._run()
......
......@@ -78,6 +78,7 @@ DEFAULTS = {
'log_data': 'off',
'log_pid': 'off',
'history_file': HISTORY_PATH,
'history_limit': 0,
'user_cli': 'astakos',
'quota_cli': 'astakos',
'resource_cli': 'astakos',
......
......@@ -34,6 +34,10 @@
# or implied, of GRNET S.A.
import codecs
from logging import getLogger
log = getLogger(__name__)
class History(object):
......@@ -46,7 +50,8 @@ class History(object):
def __getitem__(self, cmd_ids):
with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
try:
cmd_list = f.readlines()[1:]
lines = f.readlines()
self.counter, cmd_list = int(lines[0]), lines[1:]
return cmd_list[cmd_ids]
except IndexError:
return None
......@@ -61,14 +66,17 @@ class History(object):
if new_limit < 0:
raise ValueError('Invalid history limit (%s)' % new_limit)
old_limit, self._limit = self._limit, new_limit
if self._limit and self._limit < old_limit:
if self._limit and ((not old_limit) or (self._limit <= old_limit)):
with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
lines = f.readlines()
self.counter = int(lines[0])
old_len = len(lines[1:])
if old_len > new_limit:
self.counter += old_len - new_limit
if old_len > new_limit:
self.counter += old_len - new_limit
with codecs.open(
self.filepath, mode='w', encoding='utf-8') as f:
f.write('%s\n' % self.counter)
f.write(''.join(lines[(self.counter + 1):]))
f.write(''.join(lines[old_len - new_limit + 1:]))
f.flush()
@classmethod
......@@ -85,11 +93,15 @@ class History(object):
return r[- limit:]
def add(self, line):
line = '%s' % line or ''
line = line.replace(self.token, '...') if self.token else line
with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f:
f.write(line + '\n')
f.flush()
self.limit = self.limit
try:
with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f:
f.write(line + '\n')
f.flush()
self.limit = self.limit
except Exception as e:
log.debug('Add history failed for "%s" (%s)' % (line, e))
def empty(self):
with open(self.filepath, 'w') as f:
......
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