#NEWLINE# def get_best_match( target_name , names ) : #NEWLINE# #INDENT# 'Finds the best match for target_name out of names using the Nginx\n    name-matching rules (exact > longest wildcard starting with * >\n    longest wildcard ending with * > regex).\n\n    :param str target_name: The name to match\n    :param set names: The candidate server names\n    :returns: Tuple of (type of match, the name that matched)\n    :rtype: tuple\n\n    ' #NEWLINE# exact = [ ] #NEWLINE# wildcard_start = [ ] #NEWLINE# wildcard_end = [ ] #NEWLINE# regex = [ ] #NEWLINE# for name in names : #NEWLINE# #INDENT# if _exact_match ( target_name , name ) : #NEWLINE# #INDENT# exact . append ( name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _wildcard_match ( target_name , name , True ) : #NEWLINE# #INDENT# wildcard_start . append ( name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _wildcard_match ( target_name , name , False ) : #NEWLINE# #INDENT# wildcard_end . append ( name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if _regex_match ( target_name , name ) : #NEWLINE# #INDENT# regex . append ( name ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( len ( exact ) > 0 ) : #NEWLINE# #INDENT# match = min ( exact , key = len ) #NEWLINE# return ( 'exact' , match ) #NEWLINE# #UNINDENT# if ( len ( wildcard_start ) > 0 ) : #NEWLINE# #INDENT# match = max ( wildcard_start , key = len ) #NEWLINE# return ( 'wildcard_start' , match ) #NEWLINE# #UNINDENT# if ( len ( wildcard_end ) > 0 ) : #NEWLINE# #INDENT# match = max ( wildcard_end , key = len ) #NEWLINE# return ( 'wildcard_end' , match ) #NEWLINE# #UNINDENT# if ( len ( regex ) > 0 ) : #NEWLINE# #INDENT# match = regex [ 0 ] #NEWLINE# return ( 'regex' , match ) #NEWLINE# #UNINDENT# return ( None , None )