Author: pekka.klarck
Date: Mon Apr 13 11:21:35 2009
New Revision: 1794
Modified:
trunk/src/robot/libraries/BuiltIn.py
trunk/src/robot/libraries/String.py
Log:
Docs for new Get Lines Containing/Matching keywords in String and
deprecated BuiltIn.Grep (issue 285)
Modified: trunk/src/robot/libraries/BuiltIn.py
==============================================================================
--- trunk/src/robot/libraries/BuiltIn.py (original)
+++ trunk/src/robot/libraries/BuiltIn.py Mon Apr 13 11:21:35 2009
@@ -1260,7 +1260,7 @@
try:
return eval(expression, namespace)
except:
- raise Exception("Evaluating expression '%s' failed: %s"
+ raise DataError("Evaluating expression '%s' failed: %s"
% (expression, get_error_message()))
def call_method(self, object, method_name, *args):
@@ -1281,46 +1281,12 @@
try:
method = getattr(object, method_name)
except AttributeError:
- raise Exception("Object '%s' does not have a method '%s'"
+ raise DataError("Object '%s' does not have a method '%s'"
% (object, method_name))
return method(*args)
def grep(self, text, pattern, pattern_type='literal string'):
- """Returns the text grepped using `pattern`.
-
- `pattern_type` defines how the given pattern is interpreted as
- explained below. `pattern_type` argument is case-insensitive
- and may contain other text. For example, 'regexp', 'REGEXP'
- and 'Pattern is a regexp' are all considered equal.
-
- 1) If `pattern_type` contains either the strings 'simple' or
- 'glob', the `pattern` is considered a simple glob pattern
- where:
- | * | matches everything |
- | ? | matches any single character |
- | [chars] | matches any character inside square brackets
(e.g. '[abc]' matches either 'a', 'b' or 'c') |
- | [!chars] | matches any character not inside square brackets |
-
- 2) If `pattern_type` contains either 'simple' or 'glob', and
- additionally contains 'case-insensitive' or 'case
- insensitive', the glob pattern is considered
- case-insensitive. This functionality is available in 2.0.2
- version and newer.
-
- 3) If `pattern_type` contains either the string 'regular
- expression' or 'regexp', the `pattern` is considered a
- regular expression. See `Should Match Regexp` for more
information
- about using regular expressions.
-
- 4) If `pattern_type` contains either 'case-insensitive' or
- 'case insensitive' (but does not contain 'simple' or
- 'glob'), `pattern` is considered a literal string and
- lines returned, if they contain the string, regardless of
- the case.
-
- 5) Otherwise the pattern is considered a literal string and lines
- returned, if they contain the string.
- """
+ """*DEPRECATED* Use `Get Lines Matching XXX` keywords from
`String` library instead. This keyword will be removed in Robot Framework
2.2."""
lines = _filter_lines(text.splitlines(), pattern, pattern_type)
return '\n'.join(lines)
Modified: trunk/src/robot/libraries/String.py
==============================================================================
--- trunk/src/robot/libraries/String.py (original)
+++ trunk/src/robot/libraries/String.py Mon Apr 13 11:21:35 2009
@@ -78,6 +78,24 @@
return lines[start:end]
def get_lines_containing_string(self, string, pattern,
case_insensitive=False):
+ """Returns lines of the given `string` that contain the `pattern`.
+
+ The `pattern` is always considered to be a normal string and a
+ line matches if the `pattern` is found anywhere in it. By
+ default the match is case-sensitive, but setting
+ `case_insensitive` to any value makes it case-insensitive.
+
+ Lines are returned as one string catenated back together with
+ newlines. Possible trailing newline is never returned. The
+ number of matching lines is automatically logged.
+
+ Examples:
+ | ${lines} = | Get Lines Containing String | ${result} | An
example |
+ | ${ret} = | Get Lines Containing String | ${ret} | FAIL |
case-insensitive |
+
+ See `Get Lines Matching Pattern` and `Get Lines Matching Regexp`
+ if you need more complex pattern matching.
+ """
if case_insensitive:
pattern = pattern.lower()
contains = lambda line: pattern in line.lower()
@@ -86,6 +104,27 @@
return self._get_matching_lines(string, contains)
def get_lines_matching_pattern(self, string, pattern,
case_insensitive=False):
+ """Returns lines of the given `string` that match the `pattern`.
+
+ The `pattern` is a _glob pattern_ where _*_ and _?_ can be
+ used as wildcards so that the former matches anything and the
+ latter any single character. A line matches only if it matches
+ the `pattern` fully. By default the match is case-sensitive,
+ but setting `case_insensitive` to any value makes it
+ case-insensitive.
+
+ Lines are returned as one string catenated back together with
+ newlines. Possible trailing newline is never returned.The
+ number of matching lines is automatically logged.
+
+ Examples:
+ | ${lines} = | Get Lines Matching Pattern | ${result} | Wild????
example |
+ | ${ret} = | Get Lines Matching Pattern | ${ret} | FAIL: * |
case-insensitive |
+
+ See `Get Lines Matching Regexp` if you need more complex
+ patterns and `Get Lines Containing String` if searching
+ literal strings is enough.
+ """
if case_insensitive:
pattern = pattern.lower()
matches = lambda line: fnmatchcase(line.lower(), pattern)
@@ -94,6 +133,26 @@
return self._get_matching_lines(string, matches)
def get_lines_matching_regexp(self, string, pattern):
+ """Returns lines of the given `string` that match the regexp
`pattern`.
+
+ See `BuiltIn.Should Match Regexp` for more information about
+ Python regular expression syntax in general and how to use it
+ in Robot Framework test data in particular. A line matches
+ only if it matches the `pattern` fully. Notice that to make
+ the match case-insensitive, you need to embed case-insensitive
+ flag into the pattern.
+
+ Lines are returned as one string catenated back together with
+ newlines. Possible trailing newline is never returned. The
+ number of matching lines is automatically logged.
+
+ Examples:
+ | ${lines} = | Get Lines Matching Regexp | ${result} | Reg\\\\w{3}
example |
+ | ${ret} = | Get Lines Matching Pattern | ${ret} | (?i)FAIL: .* |
+
+ See `Get Lines Matching Pattern` and `Get Lines Containing
+ String` if you do not need full regular expression powers.
+ """
regexp = re.compile('^%s$' % pattern)
return self._get_matching_lines(string, regexp.match)