commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 1 | # Copyright 2014 Google Inc. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | |
Eric Boren | bb0ef0a | 2014-06-25 11:13:27 -0400 | [diff] [blame] | 7 | """Miscellaneous utilities.""" |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 8 | |
Eric Boren | bb0ef0a | 2014-06-25 11:13:27 -0400 | [diff] [blame] | 9 | |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 10 | import re |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class ReSearch(object): |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 14 | """A collection of static methods for regexing things.""" |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 15 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 16 | @staticmethod |
| 17 | def search_within_stream(input_stream, pattern, default=None): |
| 18 | """Search for regular expression in a file-like object. |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 19 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 20 | Opens a file for reading and searches line by line for a match to |
| 21 | the regex and returns the parenthesized group named return for the |
| 22 | first match. Does not search across newlines. |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 23 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 24 | For example: |
| 25 | pattern = '^root(:[^:]*){4}:(?P<return>[^:]*)' |
| 26 | with open('/etc/passwd', 'r') as stream: |
| 27 | return search_within_file(stream, pattern) |
| 28 | should return root's home directory (/root on my system). |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 29 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 30 | Args: |
| 31 | input_stream: file-like object to be read |
| 32 | pattern: (string) to be passed to re.compile |
| 33 | default: what to return if no match |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 34 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 35 | Returns: |
| 36 | A string or whatever default is |
| 37 | """ |
| 38 | pattern_object = re.compile(pattern) |
| 39 | for line in input_stream: |
| 40 | match = pattern_object.search(line) |
| 41 | if match: |
| 42 | return match.group('return') |
| 43 | return default |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 44 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 45 | @staticmethod |
| 46 | def search_within_string(input_string, pattern, default=None): |
| 47 | """Search for regular expression in a string. |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 48 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 49 | Args: |
| 50 | input_string: (string) to be searched |
| 51 | pattern: (string) to be passed to re.compile |
| 52 | default: what to return if no match |
commit-bot@chromium.org | 0ed9002 | 2014-01-30 22:12:30 +0000 | [diff] [blame] | 53 | |
borenet | 3da21d2 | 2014-06-25 08:40:58 -0700 | [diff] [blame] | 54 | Returns: |
| 55 | A string or whatever default is |
| 56 | """ |
| 57 | match = re.search(pattern, input_string) |
| 58 | return match.group('return') if match else default |
| 59 | |