def validate_config(obj, return_bool=False):
 def infinite_defaultdict():
  return collections.defaultdict(infinite_defaultdict)
 new_config = infinite_defaultdict()
 global verbose
 verbose = False
 valid_config = True
 valid_rule = True
 tool_has_default = False
 if return_bool:
  verbose = True
 elif ((obj is not None) and ('verbose' in obj) and isinstance(obj['verbose'], bool)):
  verbose = obj['verbose']
 else:
  valid_config = False
 if ((not return_bool) and verbose):
  log.debug('Running config validation...')
 if ((not valid_config) and return_bool):
  log.debug("Missing mandatory field 'verbose' in config!")
 available_rule_types = ['file_size', 'num_input_datasets', 'records', 'arguments']
 if (obj is not None):
  if ('default_destination' in obj):
   if isinstance(obj['default_destination'], str):
    new_config['default_destination'] = obj['default_destination']
   elif isinstance(obj['default_destination'], dict):
    if (('priority' in obj['default_destination']) and isinstance(obj['default_destination']['priority'], dict)):
     if ('med' not in obj['default_destination']['priority']):
      error = "No default 'med' priority destination!"
      if verbose:
       log.debug(error)
      valid_config = False
     else:
      for priority in obj['default_destination']['priority']:
       if (priority in ['low', 'med', 'high']):
        if isinstance(obj['default_destination']['priority'][priority], str):
         new_config['default_destination']['priority'][priority] = obj['default_destination']['priority'][priority]
        else:
         error = (("No default '" + str(priority)) + "' priority destination in config!")
         if verbose:
          log.debug(error)
         valid_config = False
       else:
        error = (("Invalid default priority destination '" + str(priority)) + "' found in config!")
        if verbose:
         log.debug(error)
        valid_config = False
    else:
     error = 'No default priority destinations specified in config!'
     if verbose:
      log.debug(error)
     valid_config = False
   else:
    error = 'No global default destination specified in config!'
    if verbose:
     log.debug(error)
    valid_config = False
  else:
   error = 'No global default destination specified in config!'
   if verbose:
    log.debug(error)
   valid_config = False
  if ('users' in obj):
   if isinstance(obj['users'], dict):
    for user in obj['users']:
     curr = obj['users'][user]
     if isinstance(curr, dict):
      if (('priority' in curr) and isinstance(curr['priority'], str)):
       if (curr['priority'] in ['low', 'med', 'high']):
        new_config['users'][user]['priority'] = curr['priority']
       else:
        error = ((("User '" + user) + "', priority is not valid!") + ' Must be either low, med, or high.')
        if verbose:
         log.debug(error)
        valid_config = False
      else:
       error = (("User '" + user) + "' is missing a priority!")
       if verbose:
        log.debug(error)
       valid_config = False
     else:
      error = (("User '" + user) + "' is missing a priority!")
      if verbose:
       log.debug(error)
      valid_config = False
   else:
    error = 'Users option is not a dictionary!'
    if verbose:
     log.debug(error)
    valid_config = False
  if ('tools' in obj):
   for tool in obj['tools']:
    curr = obj['tools'][tool]
    if (not isinstance(curr, list)):
     curr_tool_rules = []
     if (curr is not None):
      if ('default_destination' in curr):
       if isinstance(curr['default_destination'], str):
        new_config['tools'][tool]['default_destination'] = curr['default_destination']
        tool_has_default = True
       elif isinstance(curr['default_destination'], dict):
        if (('priority' in curr['default_destination']) and isinstance(curr['default_destination']['priority'], dict)):
         if ('med' not in curr['default_destination']['priority']):
          error = "No default 'med' priority destination "
          error += (('for ' + str(tool)) + '!')
          if verbose:
           log.debug(error)
          valid_config = False
         else:
          for priority in curr['default_destination']['priority']:
           destination = curr['default_destination']['priority'][priority]
           if (priority in ['low', 'med', 'high']):
            if isinstance(destination, str):
             new_config['tools'][tool]['default_destination']['priority'][priority] = destination
             tool_has_default = True
            else:
             error = ((((("No default '" + str(priority)) + "' priority destination ") + 'for ') + str(tool)) + ' in config!')
             if verbose:
              log.debug(error)
             valid_config = False
           else:
            error = ((((('Invalid default priority ' + "destination '") + str(priority)) + "' for ") + str(tool)) + 'found in config!')
            if verbose:
             log.debug(error)
            valid_config = False
        else:
         error = 'No default priority destinations specified'
         error += ((' for ' + str(tool)) + ' in config!')
         if verbose:
          log.debug(error)
         valid_config = False
      if (('rules' in curr) and isinstance(curr['rules'], list)):
       curr_tool = curr
       counter = 0
       for rule in curr_tool['rules']:
        if ('rule_type' in rule):
         if (rule['rule_type'] in available_rule_types):
          validated_rule = None
          counter += 1
          if return_bool:
           valid_rule = RuleValidator.validate_rule(rule['rule_type'], return_bool, rule, counter, tool)
          else:
           validated_rule = RuleValidator.validate_rule(rule['rule_type'], return_bool, rule, counter, tool)
          if (not valid_rule):
           valid_config = False
          if ((not return_bool) and (validated_rule is not None)):
           curr_tool_rules.append(copy.deepcopy(validated_rule))
         else:
          error = "Unrecognized rule_type '"
          error += (rule['rule_type'] + "' ")
          error += (("found in '" + str(tool)) + "'. ")
          if (not return_bool):
           error += 'Ignoring...'
          if verbose:
           log.debug(error)
          valid_config = False
        else:
         counter += 1
         error = 'No rule_type found for rule '
         error += str(counter)
         error += ((" in '" + str(tool)) + "'.")
         if verbose:
          log.debug(error)
         valid_config = False
      elif (not tool_has_default):
       valid_config = False
       error = (("Tool '" + str(tool)) + "' does not have rules nor a")
       error += ' default_destination!'
       if verbose:
        log.debug(error)
     else:
      valid_config = False
      error = (("Config section for tool '" + str(tool)) + "' is blank!")
      if verbose:
       log.debug(error)
     if curr_tool_rules:
      new_config['tools'][str(tool)]['rules'] = curr_tool_rules
    else:
     error = 'Malformed YML; expected job name, '
     error += 'but found a list instead!'
     if verbose:
      log.debug(error)
     valid_config = False
  for category in obj.keys():
   if (not ((category == 'verbose') or (category == 'tools') or (category == 'default_destination') or (category == 'users'))):
    error = ("Unrecognized category '" + category)
    error += "' found in config file!"
    if verbose:
     log.debug(error)
    valid_config = False
 else:
  if verbose:
   log.debug('No (or empty) config file supplied!')
  valid_config = False
 if (not return_bool):
  if verbose:
   log.debug('Finished config validation.')
 if return_bool:
  return valid_config
 else:
  return new_config

