Only two cmds in history, use history_limit
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 24 Feb 2014 11:16:33 +0000 (13:16 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 24 Feb 2014 11:16:33 +0000 (13:16 +0200)
Refs: #4479

kamaki/cli/__init__.py
kamaki/cli/command_shell.py
kamaki/cli/commands/history.py
kamaki/cli/config/__init__.py
kamaki/cli/history.py

index d9be5b3..8087c42 100644 (file)
@@ -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)
index 9034008..866c8ac 100644 (file)
@@ -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('_', ' ')
index 1142113..6bbf79e 100644 (file)
@@ -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()
index 6505613..a03c7a3 100644 (file)
@@ -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',
index fdc1f50..c824844 100644 (file)
 # 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: