#NEWLINE# def _key_string( key , key_prefix = '' , server_to_user_dict = None ) : #NEWLINE# #INDENT# "Utility function to handle different ways of requesting keys.\n\n  Args:\n    key: Either a string or tuple of (shard_number, string).  In Google App\n      Engine the sharding is automatic so the shard number is ignored.\n      To memcache, the key is just bytes (no defined encoding).\n    key_prefix: Optional string prefix to prepend to key.\n    server_to_user_dict: Optional dictionary to populate with a mapping of\n      server-side key (which includes the key_prefix) to user-supplied key\n      (which does not have the prefix).\n\n  Returns:\n    The key as a non-unicode string prepended with key_prefix. This is\n    the key sent to and stored by the server. If the resulting key is\n    longer then MAX_KEY_SIZE, it will be hashed with sha1 and will be\n    replaced with the hex representation of the said hash.\n\n  Raises:\n    TypeError: If provided key isn't a string or tuple of (int, string)\n      or key_prefix.\n  " #NEWLINE# if _is_pair ( key ) : #NEWLINE# #INDENT# key = key [ 1 ] #NEWLINE# #UNINDENT# if ( not isinstance ( key , basestring ) ) : #NEWLINE# #INDENT# raise TypeError ( ( 'Key must be a string instance, received %r' % key ) ) #NEWLINE# #UNINDENT# if ( not isinstance ( key_prefix , basestring ) ) : #NEWLINE# #INDENT# raise TypeError ( ( 'key_prefix must be a string instance, received %r' % key_prefix ) ) #NEWLINE# #UNINDENT# server_key = ( key_prefix + key ) #NEWLINE# if isinstance ( server_key , unicode ) : #NEWLINE# #INDENT# server_key = server_key . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# if ( len ( server_key ) > MAX_KEY_SIZE ) : #NEWLINE# #INDENT# server_key = hashlib . sha1 ( server_key ) . hexdigest ( ) #NEWLINE# #UNINDENT# if ( server_to_user_dict is not None ) : #NEWLINE# #INDENT# assert isinstance ( server_to_user_dict , dict ) #NEWLINE# server_to_user_dict [ server_key ] = server_to_user_dict #NEWLINE# #UNINDENT# return server_key