def validate_config ( obj , return_bool = False ) : def infinite_defaultdict ( ) : return collections . defaultdict ( infinite_defaultdict ) new_config = infinite_defaultdict ( ) global verbose verbose = False valid_config = True valid_rule = True tool_has_default = False if return_bool : verbose = True elif ( ( obj is not None ) and ( 'verbose' in obj ) and isinstance ( obj [ 'verbose' ] , bool ) ) : verbose = obj [ 'verbose' ] else : valid_config = False if ( ( not return_bool ) and verbose ) : log . debug ( 'Running config validation...' ) if ( ( not valid_config ) and return_bool ) : log . debug ( "Missing mandatory field 'verbose' in config!" ) available_rule_types = [ 'file_size' , 'num_input_datasets' , 'records' , 'arguments' ] if ( obj is not None ) : if ( 'default_destination' in obj ) : if isinstance ( obj [ 'default_destination' ] , str ) : new_config [ 'default_destination' ] = obj [ 'default_destination' ] elif isinstance ( obj [ 'default_destination' ] , dict ) : if ( ( 'priority' in obj [ 'default_destination' ] ) and isinstance ( obj [ 'default_destination' ] [ 'priority' ] , dict ) ) : if ( 'med' not in obj [ 'default_destination' ] [ 'priority' ] ) : error = "No default 'med' priority destination!" if verbose : log . debug ( error ) valid_config = False else : for priority in obj [ 'default_destination' ] [ 'priority' ] : if ( priority in [ 'low' , 'med' , 'high' ] ) : if isinstance ( obj [ 'default_destination' ] [ 'priority' ] [ priority ] , str ) : new_config [ 'default_destination' ] [ 'priority' ] [ priority ] = obj [ 'default_destination' ] [ 'priority' ] [ priority ] else : error = ( ( "No default '" + str ( priority ) ) + "' priority destination in config!" ) if verbose : log . debug ( error ) valid_config = False else : error = ( ( "Invalid default priority destination '" + str ( priority ) ) + "' found in config!" ) if verbose : log . debug ( error ) valid_config = False else : error = 'No default priority destinations specified in config!' if verbose : log . debug ( error ) valid_config = False else : error = 'No global default destination specified in config!' if verbose : log . debug ( error ) valid_config = False else : error = 'No global default destination specified in config!' if verbose : log . debug ( error ) valid_config = False if ( 'users' in obj ) : if isinstance ( obj [ 'users' ] , dict ) : for user in obj [ 'users' ] : curr = obj [ 'users' ] [ user ] if isinstance ( curr , dict ) : if ( ( 'priority' in curr ) and isinstance ( curr [ 'priority' ] , str ) ) : if ( curr [ 'priority' ] in [ 'low' , 'med' , 'high' ] ) : new_config [ 'users' ] [ user ] [ 'priority' ] = curr [ 'priority' ] else : error = ( ( ( "User '" + user ) + "', priority is not valid!" ) + ' Must be either low, med, or high.' ) if verbose : log . debug ( error ) valid_config = False else : error = ( ( "User '" + user ) + "' is missing a priority!" ) if verbose : log . debug ( error ) valid_config = False else : error = ( ( "User '" + user ) + "' is missing a priority!" ) if verbose : log . debug ( error ) valid_config = False else : error = 'Users option is not a dictionary!' if verbose : log . debug ( error ) valid_config = False if ( 'tools' in obj ) : for tool in obj [ 'tools' ] : curr = obj [ 'tools' ] [ tool ] if ( not isinstance ( curr , list ) ) : curr_tool_rules = [ ] if ( curr is not None ) : if ( 'default_destination' in curr ) : if isinstance ( curr [ 'default_destination' ] , str ) : new_config [ 'tools' ] [ tool ] [ 'default_destination' ] = curr [ 'default_destination' ] tool_has_default = True elif isinstance ( curr [ 'default_destination' ] , dict ) : if ( ( 'priority' in curr [ 'default_destination' ] ) and isinstance ( curr [ 'default_destination' ] [ 'priority' ] , dict ) ) : if ( 'med' not in curr [ 'default_destination' ] [ 'priority' ] ) : error = "No default 'med' priority destination " error += ( ( 'for ' + str ( tool ) ) + '!' ) if verbose : log . debug ( error ) valid_config = False else : for priority in curr [ 'default_destination' ] [ 'priority' ] : destination = curr [ 'default_destination' ] [ 'priority' ] [ priority ] if ( priority in [ 'low' , 'med' , 'high' ] ) : if isinstance ( destination , str ) : new_config [ 'tools' ] [ tool ] [ 'default_destination' ] [ 'priority' ] [ priority ] = destination tool_has_default = True else : error = ( ( ( ( ( "No default '" + str ( priority ) ) + "' priority destination " ) + 'for ' ) + str ( tool ) ) + ' in config!' ) if verbose : log . debug ( error ) valid_config = False else : error = ( ( ( ( ( 'Invalid default priority ' + "destination '" ) + str ( priority ) ) + "' for " ) + str ( tool ) ) + 'found in config!' ) if verbose : log . debug ( error ) valid_config = False else : error = 'No default priority destinations specified' error += ( ( ' for ' + str ( tool ) ) + ' in config!' ) if verbose : log . debug ( error ) valid_config = False if ( ( 'rules' in curr ) and isinstance ( curr [ 'rules' ] , list ) ) : curr_tool = curr counter = 0 for rule in curr_tool [ 'rules' ] : if ( 'rule_type' in rule ) : if ( rule [ 'rule_type' ] in available_rule_types ) : validated_rule = None counter += 1 if return_bool : valid_rule = RuleValidator . validate_rule ( rule [ 'rule_type' ] , return_bool , rule , counter , tool ) else : validated_rule = RuleValidator . validate_rule ( rule [ 'rule_type' ] , return_bool , rule , counter , tool ) if ( not valid_rule ) : valid_config = False if ( ( not return_bool ) and ( validated_rule is not None ) ) : curr_tool_rules . append ( copy . deepcopy ( validated_rule ) ) else : error = "Unrecognized rule_type '" error += ( rule [ 'rule_type' ] + "' " ) error += ( ( "found in '" + str ( tool ) ) + "'. " ) if ( not return_bool ) : error += 'Ignoring...' if verbose : log . debug ( error ) valid_config = False else : counter += 1 error = 'No rule_type found for rule ' error += str ( counter ) error += ( ( " in '" + str ( tool ) ) + "'." ) if verbose : log . debug ( error ) valid_config = False elif ( not tool_has_default ) : valid_config = False error = ( ( "Tool '" + str ( tool ) ) + "' does not have rules nor a" ) error += ' default_destination!' if verbose : log . debug ( error ) else : valid_config = False error = ( ( "Config section for tool '" + str ( tool ) ) + "' is blank!" ) if verbose : log . debug ( error ) if curr_tool_rules : new_config [ 'tools' ] [ str ( tool ) ] [ 'rules' ] = curr_tool_rules else : error = 'Malformed YML; expected job name, ' error += 'but found a list instead!' if verbose : log . debug ( error ) valid_config = False for category in obj . keys ( ) : if ( not ( ( category == 'verbose' ) or ( category == 'tools' ) or ( category == 'default_destination' ) or ( category == 'users' ) ) ) : error = ( "Unrecognized category '" + category ) error += "' found in config file!" if verbose : log . debug ( error ) valid_config = False else : if verbose : log . debug ( 'No (or empty) config file supplied!' ) valid_config = False if ( not return_bool ) : if verbose : log . debug ( 'Finished config validation.' ) if return_bool : return valid_config else : return new_config
validate universal media player configuration .