Rename method in kamaki.cli.history
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Thu, 20 Feb 2014 12:34:21 +0000 (14:34 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Thu, 20 Feb 2014 12:34:21 +0000 (14:34 +0200)
Refs: #4479

retrieve --> __getitem__
clean --> empty

kamaki/cli/commands/history.py
kamaki/cli/history.py
kamaki/cli/test.py

index 32601ac..52c2d4e 100644 (file)
@@ -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):
index 7623bbb..49a7c20 100644 (file)
@@ -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]
index 9fbd613..b48bb3a 100644 (file)
@@ -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):