Revision f8638e28 lib/qlang.py
b/lib/qlang.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
import re |
35 | 35 |
import string # pylint: disable-msg=W0402 |
36 |
import logging |
|
36 | 37 |
|
37 | 38 |
import pyparsing as pyp |
38 | 39 |
|
39 | 40 |
from ganeti import errors |
40 | 41 |
from ganeti import netutils |
41 | 42 |
from ganeti import utils |
43 |
from ganeti import compat |
|
42 | 44 |
|
43 | 45 |
|
44 | 46 |
# Logic operators with one or more operands, each of which is a filter on its |
... | ... | |
61 | 63 |
|
62 | 64 |
|
63 | 65 |
#: Characters used for detecting user-written filters (see L{MaybeFilter}) |
64 |
FILTER_DETECTION_CHARS = frozenset("()=/!~" + string.whitespace) |
|
66 |
FILTER_DETECTION_CHARS = frozenset("()=/!~'\"\\" + string.whitespace) |
|
67 |
|
|
68 |
#: Characters used to detect globbing filters (see L{MaybeGlobbing}) |
|
69 |
GLOB_DETECTION_CHARS = frozenset("*?") |
|
65 | 70 |
|
66 | 71 |
|
67 | 72 |
def MakeSimpleFilter(namefield, values): |
... | ... | |
233 | 238 |
@rtype: list |
234 | 239 |
|
235 | 240 |
""" |
241 |
logging.debug("Parsing as query filter: %s", text) |
|
242 |
|
|
236 | 243 |
if parser is None: |
237 | 244 |
parser = BuildFilterParser() |
238 | 245 |
|
... | ... | |
243 | 250 |
" '%s': %s" % (text, err), err) |
244 | 251 |
|
245 | 252 |
|
246 |
def MaybeFilter(text):
|
|
247 |
"""Try to determine if a string is a filter or a name.
|
|
253 |
def _IsHostname(text):
|
|
254 |
"""Checks if a string could be a hostname.
|
|
248 | 255 |
|
249 |
If in doubt, this function treats a text as a name. |
|
250 |
|
|
251 |
@type text: string |
|
252 |
@param text: String to be examined |
|
253 | 256 |
@rtype: bool |
254 | 257 |
|
255 | 258 |
""" |
256 |
# Quick check for punctuation and whitespace |
|
257 |
if frozenset(text) & FILTER_DETECTION_CHARS: |
|
258 |
return True |
|
259 |
|
|
260 | 259 |
try: |
261 | 260 |
netutils.Hostname.GetNormalizedName(text) |
262 | 261 |
except errors.OpPrereqError: |
263 |
# Not a valid hostname, treat as filter |
|
262 |
return False |
|
263 |
else: |
|
264 | 264 |
return True |
265 | 265 |
|
266 |
# Most probably a name |
|
267 |
return False |
|
266 |
|
|
267 |
def _CheckFilter(text): |
|
268 |
"""CHecks if a string could be a filter. |
|
269 |
|
|
270 |
@rtype: bool |
|
271 |
|
|
272 |
""" |
|
273 |
return bool(frozenset(text) & FILTER_DETECTION_CHARS) |
|
274 |
|
|
275 |
|
|
276 |
def _CheckGlobbing(text): |
|
277 |
"""Checks if a string could be a globbing pattern. |
|
278 |
|
|
279 |
@rtype: bool |
|
280 |
|
|
281 |
""" |
|
282 |
return bool(frozenset(text) & GLOB_DETECTION_CHARS) |
|
283 |
|
|
284 |
|
|
285 |
def _MakeFilterPart(namefield, text): |
|
286 |
"""Generates filter for one argument. |
|
287 |
|
|
288 |
""" |
|
289 |
if _CheckGlobbing(text): |
|
290 |
return [OP_REGEXP, namefield, utils.DnsNameGlobPattern(text)] |
|
291 |
else: |
|
292 |
return [OP_EQUAL, namefield, text] |
|
293 |
|
|
294 |
|
|
295 |
def MakeFilter(args, force_filter): |
|
296 |
"""Try to make a filter from arguments to a command. |
|
297 |
|
|
298 |
If the name could be a filter it is parsed as such. If it's just a globbing |
|
299 |
pattern, e.g. "*.site", such a filter is constructed. As a last resort the |
|
300 |
names are treated just as a plain name filter. |
|
301 |
|
|
302 |
@type args: list of string |
|
303 |
@param args: Arguments to command |
|
304 |
@type force_filter: bool |
|
305 |
@param force_filter: Whether to force treatment as a full-fledged filter |
|
306 |
@rtype: list |
|
307 |
@return: Query filter |
|
308 |
|
|
309 |
""" |
|
310 |
if (force_filter or |
|
311 |
(args and len(args) == 1 and _CheckFilter(args[0]))): |
|
312 |
try: |
|
313 |
(filter_text, ) = args |
|
314 |
except (TypeError, ValueError): |
|
315 |
raise errors.OpPrereqError("Exactly one argument must be given as a" |
|
316 |
" filter") |
|
317 |
|
|
318 |
result = ParseFilter(filter_text) |
|
319 |
elif args: |
|
320 |
result = [OP_OR] + map(compat.partial(_MakeFilterPart, "name"), args) |
|
321 |
else: |
|
322 |
result = None |
|
323 |
|
|
324 |
return result |
Also available in: Unified diff