forked from release-engineering/pubtools-pulplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
criteria.py
281 lines (210 loc) · 7.25 KB
/
criteria.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import collections
import re
import warnings
import six
# Due to below block:
# pylint: disable=wrong-import-position
try:
# python 3
Iterable = collections.abc.Iterable # pylint: disable=invalid-name
except AttributeError: # pragma: no cover
# python 2
Iterable = collections.Iterable # pylint: disable=invalid-name
from pubtools.pulplib._impl import compat_attr as attr
class Criteria(object):
"""Represents a Pulp search criteria.
This is an opaque class which is not intended to be created
or used directly. Instances of this class should be obtained and
composed by calls to the documented class methods.
Example:
.. code-block:: python
# With Pulp 2.x / mongo, this is roughly equivalent
# to search fragment:
#
# {"notes.my-field": {"$exists": True},
# "notes.other-field": {"$eq": ["a", "b", "c"]}}
#
crit = Criteria.and_(
Criteria.with_field('notes.my-field', Matcher.exists()),
Criteria.with_field('notes.other-field', ["a", "b", "c"])
)
# criteria may now be used with client to execute a search
repos = client.search_repository(crit)
"""
exists = object()
# exists is undocumented and deprecated, use Matcher.exists() instead
@classmethod
def with_id(cls, ids):
"""Args:
ids (str, list[str])
An id or list of ids
Returns:
Criteria
criteria for finding objects matching the given ID(s)
"""
if isinstance(ids, six.string_types):
return cls.with_field("id", ids)
return cls.with_field_in("id", ids)
@classmethod
def with_field(cls, field_name, field_value):
"""Args:
field_name (str)
The name of a field.
Field names may contain a "." to indicate nested fields,
such as ``notes.created``.
field_value
:class:`Matcher`
A matcher to be applied against the field.
object
Any value, to be matched against the field via
:meth:`Matcher.equals`.
Returns:
Criteria
criteria for finding objects where ``field_name`` is present and
matches ``field_value``.
"""
return FieldMatchCriteria(field_name, field_value)
@classmethod
def with_field_in(cls, field_name, field_value):
warnings.warn(
"with_field_in is deprecated, use Matcher.in_() instead", DeprecationWarning
)
return cls.with_field(field_name, Matcher.in_(field_value))
@classmethod
def and_(cls, *criteria):
"""Args:
criteria (list[Criteria])
Any number of criteria.
Returns:
:class:`Criteria`
criteria for finding objects which satisfy all of the input ``criteria``.
"""
return AndCriteria(criteria)
@classmethod
def or_(cls, *criteria):
"""Args:
criteria (list[Criteria])
Any number of criteria.
Returns:
Criteria
criteria for finding objects which satisfy any of the input ``criteria``.
"""
return OrCriteria(criteria)
@classmethod
def true(cls):
"""
Returns:
Criteria
a criteria which always matches any object.
"""
return TrueCriteria()
class Matcher(object):
"""Methods for matching fields within a Pulp search query.
Instances of this class are created by the documented class methods,
and should be used in conjunction with :class:`Criteria` methods, such
as :meth:`Criteria.with_field`.
.. versionadded:: 1.1.0
"""
@classmethod
def equals(cls, value):
"""
Matcher for a field which must equal exactly the given value.
Arguments:
value (object)
An object to match against a field.
"""
return EqMatcher(value)
@classmethod
def regex(cls, pattern):
"""
Matcher for a field which must be a string and must match the given
regular expression.
Arguments:
pattern (str)
A regular expression to match against the field.
The expression is not implicitly anchored.
.. warning::
It is not defined which specific regular expression syntax is
supported. For portable code, callers are recommended to use
only the common subset of PCRE-compatible and Python-compatible
regular expressions.
Raises:
:class:`re.error`
If the given pattern is not a valid regular expression.
Example:
.. code-block:: python
# Would match any Repository where notes.my-field starts
# with "abc"
crit = Criteria.with_field('notes.my-field', Matcher.regex("^abc"))
"""
return RegexMatcher(pattern)
@classmethod
def exists(cls):
"""
Matcher for a field which must exist, with no specific value.
Example:
.. code-block:: python
# Would match any Repository where notes.my-field exists
crit = Criteria.with_field('notes.my-field', Matcher.exists())
"""
return ExistsMatcher()
@classmethod
def in_(cls, values):
"""
Returns a matcher for a field whose value equals one of the specified
input values.
Arguments:
values (iterable)
An iterable of values used to match a field.
Example:
.. code-block:: python
# Would match any Repository where notes.my-field is "a", "b" or "c"
crit = Criteria.with_field(
'notes.my-field',
Matcher.in_(["a", "b", "c"])
)
"""
return InMatcher(values)
@attr.s
class RegexMatcher(Matcher):
_pattern = attr.ib()
@_pattern.validator
def _check_pattern(self, _, pattern):
re.compile(pattern)
@attr.s
class EqMatcher(Matcher):
_value = attr.ib()
@attr.s
class InMatcher(Matcher):
_values = attr.ib()
@_values.validator
def _check_values(self, _, values):
if isinstance(values, Iterable) and not isinstance(values, six.string_types):
return
raise ValueError("Must be an iterable: %s" % repr(values))
@attr.s
class ExistsMatcher(Matcher):
pass
def coerce_to_matcher(value):
if isinstance(value, Matcher):
return value
if value is Criteria.exists:
warnings.warn(
"Criteria.exists is deprecated, use Matcher.exists() instead",
DeprecationWarning,
stacklevel=2,
)
return ExistsMatcher()
return EqMatcher(value)
@attr.s
class FieldMatchCriteria(Criteria):
_field = attr.ib()
_matcher = attr.ib(converter=coerce_to_matcher)
@attr.s
class AndCriteria(Criteria):
_operands = attr.ib()
@attr.s
class OrCriteria(Criteria):
_operands = attr.ib()
class TrueCriteria(Criteria):
pass