Revision 3f2f55bb lib/qlang.py
b/lib/qlang.py | ||
---|---|---|
32 | 32 |
""" |
33 | 33 |
|
34 | 34 |
import re |
35 |
import string # pylint: disable-msg=W0402 |
|
35 | 36 |
|
36 | 37 |
import pyparsing as pyp |
37 | 38 |
|
38 | 39 |
from ganeti import errors |
40 |
from ganeti import netutils |
|
39 | 41 |
|
40 | 42 |
|
41 | 43 |
# Logic operators with one or more operands, each of which is a filter on its |
... | ... | |
57 | 59 |
OP_CONTAINS = "=[]" |
58 | 60 |
|
59 | 61 |
|
62 |
#: Characters used for detecting user-written filters (see L{MaybeFilter}) |
|
63 |
FILTER_DETECTION_CHARS = frozenset("()=/!~" + string.whitespace) |
|
64 |
|
|
65 |
|
|
60 | 66 |
def MakeSimpleFilter(namefield, values): |
61 | 67 |
"""Builds simple a filter. |
62 | 68 |
|
... | ... | |
220 | 226 |
except pyp.ParseBaseException, err: |
221 | 227 |
raise errors.QueryFilterParseError("Failed to parse query filter" |
222 | 228 |
" '%s': %s" % (text, err), err) |
229 |
|
|
230 |
|
|
231 |
def MaybeFilter(text): |
|
232 |
"""Try to determine if a string is a filter or a name. |
|
233 |
|
|
234 |
If in doubt, this function treats a text as a name. |
|
235 |
|
|
236 |
@type text: string |
|
237 |
@param text: String to be examined |
|
238 |
@rtype: bool |
|
239 |
|
|
240 |
""" |
|
241 |
# Quick check for punctuation and whitespace |
|
242 |
if frozenset(text) & FILTER_DETECTION_CHARS: |
|
243 |
return True |
|
244 |
|
|
245 |
try: |
|
246 |
netutils.Hostname.GetNormalizedName(text) |
|
247 |
except errors.OpPrereqError: |
|
248 |
# Not a valid hostname, treat as filter |
|
249 |
return True |
|
250 |
|
|
251 |
# Most probably a name |
|
252 |
return False |
Also available in: Unified diff