def _resolve_datacenter(dc, pillarenv):
 log.debug('Resolving Consul datacenter based on: %s', dc)
 try:
  mappings = dc.items()
 except AttributeError:
  log.debug("Using pre-defined DC: '%s'", dc)
  return dc
 log.debug('Selecting DC based on pillarenv using %d pattern(s)', len(mappings))
 log.debug("Pillarenv set to '%s'", pillarenv)
 sorted_mappings = sorted(mappings, key=(lambda m: ((- len(m[0])), m[0])))
 for (pattern, target) in sorted_mappings:
  match = re.match(pattern, pillarenv)
  if match:
   log.debug("Matched pattern: '%s'", pattern)
   result = target.format(**match.groupdict())
   log.debug("Resolved datacenter: '%s'", result)
   return result
 log.debug('None of following patterns matched pillarenv=%s: %s', pillarenv, ', '.join((repr(x) for x in mappings)))

def _resolve_datacenter ( dc , pillarenv ) : log . debug ( 'Resolving Consul datacenter based on: %s' , dc ) try : mappings = dc . items ( ) except AttributeError : log . debug ( "Using pre-defined DC: '%s'" , dc ) return dc log . debug ( 'Selecting DC based on pillarenv using %d pattern(s)' , len ( mappings ) ) log . debug ( "Pillarenv set to '%s'" , pillarenv ) sorted_mappings = sorted ( mappings , key = ( lambda m : ( ( - len ( m [ 0 ] ) ) , m [ 0 ] ) ) ) for ( pattern , target ) in sorted_mappings : match = re . match ( pattern , pillarenv ) if match : log . debug ( "Matched pattern: '%s'" , pattern ) result = target . format ( ** match . groupdict ( ) ) log . debug ( "Resolved datacenter: '%s'" , result ) return result log . debug ( 'None of following patterns matched pillarenv=%s: %s' , pillarenv , ', ' . join ( ( repr ( x ) for x in mappings ) ) )
if dc is a string - return it as is .