Revision a44a9d97

b/kamaki/cli/commands/history.py
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[cmd_id]
199
            r = self.history[cmd_id - 1 if cmd_id > 0 else cmd_id]
200 200
            try:
201 201
                self.writeln('< %s >' % r[:-1])
202 202
            except (TypeError, KeyError):
b/kamaki/cli/history.py
42 42
        self.token = token
43 43
        self.max_lines = max_lines
44 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:
45
    def __getitem__(self, cmd_ids):
46
        with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
50 47
            try:
51
                cmd_list = f.readlines()[:-1]  # exclude current command
52
                return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)]
48
                cmd_list = f.readlines()
49
                return cmd_list[cmd_ids]
53 50
            except IndexError:
54 51
                return None
55 52

  
56 53
    @classmethod
57 54
    def _match(self, line, match_terms):
58
        if match_terms is None:
59
            return True
60
        for term in match_terms.split():
61
            if term not in line:
62
                return False
55
        if match_terms:
56
            return all(term in line for term in match_terms.split())
63 57
        return True
64 58

  
65 59
    def get(self, match_terms=None, limit=0):
66 60
        limit = int(limit or 0)
67
        with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
68
            result = [u'%s.  \t%s' % (
69
                i + 1, line) for i, line in enumerate(f.readlines())
70
                if self._match(line, match_terms)]
71
            return result[- limit:]
61
        r = ['%s.\t%s' % (i + 1, line) for i, line in enumerate(self[:]) if (
62
                self._match(line, match_terms))]
63
        return r[- limit:]
72 64

  
73 65
    def add(self, line):
74 66
        line = line.replace(self.token, '...') if self.token else line
75
        with open(self.filepath, 'a+') as f:
67
        with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f:
76 68
            f.write(line + '\n')
69
            f.flush()
77 70

  
78 71
    def empty(self):
79
        with open(self.filepath, 'w'):
80
            pass
72
        with open(self.filepath, 'w') as f:
73
            f.flush()
81 74

  
82 75
    def clean(self):
83
        """DEPRECATED in version 0.14"""
76
        """DEPRECATED since version 0.14"""
84 77
        return self.empty()
85 78

  
86 79
    def retrieve(self, cmd_id):
87
        """DEPRECATED in version 0.14"""
88
        return self[cmd_id]
80
        if not cmd_id:
81
            return None
82
        cmd_id = int(cmd_id)
83
        return self[cmd_id - (1 if cmd_id > 0 else 0)]
b/kamaki/cli/test.py
75 75
        self.assertEqual(history.get(), [])
76 76

  
77 77
        sample_history = (
78
            'kamaki history show\n',
79
            'kamaki file list\n',
80
            'kamaki touch pithos:f1\n',
81
            'kamaki file info pithos:f1\n')
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 82
        self.file.write(''.join(sample_history))
83 83
        self.file.flush()
84 84

  
85
        expected = ['%s.  \t%s' % (
85
        expected = [u'%s.\t%s' % (
86 86
            i + 1, event) for i, event in enumerate(sample_history)]
87 87
        self.assertEqual(history.get(), expected)
88 88
        self.assertEqual(history.get('kamaki'), expected)
......
119 119
        sample_history = (
120 120
            'kamaki history show\n',
121 121
            'kamaki file list\n',
122
            'kamaki touch pithos:f1\n',
123
            'kamaki file info pithos:f1\n',
124
            'current / last command is always excluded')
122
            'kamaki file create /pithos/f1\n',
123
            'kamaki file info /pithos/f1\n',
124
            'last command is always excluded')
125 125
        self.file.write(''.join(sample_history))
126 126
        self.file.flush()
127 127

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

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

  
139 138

  
140 139
class LoggerMethods(TestCase):

Also available in: Unified diff