Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: tools/misc_utils.py

Issue 353853003: Whitespace fixes for Python tools (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/add_codereview_message.py ('k') | tools/roll_deps.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 Google Inc. 1 # Copyright 2014 Google Inc.
2 # 2 #
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 6
7 """Miscellaneous utilities.""" 7 """Miscellaneous utilities."""
8 8
9 9
10 import re 10 import re
11 11
12 12
13 class ReSearch(object): 13 class ReSearch(object):
14 """A collection of static methods for regexing things.""" 14 """A collection of static methods for regexing things."""
15 15
16 @staticmethod 16 @staticmethod
17 def search_within_stream(input_stream, pattern, default=None): 17 def search_within_stream(input_stream, pattern, default=None):
18 """Search for regular expression in a file-like object. 18 """Search for regular expression in a file-like object.
19 19
20 Opens a file for reading and searches line by line for a match to 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 21 the regex and returns the parenthesized group named return for the
22 first match. Does not search across newlines. 22 first match. Does not search across newlines.
23 23
24 For example: 24 For example:
25 pattern = '^root(:[^:]*){4}:(?P<return>[^:]*)' 25 pattern = '^root(:[^:]*){4}:(?P<return>[^:]*)'
26 with open('/etc/passwd', 'r') as stream: 26 with open('/etc/passwd', 'r') as stream:
27 return search_within_file(stream, pattern) 27 return search_within_file(stream, pattern)
28 should return root's home directory (/root on my system). 28 should return root's home directory (/root on my system).
29 29
30 Args: 30 Args:
31 input_stream: file-like object to be read 31 input_stream: file-like object to be read
32 pattern: (string) to be passed to re.compile 32 pattern: (string) to be passed to re.compile
33 default: what to return if no match 33 default: what to return if no match
34 34
35 Returns: 35 Returns:
36 A string or whatever default is 36 A string or whatever default is
37 """ 37 """
38 pattern_object = re.compile(pattern) 38 pattern_object = re.compile(pattern)
39 for line in input_stream: 39 for line in input_stream:
40 match = pattern_object.search(line) 40 match = pattern_object.search(line)
41 if match: 41 if match:
42 return match.group('return') 42 return match.group('return')
43 return default 43 return default
44 44
45 @staticmethod 45 @staticmethod
46 def search_within_string(input_string, pattern, default=None): 46 def search_within_string(input_string, pattern, default=None):
47 """Search for regular expression in a string. 47 """Search for regular expression in a string.
48 48
49 Args: 49 Args:
50 input_string: (string) to be searched 50 input_string: (string) to be searched
51 pattern: (string) to be passed to re.compile 51 pattern: (string) to be passed to re.compile
52 default: what to return if no match 52 default: what to return if no match
53 53
54 Returns: 54 Returns:
55 A string or whatever default is 55 A string or whatever default is
56 """ 56 """
57 match = re.search(pattern, input_string) 57 match = re.search(pattern, input_string)
58 return match.group('return') if match else default 58 return match.group('return') if match else default
59
OLDNEW
« no previous file with comments | « tools/add_codereview_message.py ('k') | tools/roll_deps.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698