#NEWLINE# def hash_identifier( s , length , pad = True , hasher = md5 , prefix = '' , group = None , upper = False ) : #NEWLINE# #INDENT# "\n    Hashes the string (with the given hashing module), then turns that\n    hash into an identifier of the given length (using modulo to\n    reduce the length of the identifier).  If ``pad`` is False, then\n    the minimum-length identifier will be used; otherwise the\n    identifier will be padded with 0's as necessary.\n\n    ``prefix`` will be added last, and does not count towards the\n    target length.  ``group`` will group the characters with ``-`` in\n    the given lengths, and also does not count towards the target\n    length.  E.g., ``group=4`` will cause a identifier like\n    ``a5f3-hgk3-asdf``.  Grouping occurs before the prefix.\n    " #NEWLINE# if ( not callable ( hasher ) ) : #NEWLINE# #INDENT# hasher = hasher . new #NEWLINE# #UNINDENT# if ( ( length > 26 ) and ( hasher is md5 ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'md5 cannot create hashes longer than 26 characters in length (you gave %s)' % length ) ) #NEWLINE# #UNINDENT# if isinstance ( s , six . text_type ) : #NEWLINE# #INDENT# s = s . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isinstance ( s , six . binary_type ) ) : #NEWLINE# #INDENT# s = str ( s ) #NEWLINE# if six . PY3 : #NEWLINE# #INDENT# s = s . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# h = hasher ( s ) #NEWLINE# bin_hash = h . digest ( ) #NEWLINE# modulo = ( base ** length ) #NEWLINE# number = 0 #NEWLINE# for c in list ( bin_hash ) : #NEWLINE# #INDENT# number = ( ( ( number * 256 ) + six . byte2int ( [ c ] ) ) % modulo ) #NEWLINE# #UNINDENT# ident = make_identifier ( number ) #NEWLINE# if pad : #NEWLINE# #INDENT# ident = ( ( good_characters [ 0 ] * ( length - len ( ident ) ) ) + ident ) #NEWLINE# #UNINDENT# if group : #NEWLINE# #INDENT# parts = [ ] #NEWLINE# while ident : #NEWLINE# #INDENT# parts . insert ( 0 , ident [ ( - group ) : ] ) #NEWLINE# ident = ident [ : ( - group ) ] #NEWLINE# #UNINDENT# ident = '-' . join ( parts ) #NEWLINE# #UNINDENT# if upper : #NEWLINE# #INDENT# ident = ident . upper ( ) #NEWLINE# #UNINDENT# return ( prefix + ident )