| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """This file contains post process filters for use with the | 5 """This file contains post process filters for use with the |
| 6 RecipeTestApi.post_process method in GenTests. | 6 RecipeTestApi.post_process method in GenTests. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 import re | 9 import re |
| 10 | 10 |
| 11 from collections import defaultdict, OrderedDict, namedtuple | 11 from collections import defaultdict, OrderedDict, namedtuple |
| 12 | 12 |
| 13 | 13 |
| 14 _filterRegexEntry = namedtuple('_filterRegexEntry', 'at_most at_least fields') |
| 15 |
| 16 |
| 14 class Filter(object): | 17 class Filter(object): |
| 15 """Filter is an implementation of a post_process callable which can remove | 18 """Filter is an implementation of a post_process callable which can remove |
| 16 unwanted data from a step OrderedDict.""" | 19 unwanted data from a step OrderedDict.""" |
| 17 _reEntry = namedtuple('_reEntry', 'at_most at_least fields') | |
| 18 | 20 |
| 19 def __init__(self, *steps): | 21 def __init__(self, *steps): |
| 20 """Builds a new Filter object. It may be optionally prepopulated by | 22 """Builds a new Filter object. It may be optionally prepopulated by |
| 21 specifying steps. | 23 specifying steps. |
| 22 | 24 |
| 23 Usage: | 25 Usage: |
| 24 f = Filter('step_a', 'step_b') | 26 f = Filter('step_a', 'step_b') |
| 25 yield TEST + api.post_process(f) | 27 yield TEST + api.post_process(f) |
| 26 | 28 |
| 27 f = f.include('other_step') | 29 f = f.include('other_step') |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 at_least (int) - the number of steps that this regular expression MUST | 104 at_least (int) - the number of steps that this regular expression MUST |
| 103 match. | 105 match. |
| 104 at_most (int) - the maximum number of steps that this regular expression | 106 at_most (int) - the maximum number of steps that this regular expression |
| 105 MUST NOT exceed. | 107 MUST NOT exceed. |
| 106 | 108 |
| 107 Returns the new filter. | 109 Returns the new filter. |
| 108 """ | 110 """ |
| 109 if isinstance(fields, basestring): | 111 if isinstance(fields, basestring): |
| 110 raise ValueError('Expected fields to be a non-string iterable') | 112 raise ValueError('Expected fields to be a non-string iterable') |
| 111 new_re_data = self.re_data.copy() | 113 new_re_data = self.re_data.copy() |
| 112 new_re_data[re.compile(step_name_re)] = Filter._reEntry( | 114 new_re_data[re.compile(step_name_re)] = _filterRegexEntry( |
| 113 at_least, at_most, frozenset(fields)) | 115 at_least, at_most, frozenset(fields)) |
| 114 | 116 |
| 115 ret = Filter() | 117 ret = Filter() |
| 116 ret.data = self.data | 118 ret.data = self.data |
| 117 ret.re_data = new_re_data | 119 ret.re_data = new_re_data |
| 118 return ret | 120 return ret |
| 119 | 121 |
| 120 | 122 |
| 121 def DoesNotRun(check, step_odict, *steps): | 123 def DoesNotRun(check, step_odict, *steps): |
| 122 """Asserts that the given steps don't run. | 124 """Asserts that the given steps don't run. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 187 |
| 186 def DropExpectation(_check, _step_odict): | 188 def DropExpectation(_check, _step_odict): |
| 187 """Using this post-process hook will drop the expectations for this test | 189 """Using this post-process hook will drop the expectations for this test |
| 188 completely. | 190 completely. |
| 189 | 191 |
| 190 Usage: | 192 Usage: |
| 191 yield TEST + api.post_process(DropExpectation) | 193 yield TEST + api.post_process(DropExpectation) |
| 192 | 194 |
| 193 """ | 195 """ |
| 194 return {} | 196 return {} |
| OLD | NEW |