@errors.generic.all
def _run(self):
- self.history.clean()
+ self.history.empty()
def main(self):
super(self.__class__, self)._run()
def _run(self, *command_ids):
cmd_list = self._get_cmd_ids(command_ids)
for cmd_id in cmd_list:
- r = self.history.retrieve(cmd_id)
+ r = self.history[cmd_id]
try:
self.writeln('< %s >' % r[:-1])
except (TypeError, KeyError):
class History(object):
- def __init__(self, filepath, token=None):
+ def __init__(self, filepath, token=None, max_lines=0):
self.filepath = filepath
self.token = token
+ self.max_lines = max_lines
+
+ def __getitem__(self, cmd_id):
+ cmd_id = int(cmd_id)
+ if not cmd_id:
+ return None
+ with open(self.filepath) as f:
+ try:
+ cmd_list = f.readlines()[:-1] # exclude current command
+ return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)]
+ except IndexError:
+ return None
@classmethod
def _match(self, line, match_terms):
with open(self.filepath, 'a+') as f:
f.write(line + '\n')
- def clean(self):
+ def empty(self):
with open(self.filepath, 'w'):
pass
- def retrieve(self, cmd_id):
- """
- :param cmd_id: (int) the id of the command to retrieve can be positive
- or negative, zero values are ignored
+ def clean(self):
+ """DEPRECATED in version 0.14"""
+ return self.empty()
- :returns: (str) the stored command record without the id
- """
- cmd_id = int(cmd_id)
- if not cmd_id:
- return None
- with open(self.filepath) as f:
- try:
- cmd_list = f.readlines()[:-1] # exclude current command
- return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)]
- except IndexError:
- return None
+ def retrieve(self, cmd_id):
+ """DEPRECATED in version 0.14"""
+ return self[cmd_id]
self.assertEqual(
self.file.read(), '\n'.join(some_strings[:(i + 1)]) + '\n')
- def test_clean(self):
+ def test_empty(self):
content = 'a brick\ntwo bricks\nanother brick\nA wall!\n'
self.file.write(content)
self.file.flush()
self.file.seek(0)
self.assertEqual(self.file.read(), content)
history = self.HCLASS(self.file.name)
- history.clean()
+ history.empty()
self.file.seek(0)
self.assertEqual(self.file.read(), '')
self.file.flush()
history = self.HCLASS(self.file.name)
- self.assertRaises(ValueError, history.retrieve, 'must be number')
- self.assertRaises(TypeError, history.retrieve, [1, 2, 3])
+ retrieve = history.__getitem__
+ self.assertRaises(ValueError, retrieve, 'must be number')
+ self.assertRaises(TypeError, retrieve, [1, 2, 3])
for i in (0, len(sample_history), -len(sample_history)):
- self.assertEqual(history.retrieve(i), None)
+ self.assertEqual(history[i], None)
for i in range(1, len(sample_history)):
- self.assertEqual(history.retrieve(i), sample_history[i - 1])
- self.assertEqual(history.retrieve(- i), sample_history[- i - 1])
+ self.assertEqual(history[i], sample_history[i - 1])
+ self.assertEqual(history[- i], sample_history[- i - 1])
class LoggerMethods(TestCase):