Revision 31e22135 lib/utils.py

b/lib/utils.py
373 373
  return ret
374 374

  
375 375

  
376
def CheckDict(target, template, logname=None):
377
  """Ensure a dictionary has a required set of keys.
378

  
379
  For the given dictionaries I{target} and I{template}, ensure
380
  I{target} has all the keys from I{template}. Missing keys are added
381
  with values from template.
382

  
383
  @type target: dict
384
  @param target: the dictionary to update
385
  @type template: dict
386
  @param template: the dictionary holding the default values
387
  @type logname: str or None
388
  @param logname: if not None, causes the missing keys to be
389
      logged with this name
390

  
391
  """
392
  missing = []
393
  for k in template:
394
    if k not in target:
395
      missing.append(k)
396
      target[k] = template[k]
397

  
398
  if missing and logname:
399
    logging.warning('%s missing keys %s', logname, ', '.join(missing))
400

  
401

  
402 376
def ForceDictType(target, key_types, allowed_values=None):
403 377
  """Force the values of a dict to have certain types.
404 378

  

Also available in: Unified diff