From edaf3ba61a0b096ceef338138bb6fc41dbbc56e6 Mon Sep 17 00:00:00 2001 From: Stavros Sachtouris Date: Thu, 20 Feb 2014 14:34:21 +0200 Subject: [PATCH] Rename method in kamaki.cli.history Refs: #4479 retrieve --> __getitem__ clean --> empty --- kamaki/cli/commands/history.py | 4 ++-- kamaki/cli/history.py | 37 ++++++++++++++++++++----------------- kamaki/cli/test.py | 15 ++++++++------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/kamaki/cli/commands/history.py b/kamaki/cli/commands/history.py index 32601ac..52c2d4e 100644 --- a/kamaki/cli/commands/history.py +++ b/kamaki/cli/commands/history.py @@ -137,7 +137,7 @@ class history_clean(_init_history): @errors.generic.all def _run(self): - self.history.clean() + self.history.empty() def main(self): super(self.__class__, self)._run() @@ -196,7 +196,7 @@ class history_run(_init_history): 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): diff --git a/kamaki/cli/history.py b/kamaki/cli/history.py index 7623bbb..49a7c20 100644 --- a/kamaki/cli/history.py +++ b/kamaki/cli/history.py @@ -37,9 +37,21 @@ import codecs 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): @@ -63,23 +75,14 @@ class History(object): 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] diff --git a/kamaki/cli/test.py b/kamaki/cli/test.py index 9fbd613..b48bb3a 100644 --- a/kamaki/cli/test.py +++ b/kamaki/cli/test.py @@ -104,14 +104,14 @@ class History(TestCase): 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(), '') @@ -126,14 +126,15 @@ class History(TestCase): 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): -- 1.7.10.4