Revision 6de7c41d lib/cmdlib.py

b/lib/cmdlib.py
32 32
import platform
33 33
import logging
34 34
import copy
35
import itertools
35 36

  
36 37
from ganeti import ssh
37 38
from ganeti import utils
......
322 323
  HTYPE = None
323 324

  
324 325

  
326
class _FieldSet(object):
327
  """A simple field set.
328

  
329
  Among the features are:
330
    - checking if a string is among a list of static string or regex objects
331
    - checking if a whole list of string matches
332
    - returning the matching groups from a regex match
333

  
334
  Internally, all fields are held as regular expression objects.
335

  
336
  """
337
  def __init__(self, *items):
338
    self.items = [re.compile("^%s$" % value) for value in items]
339

  
340
  def Extend(self, other_set):
341
    """Extend the field set with the items from another one"""
342
    self.items.extend(other_set.items)
343

  
344
  def Matches(self, field):
345
    """Checks if a field matches the current set
346

  
347
    @type field: str
348
    @param field: the string to match
349
    @return: either False or a regular expression match object
350

  
351
    """
352
    for m in itertools.ifilter(None, (val.match(field) for val in self.items)):
353
      return m
354
    return False
355

  
356
  def NonMatching(self, items):
357
    """Returns the list of fields not matching the current set
358

  
359
    @type items: list
360
    @param items: the list of fields to check
361
    @rtype: list
362
    @return: list of non-matching fields
363

  
364
    """
365
    return [val for val in items if not self.Matches(val)]
366

  
367

  
325 368
def _GetWantedNodes(lu, nodes):
326 369
  """Returns list of checked and expanded node names.
327 370

  

Also available in: Unified diff