Simplify history _match and get
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Thu, 20 Feb 2014 13:48:19 +0000 (15:48 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Thu, 20 Feb 2014 13:48:19 +0000 (15:48 +0200)
Refs: #4479

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

index 52c2d4e..139fbeb 100644 (file)
@@ -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[cmd_id]
+            r = self.history[cmd_id - 1 if cmd_id > 0 else cmd_id]
             try:
                 self.writeln('< %s >' % r[:-1])
             except (TypeError, KeyError):
index 49a7c20..5b039fd 100644 (file)
@@ -42,47 +42,42 @@ class History(object):
         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:
+    def __getitem__(self, cmd_ids):
+        with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
             try:
-                cmd_list = f.readlines()[:-1]  # exclude current command
-                return cmd_list[cmd_id - (1 if cmd_id > 0 else 0)]
+                cmd_list = f.readlines()
+                return cmd_list[cmd_ids]
             except IndexError:
                 return None
 
     @classmethod
     def _match(self, line, match_terms):
-        if match_terms is None:
-            return True
-        for term in match_terms.split():
-            if term not in line:
-                return False
+        if match_terms:
+            return all(term in line for term in match_terms.split())
         return True
 
     def get(self, match_terms=None, limit=0):
         limit = int(limit or 0)
-        with codecs.open(self.filepath, mode='r', encoding='utf-8') as f:
-            result = [u'%s.  \t%s' % (
-                i + 1, line) for i, line in enumerate(f.readlines())
-                if self._match(line, match_terms)]
-            return result[- limit:]
+        r = ['%s.\t%s' % (i + 1, line) for i, line in enumerate(self[:]) if (
+                self._match(line, match_terms))]
+        return r[- limit:]
 
     def add(self, line):
         line = line.replace(self.token, '...') if self.token else line
-        with open(self.filepath, 'a+') as f:
+        with codecs.open(self.filepath, mode='a+', encoding='utf-8') as f:
             f.write(line + '\n')
+            f.flush()
 
     def empty(self):
-        with open(self.filepath, 'w'):
-            pass
+        with open(self.filepath, 'w') as f:
+            f.flush()
 
     def clean(self):
-        """DEPRECATED in version 0.14"""
+        """DEPRECATED since version 0.14"""
         return self.empty()
 
     def retrieve(self, cmd_id):
-        """DEPRECATED in version 0.14"""
-        return self[cmd_id]
+        if not cmd_id:
+            return None
+        cmd_id = int(cmd_id)
+        return self[cmd_id - (1 if cmd_id > 0 else 0)]
index b48bb3a..1eec2b5 100644 (file)
@@ -75,14 +75,14 @@ class History(TestCase):
         self.assertEqual(history.get(), [])
 
         sample_history = (
-            'kamaki history show\n',
-            'kamaki file list\n',
-            'kamaki touch pithos:f1\n',
-            'kamaki file info pithos:f1\n')
+            u'kamaki history show\n',
+            u'kamaki file list\n',
+            u'kamaki touch pithos:f1\n',
+            u'kamaki file info pithos:f1\n')
         self.file.write(''.join(sample_history))
         self.file.flush()
 
-        expected = ['%s.  \t%s' % (
+        expected = [u'%s.\t%s' % (
             i + 1, event) for i, event in enumerate(sample_history)]
         self.assertEqual(history.get(), expected)
         self.assertEqual(history.get('kamaki'), expected)
@@ -119,22 +119,21 @@ class History(TestCase):
         sample_history = (
             'kamaki history show\n',
             'kamaki file list\n',
-            'kamaki touch pithos:f1\n',
-            'kamaki file info pithos:f1\n',
-            'current / last command is always excluded')
+            'kamaki file create /pithos/f1\n',
+            'kamaki file info /pithos/f1\n',
+            'last command is always excluded')
         self.file.write(''.join(sample_history))
         self.file.flush()
 
         history = self.HCLASS(self.file.name)
-        retrieve = history.__getitem__
-        self.assertRaises(ValueError, retrieve, 'must be number')
-        self.assertRaises(TypeError, retrieve, [1, 2, 3])
+        self.assertRaises(ValueError, history.retrieve, 'must be number')
+        self.assertRaises(TypeError, history.retrieve, [1, 2, 3])
 
-        for i in (0, len(sample_history), -len(sample_history)):
-            self.assertEqual(history[i], None)
+        for i in (0, len(sample_history) + 1, - len(sample_history) - 1):
+            self.assertEqual(history.retrieve(i), None)
         for i in range(1, len(sample_history)):
-            self.assertEqual(history[i], sample_history[i - 1])
-            self.assertEqual(history[- i], sample_history[- i - 1])
+            self.assertEqual(history.retrieve(i), sample_history[i - 1])
+            self.assertEqual(history.retrieve(- i), sample_history[- i])
 
 
 class LoggerMethods(TestCase):