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)
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('_', ' ')
# or implied, of GRNET S.A.
import codecs
+from logging import getLogger
+
+
+log = getLogger(__name__)
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
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
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: