Revision 50a32c37

b/kamaki/cli/__init__.py
557 557
    cloud = _init_session(parser.arguments, is_non_API(parser))
558 558
    if parser.unparsed:
559 559
        global _history
560
        cnf = parser.arguments['config']
560 561
        try:
561
            token = parser.arguments['config'].get_cloud(
562
                cloud, 'token').split()[0]
562
            token = cnf.get_cloud(cloud, 'token').split()[0]
563 563
        except Exception:
564 564
            token = None
565
        _history = History(
566
            parser.arguments['config'].get('global', 'history_file'),
567
            token=token)
565
        _history = History(cnf.get('global', 'history_file'), token=token)
566
        _history.limit = cnf.get('global', 'history_limit')
568 567
        _history.add(' '.join([exe] + argv[1:]))
569 568
        from kamaki.cli import one_command
570 569
        one_command.run(cloud, parser, _help)
b/kamaki/cli/command_shell.py
307 307
        self.auth_base = auth_base
308 308
        self.cloud = cloud
309 309
        self._parser = parser
310
        self._history = History(
311
            parser.arguments['config'].get('global', 'history_file'))
310
        cnf = parser.arguments['config']
311
        self._history = History(cnf.get('global', 'history_file'))
312
        self._history.limit = cnf.get('global', 'history_limit')
312 313
        if path:
313 314
            cmd = self.cmd_tree.get_command(path)
314 315
            intro = cmd.path.replace('_', ' ')
b/kamaki/cli/commands/history.py
49 49
    @errors.history.init
50 50
    def _run(self):
51 51
        self.history = History(self.config.get('global', 'history_file'))
52
        self.history.limit = self.config.get('global', 'history_limit')
52 53

  
53 54
    def main(self):
54 55
        self._run()
b/kamaki/cli/config/__init__.py
78 78
        'log_data': 'off',
79 79
        'log_pid': 'off',
80 80
        'history_file': HISTORY_PATH,
81
        'history_limit': 0,
81 82
        'user_cli': 'astakos',
82 83
        'quota_cli': 'astakos',
83 84
        'resource_cli': 'astakos',
b/kamaki/cli/history.py
34 34
# or implied, of GRNET S.A.
35 35

  
36 36
import codecs
37
from logging import getLogger
38

  
39

  
40
log = getLogger(__name__)
37 41

  
38 42

  
39 43
class History(object):
......
46 50
    def __getitem__(self, cmd_ids):
47 51
        with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
48 52
            try:
49
                cmd_list = f.readlines()[1:]
53
                lines = f.readlines()
54
                self.counter, cmd_list = int(lines[0]), lines[1:]
50 55
                return cmd_list[cmd_ids]
51 56
            except IndexError:
52 57
                return None
......
61 66
        if new_limit < 0:
62 67
            raise ValueError('Invalid history limit (%s)' % new_limit)
63 68
        old_limit, self._limit = self._limit, new_limit
64
        if self._limit and self._limit < old_limit:
69
        if self._limit and ((not old_limit) or (self._limit <= old_limit)):
65 70
            with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
66 71
                lines = f.readlines()
72
                self.counter = int(lines[0])
67 73
                old_len = len(lines[1:])
68
                if old_len > new_limit:
69
                    self.counter += old_len - new_limit
74
            if old_len > new_limit:
75
                self.counter += old_len - new_limit
76
                with codecs.open(
77
                        self.filepath, mode='w', encoding='utf-8') as f:
70 78
                    f.write('%s\n' % self.counter)
71
                    f.write(''.join(lines[(self.counter + 1):]))
79
                    f.write(''.join(lines[old_len - new_limit + 1:]))
72 80
                    f.flush()
73 81

  
74 82
    @classmethod
......
85 93
        return r[- limit:]
86 94

  
87 95
    def add(self, line):
96
        line = '%s' % line or ''
88 97
        line = line.replace(self.token, '...') if self.token else line
89
        with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f:
90
            f.write(line + '\n')
91
            f.flush()
92
        self.limit = self.limit
98
        try:
99
            with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f:
100
                f.write(line + '\n')
101
                f.flush()
102
            self.limit = self.limit
103
        except Exception as e:
104
            log.debug('Add history failed for "%s" (%s)' % (line, e))
93 105

  
94 106
    def empty(self):
95 107
        with open(self.filepath, 'w') as f:

Also available in: Unified diff