Revision: 4470
Author: jprantan
Date: Thu Dec  9 04:11:50 2010
Log: Added grep to deprecated_builtin. Issue 730.
http://code.google.com/p/robotframework/source/detail?r=4470

Modified:
 /trunk/src/robot/libraries/DeprecatedBuiltIn.py

=======================================
--- /trunk/src/robot/libraries/DeprecatedBuiltIn.py     Mon May 31 05:21:54 2010
+++ /trunk/src/robot/libraries/DeprecatedBuiltIn.py     Thu Dec  9 04:11:50 2010
@@ -13,6 +13,10 @@
 #  limitations under the License.


+import re
+import fnmatch
+
+
 from robot.utils import asserts

 import BuiltIn
@@ -58,3 +62,28 @@
     def error(self, msg=None):
         """Errors the test immediately with the given message."""
         asserts.error(msg)
+
+    def grep(self, text, pattern, pattern_type='literal string'):
+ lines = self._filter_lines(text.splitlines(), pattern, pattern_type)
+        return '\n'.join(lines)
+
+    def _filter_lines(self, lines, pattern, ptype):
+        ptype = ptype.lower().replace(' ','').replace('-','')
+        if not pattern:
+            filtr = lambda line: True
+        elif 'simple' in ptype or 'glob' in ptype:
+            if 'caseinsensitive' in ptype:
+                pattern = pattern.lower()
+ filtr = lambda line: fnmatch.fnmatchcase(line.lower(), pattern)
+            else:
+                filtr = lambda line: fnmatch.fnmatchcase(line, pattern)
+        elif 'regularexpression' in ptype or 'regexp' in ptype:
+            pattern = re.compile(pattern)
+            filtr = lambda line: pattern.search(line)
+        elif 'caseinsensitive' in ptype:
+            pattern = pattern.lower()
+            filtr = lambda line: pattern in line.lower()
+        else:
+            filtr = lambda line: pattern in line
+        return [ line for line in lines if filtr(line) ]
+

Reply via email to