Revision edaf3ba6

b/kamaki/cli/commands/history.py
137 137

  
138 138
    @errors.generic.all
139 139
    def _run(self):
140
        self.history.clean()
140
        self.history.empty()
141 141

  
142 142
    def main(self):
143 143
        super(self.__class__, self)._run()
......
196 196
    def _run(self, *command_ids):
197 197
        cmd_list = self._get_cmd_ids(command_ids)
198 198
        for cmd_id in cmd_list:
199
            r = self.history.retrieve(cmd_id)
199
            r = self.history[cmd_id]
200 200
            try:
201 201
                self.writeln('< %s >' % r[:-1])
202 202
            except (TypeError, KeyError):
b/kamaki/cli/history.py
37 37

  
38 38

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

  
45
    def __getitem__(self, cmd_id):
46
        cmd_id = int(cmd_id)
47
        if not cmd_id:
48
            return None
49
        with open(self.filepath) as f:
50
            try:
51
                cmd_list = f.readlines()[:-1]  # exclude current command
52
                return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)]
53
            except IndexError:
54
                return None
43 55

  
44 56
    @classmethod
45 57
    def _match(self, line, match_terms):
......
63 75
        with open(self.filepath, 'a+') as f:
64 76
            f.write(line + '\n')
65 77

  
66
    def clean(self):
78
    def empty(self):
67 79
        with open(self.filepath, 'w'):
68 80
            pass
69 81

  
70
    def retrieve(self, cmd_id):
71
        """
72
        :param cmd_id: (int) the id of the command to retrieve can be positive
73
            or negative, zero values are ignored
82
    def clean(self):
83
        """DEPRECATED in version 0.14"""
84
        return self.empty()
74 85

  
75
        :returns: (str) the stored command record without the id
76
        """
77
        cmd_id = int(cmd_id)
78
        if not cmd_id:
79
            return None
80
        with open(self.filepath) as f:
81
            try:
82
                cmd_list = f.readlines()[:-1]  # exclude current command
83
                return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)]
84
            except IndexError:
85
                return None
86
    def retrieve(self, cmd_id):
87
        """DEPRECATED in version 0.14"""
88
        return self[cmd_id]
b/kamaki/cli/test.py
104 104
            self.assertEqual(
105 105
                self.file.read(), '\n'.join(some_strings[:(i + 1)]) + '\n')
106 106

  
107
    def test_clean(self):
107
    def test_empty(self):
108 108
        content = 'a brick\ntwo bricks\nanother brick\nA wall!\n'
109 109
        self.file.write(content)
110 110
        self.file.flush()
111 111
        self.file.seek(0)
112 112
        self.assertEqual(self.file.read(), content)
113 113
        history = self.HCLASS(self.file.name)
114
        history.clean()
114
        history.empty()
115 115
        self.file.seek(0)
116 116
        self.assertEqual(self.file.read(), '')
117 117

  
......
126 126
        self.file.flush()
127 127

  
128 128
        history = self.HCLASS(self.file.name)
129
        self.assertRaises(ValueError, history.retrieve, 'must be number')
130
        self.assertRaises(TypeError, history.retrieve, [1, 2, 3])
129
        retrieve = history.__getitem__
130
        self.assertRaises(ValueError, retrieve, 'must be number')
131
        self.assertRaises(TypeError, retrieve, [1, 2, 3])
131 132

  
132 133
        for i in (0, len(sample_history), -len(sample_history)):
133
            self.assertEqual(history.retrieve(i), None)
134
            self.assertEqual(history[i], None)
134 135
        for i in range(1, len(sample_history)):
135
            self.assertEqual(history.retrieve(i), sample_history[i - 1])
136
            self.assertEqual(history.retrieve(- i), sample_history[- i - 1])
136
            self.assertEqual(history[i], sample_history[i - 1])
137
            self.assertEqual(history[- i], sample_history[- i - 1])
137 138

  
138 139

  
139 140
class LoggerMethods(TestCase):

Also available in: Unified diff