Revision ddc97a10

b/kamaki/cli/commands/history.py
34 34
# or implied, of GRNET S.A.
35 35

  
36 36
from kamaki.cli.command_tree import CommandTree
37
from kamaki.cli.argument import IntArgument, ValueArgument
37
from kamaki.cli.argument import ValueArgument
38 38
from kamaki.cli.argument import ArgumentParseManager
39 39
from kamaki.cli.history import History
40 40
from kamaki.cli import command
......
83 83

  
84 84
@command(history_cmds)
85 85
class history_show(_init_history):
86
    """Show intersession command history
87
    ---
88
    - With no parameters : pick all commands in history records
89
    - With:
90
    .   1.  <order-id> : pick the <order-id>th command
91
    .   2.  <order-id-1>-<order-id-2> : pick all commands ordered in the range
92
    .       [<order-id-1> - <order-id-2>]
93
    .   - the above can be mixed and repeated freely, separated by spaces
94
    .       e.g., pick 2 4-7 -3
95
    .   - Use negative integers to count from the end of the list, e.g.,:
96
    .       -2 means : the command before the last one
97
    .       -2-5 means : last 2 commands + the first 5
98
    .       -5--2 means : the last 5 commands except the last 2
86
    """Show history
87
        Featutes:
88
        - slice notation (cmd numbers --> N or :N or N: or N1:N2)
89
        - text matching (--match)
99 90
    """
100 91

  
101 92
    arguments = dict(
102
        limit=IntArgument(
103
            'number of lines to show',
104
            ('-n', '--numner'),
105
            default=0),
106
        match=ValueArgument('show lines that match given terms', '--match')
93
        match=ValueArgument('Show lines matching this', '--match'),
107 94
    )
108 95

  
109 96
    @errors.generic.all
110
    def _run(self, *cmd_ids):
111
        ret = self.history.get(match_terms=self['match'], limit=self['limit'])
112

  
113
        if not cmd_ids:
114
            self.print_list(ret)
115
            return
116

  
117
        num_list = []
118
        for num_str in cmd_ids:
119
            num_list += _get_num_list(num_str)
120

  
121
        for cmd_id in num_list:
122
            try:
123
                cur_id = int(cmd_id)
124
                if cur_id:
125
                    self.writeln(ret[cur_id - (1 if cur_id > 0 else 0)][:-1])
126
            except IndexError as e2:
127
                raiseCLIError(e2, 'Command id out of 1-%s range' % len(ret))
128

  
129
    def main(self, *cmd_ids):
97
    def _run(self, cmd_slice):
98
        lines = ['%s.\t%s' % (i, l) for i, l in enumerate(self.history[:])][
99
            cmd_slice]
100
        if not isinstance(cmd_slice, slice):
101
            lines = [lines, ]
102
        if self['match']:
103
            lines = [l for l in lines if self._match(l, self['match'])]
104
        self.print_items([l[:-1] for l in lines])
105

  
106
    def main(self, cmd_numbers=''):
130 107
        super(self.__class__, self)._run()
131
        self._run(*cmd_ids)
108
        sl_args = [
109
            int(x) if x else None for x in cmd_numbers.split(':')] if (
110
                cmd_numbers) else [None, None]
111
        slice_cmds = slice(*sl_args) if len(sl_args) > 1 else sl_args[0]
112
        self._run(slice_cmds)
132 113

  
133 114

  
134 115
@command(history_cmds)
b/kamaki/cli/history.py
37 37

  
38 38

  
39 39
class History(object):
40
    def __init__(self, filepath, token=None, max_lines=0):
40
    def __init__(self, filepath, token=None):
41 41
        self.filepath = filepath
42 42
        self.token = token
43
        self.max_lines = max_lines
44 43

  
45 44
    def __getitem__(self, cmd_ids):
46 45
        with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
......
57 56
        return True
58 57

  
59 58
    def get(self, match_terms=None, limit=0):
59
        """DEPRECATED since 0.14"""
60 60
        limit = int(limit or 0)
61 61
        r = ['%s.\t%s' % (i + 1, line) for i, line in enumerate(self[:]) if (
62 62
                self._match(line, match_terms))]
b/kamaki/cli/test.py
70 70
                ((['line', 'with', 'some terms'], 'some terms'), False)):
71 71
            self.assertEqual(self.HCLASS._match(*args), expected)
72 72

  
73
    def test_get(self):
74
        history = self.HCLASS(self.file.name)
75
        self.assertEqual(history.get(), [])
76

  
77
        sample_history = (
78
            u'kamaki history show\n',
79
            u'kamaki file list\n',
80
            u'kamaki touch pithos:f1\n',
81
            u'kamaki file info pithos:f1\n')
82
        self.file.write(''.join(sample_history))
83
        self.file.flush()
84

  
85
        expected = [u'%s.\t%s' % (
86
            i + 1, event) for i, event in enumerate(sample_history)]
87
        self.assertEqual(history.get(), expected)
88
        self.assertEqual(history.get('kamaki'), expected)
89
        self.assertEqual(history.get('file kamaki'), expected[1::2])
90
        self.assertEqual(history.get('pithos:f1'), expected[2:])
91
        self.assertEqual(history.get('touch pithos:f1'), expected[2:3])
92

  
93
        for limit in range(len(sample_history)):
94
            self.assertEqual(history.get(limit=limit), expected[-limit:])
95
            self.assertEqual(
96
                history.get('kamaki', limit=limit), expected[-limit:])
97

  
98 73
    def test_add(self):
99 74
        history = self.HCLASS(self.file.name)
100 75
        some_strings = ('a brick', 'two bricks', 'another brick', 'A wall!')

Also available in: Unified diff