|
17 | 17 | import struct |
18 | 18 |
|
19 | 19 |
|
20 | | -from google.cloud.bigtable.data.row_filters import ( |
| 20 | +from google.cloud.bigtable.data.row_filters import ( # noqa: F401 |
21 | 21 | RowFilter, |
22 | 22 | SinkFilter, |
23 | | - _BoolFilter as _BaseBoolFilter, |
| 23 | + _BoolFilter, |
24 | 24 | PassAllFilter, |
25 | 25 | BlockAllFilter, |
26 | | - _RegexFilter as _BaseRegexFilter, |
| 26 | + _RegexFilter, |
27 | 27 | RowKeyRegexFilter, |
28 | 28 | RowSampleFilter, |
29 | 29 | FamilyNameRegexFilter, |
|
33 | 33 | ColumnRangeFilter as BaseColumnRangeFilter, |
34 | 34 | ValueRegexFilter, |
35 | 35 | ValueRangeFilter, |
36 | | - _CellCountFilter as _BaseCellCountFilter, |
| 36 | + _CellCountFilter, |
37 | 37 | CellsRowOffsetFilter, |
38 | 38 | CellsRowLimitFilter, |
39 | 39 | CellsColumnLimitFilter, |
40 | 40 | StripValueTransformerFilter, |
41 | 41 | ApplyLabelFilter, |
42 | | - _FilterCombination as _BaseFilterCombination, |
| 42 | + _FilterCombination, |
43 | 43 | RowFilterChain, |
44 | 44 | RowFilterUnion, |
45 | 45 | ConditionalRowFilter as BaseConditionalRowFilter, |
46 | 46 | ) |
47 | 47 |
|
48 | 48 | _PACK_I64 = struct.Struct(">q").pack |
49 | 49 |
|
50 | | -# The classes defined below are to provide constructors and members |
51 | | -# that have an interface that does not match the one used by the data |
52 | | -# client, for backwards compatibility purposes. |
53 | | - |
54 | | -# Each underscored class is an ABC. Make them into classes that can be |
55 | | -# instantiated with a placeholder to_dict method for consistency. |
56 | 50 |
|
| 51 | +class _MappableAttributesMixin: |
| 52 | + """ |
| 53 | + Mixin for classes that need some of their attribute names remapped. |
57 | 54 |
|
58 | | -class _BoolFilter(_BaseBoolFilter): |
59 | | - """Row filter that uses a boolean flag. |
| 55 | + This is for taking some of the classes from the data client row filters |
| 56 | + that are 1:1 with their legacy client counterparts but with some of their |
| 57 | + attributes renamed. To use in a class, override the base class with this mixin |
| 58 | + class and define a map _attribute_map from legacy client attributes to data client |
| 59 | + attributes. |
60 | 60 |
|
61 | | - :type flag: bool |
62 | | - :param flag: An indicator if a setting is turned on or off. |
| 61 | + Attributes are remapped and redefined in __init__ as well as getattr/setattr. |
63 | 62 | """ |
64 | 63 |
|
65 | | - def _to_dict(self): |
66 | | - pass |
67 | | - |
| 64 | + def __init__(self, *args, **kwargs): |
| 65 | + new_kwargs = {self._attribute_map.get(k, k): v for (k, v) in kwargs.items()} |
| 66 | + super(_MappableAttributesMixin, self).__init__(*args, **new_kwargs) |
68 | 67 |
|
69 | | -class _RegexFilter(_BaseRegexFilter): |
70 | | - """Row filter that uses a regular expression. |
| 68 | + def __getattr__(self, name): |
| 69 | + if name not in self._attribute_map: |
| 70 | + raise AttributeError |
| 71 | + return getattr(self, self._attribute_map[name]) |
71 | 72 |
|
72 | | - The ``regex`` must be valid RE2 patterns. See Google's |
73 | | - `RE2 reference`_ for the accepted syntax. |
| 73 | + def __setattr__(self, name, value): |
| 74 | + attribute = self._attribute_map.get(name, name) |
| 75 | + super(_MappableAttributesMixin, self).__setattr__(attribute, value) |
74 | 76 |
|
75 | | - .. _RE2 reference: https://github.com/google/re2/wiki/Syntax |
76 | 77 |
|
77 | | - :type regex: bytes or str |
78 | | - :param regex: |
79 | | - A regular expression (RE2) for some row filter. String values |
80 | | - will be encoded as ASCII. |
81 | | - """ |
| 78 | +# The classes defined below are to provide constructors and members |
| 79 | +# that have an interface that does not match the one used by the data |
| 80 | +# client, for backwards compatibility purposes. |
82 | 81 |
|
83 | | - def _to_dict(self): |
84 | | - pass |
| 82 | +# Each underscored class is an ABC. Make them into classes that can be |
| 83 | +# instantiated with a placeholder to_dict method for consistency. |
85 | 84 |
|
86 | 85 |
|
87 | 86 | class TimestampRangeFilter(BaseTimestampRangeFilter): |
@@ -111,21 +110,7 @@ def __init__(self, value): |
111 | 110 | super(ExactValueFilter, self).__init__(value) |
112 | 111 |
|
113 | 112 |
|
114 | | -class _CellCountFilter(_BaseCellCountFilter): |
115 | | - """Row filter that uses an integer count of cells. |
116 | | -
|
117 | | - The cell count is used as an offset or a limit for the number |
118 | | - of results returned. |
119 | | -
|
120 | | - :type num_cells: int |
121 | | - :param num_cells: An integer count / offset / limit. |
122 | | - """ |
123 | | - |
124 | | - def _to_dict(self): |
125 | | - pass |
126 | | - |
127 | | - |
128 | | -class ColumnRangeFilter(BaseColumnRangeFilter): |
| 113 | +class ColumnRangeFilter(_MappableAttributesMixin, BaseColumnRangeFilter): |
129 | 114 | """A row filter to restrict to a range of columns. |
130 | 115 |
|
131 | 116 | Both the start and end column can be included or excluded in the range. |
@@ -162,63 +147,14 @@ class ColumnRangeFilter(BaseColumnRangeFilter): |
162 | 147 | is set but no ``end_column`` is given |
163 | 148 | """ |
164 | 149 |
|
165 | | - def __init__( |
166 | | - self, |
167 | | - column_family_id, |
168 | | - start_column=None, |
169 | | - end_column=None, |
170 | | - inclusive_start=None, |
171 | | - inclusive_end=None, |
172 | | - ): |
173 | | - super(ColumnRangeFilter, self).__init__( |
174 | | - family_id=column_family_id, |
175 | | - start_qualifier=start_column, |
176 | | - end_qualifier=end_column, |
177 | | - inclusive_start=inclusive_start, |
178 | | - inclusive_end=inclusive_end, |
179 | | - ) |
180 | | - |
181 | | - @property |
182 | | - def column_family_id(self): |
183 | | - return self.family_id |
184 | | - |
185 | | - @column_family_id.setter |
186 | | - def column_family_id(self, column_family_id): |
187 | | - self.family_id = column_family_id |
188 | | - |
189 | | - @property |
190 | | - def start_column(self): |
191 | | - return self.start_qualifier |
192 | | - |
193 | | - @start_column.setter |
194 | | - def start_column(self, start_column): |
195 | | - self.start_qualifier = start_column |
196 | | - |
197 | | - @property |
198 | | - def end_column(self): |
199 | | - return self.end_qualifier |
200 | | - |
201 | | - @end_column.setter |
202 | | - def end_column(self, end_column): |
203 | | - self.end_qualifier = end_column |
204 | | - |
205 | | - |
206 | | -class _FilterCombination(_BaseFilterCombination): |
207 | | - """Chain of row filters. |
208 | | -
|
209 | | - Sends rows through several filters in sequence. The filters are "chained" |
210 | | - together to process a row. After the first filter is applied, the second |
211 | | - is applied to the filtered output and so on for subsequent filters. |
212 | | -
|
213 | | - :type filters: list |
214 | | - :param filters: List of :class:`RowFilter` |
215 | | - """ |
| 150 | + _attribute_map = { |
| 151 | + "column_family_id": "family_id", |
| 152 | + "start_column": "start_qualifier", |
| 153 | + "end_column": "end_qualifier", |
| 154 | + } |
216 | 155 |
|
217 | | - def _to_dict(self): |
218 | | - pass |
219 | 156 |
|
220 | | - |
221 | | -class ConditionalRowFilter(BaseConditionalRowFilter): |
| 157 | +class ConditionalRowFilter(_MappableAttributesMixin, BaseConditionalRowFilter): |
222 | 158 | """Conditional row filter which exhibits ternary behavior. |
223 | 159 |
|
224 | 160 | Executes one of two filters based on another filter. If the ``base_filter`` |
@@ -248,36 +184,30 @@ class ConditionalRowFilter(BaseConditionalRowFilter): |
248 | 184 | will be returned in the false case. |
249 | 185 | """ |
250 | 186 |
|
251 | | - @property |
252 | | - def base_filter(self): |
253 | | - return self.predicate_filter |
254 | | - |
255 | | - @base_filter.setter |
256 | | - def base_filter(self, value: RowFilter): |
257 | | - self.predicate_filter = value |
| 187 | + _attribute_map = {"base_filter": "predicate_filter"} |
258 | 188 |
|
259 | 189 |
|
260 | 190 | __all__ = ( |
261 | | - RowFilter, |
262 | | - SinkFilter, |
263 | | - PassAllFilter, |
264 | | - BlockAllFilter, |
265 | | - RowKeyRegexFilter, |
266 | | - RowSampleFilter, |
267 | | - FamilyNameRegexFilter, |
268 | | - ColumnQualifierRegexFilter, |
269 | | - TimestampRange, |
270 | | - TimestampRangeFilter, |
271 | | - ColumnRangeFilter, |
272 | | - ValueRegexFilter, |
273 | | - ExactValueFilter, |
274 | | - ValueRangeFilter, |
275 | | - CellsRowOffsetFilter, |
276 | | - CellsRowLimitFilter, |
277 | | - CellsColumnLimitFilter, |
278 | | - StripValueTransformerFilter, |
279 | | - ApplyLabelFilter, |
280 | | - RowFilterChain, |
281 | | - RowFilterUnion, |
282 | | - ConditionalRowFilter, |
| 191 | + "RowFilter", |
| 192 | + "SinkFilter", |
| 193 | + "PassAllFilter", |
| 194 | + "BlockAllFilter", |
| 195 | + "RowKeyRegexFilter", |
| 196 | + "RowSampleFilter", |
| 197 | + "FamilyNameRegexFilter", |
| 198 | + "ColumnQualifierRegexFilter", |
| 199 | + "TimestampRange", |
| 200 | + "TimestampRangeFilter", |
| 201 | + "ColumnRangeFilter", |
| 202 | + "ValueRegexFilter", |
| 203 | + "ExactValueFilter", |
| 204 | + "ValueRangeFilter", |
| 205 | + "CellsRowOffsetFilter", |
| 206 | + "CellsRowLimitFilter", |
| 207 | + "CellsColumnLimitFilter", |
| 208 | + "StripValueTransformerFilter", |
| 209 | + "ApplyLabelFilter", |
| 210 | + "RowFilterChain", |
| 211 | + "RowFilterUnion", |
| 212 | + "ConditionalRowFilter", |
283 | 213 | ) |
0 commit comments