<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import copy <EOL> import warnings <EOL> import urllib <EOL> from openid import oidutil <EOL> from openid import kvform <EOL> try : <EOL> ElementTree = oidutil . importElementTree ( ) <EOL> except ImportError : <EOL> ElementTree = None <EOL> IDENTIFIER_SELECT = '<STR_LIT>' <EOL> SREG_URI = '<STR_LIT>' <EOL> OPENID1_NS = '<STR_LIT>' <EOL> OPENID2_NS = '<STR_LIT>' <EOL> NULL_NAMESPACE = oidutil . Symbol ( '<STR_LIT>' ) <EOL> OPENID_NS = oidutil . Symbol ( '<STR_LIT>' ) <EOL> BARE_NS = oidutil . Symbol ( '<STR_LIT>' ) <EOL> OPENID1_URL_LIMIT = <NUM_LIT> <EOL> OPENID_PROTOCOL_FIELDS = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:error>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> class UndefinedOpenIDNamespace ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> no_default = object ( ) <EOL> registered_aliases = { } <EOL> class NamespaceAliasRegistrationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def registerNamespaceAlias ( namespace_uri , alias ) : <EOL> """<STR_LIT>""" <EOL> global registered_aliases <EOL> if registered_aliases . get ( alias ) == namespace_uri : <EOL> return <EOL> if namespace_uri in registered_aliases . values ( ) : <EOL> raise NamespaceAliasRegistrationError , '<STR_LIT>' % ( namespace_uri , ) <EOL> if alias in registered_aliases : <EOL> raise NamespaceAliasRegistrationError , '<STR_LIT>' % ( alias , ) <EOL> registered_aliases [ alias ] = namespace_uri <EOL> class Message ( object ) : <EOL> """<STR_LIT>""" <EOL> allowed_openid_namespaces = [ OPENID1_NS , OPENID2_NS ] <EOL> def __init__ ( self , openid_namespace = None ) : <EOL> """<STR_LIT>""" <EOL> self . args = { } <EOL> self . namespaces = NamespaceMap ( ) <EOL> if openid_namespace is None : <EOL> self . _openid_ns_uri = None <EOL> else : <EOL> self . setOpenIDNamespace ( openid_namespace ) <EOL> def fromPostArgs ( cls , args ) : <EOL> """<STR_LIT>""" <EOL> self = cls ( ) <EOL> openid_args = { } <EOL> for key , value in args . iteritems ( ) : <EOL> if isinstance ( value , list ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( args , ) ) <EOL> try : <EOL> prefix , rest = key . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> prefix = None <EOL> if prefix != '<STR_LIT>' : <EOL> self . args [ ( BARE_NS , key ) ] = value <EOL> else : <EOL> openid_args [ rest ] = value <EOL> self . _fromOpenIDArgs ( openid_args ) <EOL> return self <EOL> fromPostArgs = classmethod ( fromPostArgs ) <EOL> def fromOpenIDArgs ( cls , openid_args ) : <EOL> """<STR_LIT>""" <EOL> self = cls ( ) <EOL> self . _fromOpenIDArgs ( openid_args ) <EOL> return self <EOL> fromOpenIDArgs = classmethod ( fromOpenIDArgs ) <EOL> def _fromOpenIDArgs ( self , openid_args ) : <EOL> global registered_aliases <EOL> ns_args = [ ] <EOL> for rest , value in openid_args . iteritems ( ) : <EOL> try : <EOL> ns_alias , ns_key = rest . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> ns_alias = NULL_NAMESPACE <EOL> ns_key = rest <EOL> if ns_alias == '<STR_LIT>' : <EOL> self . namespaces . addAlias ( value , ns_key ) <EOL> elif ns_alias == NULL_NAMESPACE and ns_key == '<STR_LIT>' : <EOL> self . namespaces . addAlias ( value , NULL_NAMESPACE ) <EOL> else : <EOL> ns_args . append ( ( ns_alias , ns_key , value ) ) <EOL> openid_ns_uri = self . namespaces . getNamespaceURI ( NULL_NAMESPACE ) <EOL> if openid_ns_uri is None : <EOL> openid_ns_uri = OPENID1_NS <EOL> self . setOpenIDNamespace ( openid_ns_uri ) <EOL> for ( ns_alias , ns_key , value ) in ns_args : <EOL> ns_uri = self . namespaces . getNamespaceURI ( ns_alias ) <EOL> if ns_uri is None : <EOL> if openid_ns_uri == OPENID1_NS : <EOL> for _alias , _uri in registered_aliases . iteritems ( ) : <EOL> if _alias == ns_alias : <EOL> ns_uri = _uri <EOL> break <EOL> if ns_uri is None : <EOL> ns_uri = openid_ns_uri <EOL> ns_key = '<STR_LIT>' % ( ns_alias , ns_key ) <EOL> else : <EOL> self . namespaces . addAlias ( ns_uri , ns_alias ) <EOL> self . setArg ( ns_uri , ns_key , value ) <EOL> def setOpenIDNamespace ( self , openid_ns_uri ) : <EOL> if openid_ns_uri not in self . allowed_openid_namespaces : <EOL> raise ValueError ( '<STR_LIT>' % ( openid_ns_uri , ) ) <EOL> self . namespaces . addAlias ( openid_ns_uri , NULL_NAMESPACE ) <EOL> self . _openid_ns_uri = openid_ns_uri <EOL> def getOpenIDNamespace ( self ) : <EOL> return self . _openid_ns_uri <EOL> def isOpenID1 ( self ) : <EOL> return self . getOpenIDNamespace ( ) == OPENID1_NS <EOL> def isOpenID2 ( self ) : <EOL> return self . getOpenIDNamespace ( ) == OPENID2_NS <EOL> def fromKVForm ( cls , kvform_string ) : <EOL> """<STR_LIT>""" <EOL> return cls . fromOpenIDArgs ( kvform . kvToDict ( kvform_string ) ) <EOL> fromKVForm = classmethod ( fromKVForm ) <EOL> def copy ( self ) : <EOL> return copy . deepcopy ( self ) <EOL> def toPostArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> args = { } <EOL> for ns_uri , alias in self . namespaces . iteritems ( ) : <EOL> if alias == NULL_NAMESPACE : <EOL> if ns_uri != OPENID1_NS : <EOL> args [ '<STR_LIT>' ] = ns_uri <EOL> else : <EOL> pass <EOL> else : <EOL> if self . getOpenIDNamespace ( ) != OPENID1_NS : <EOL> ns_key = '<STR_LIT>' + alias <EOL> args [ ns_key ] = ns_uri <EOL> for ( ns_uri , ns_key ) , value in self . args . iteritems ( ) : <EOL> key = self . getKey ( ns_uri , ns_key ) <EOL> args [ key ] = value <EOL> return args <EOL> def toArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> post_args = self . toPostArgs ( ) <EOL> kvargs = { } <EOL> for k , v in post_args . iteritems ( ) : <EOL> if not k . startswith ( '<STR_LIT>' ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> kvargs [ k [ <NUM_LIT:7> : ] ] = v <EOL> return kvargs <EOL> def toFormMarkup ( self , action_url , form_tag_attrs = None , <EOL> submit_text = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if ElementTree is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> form = ElementTree . Element ( '<STR_LIT>' ) <EOL> if form_tag_attrs : <EOL> for name , attr in form_tag_attrs . iteritems ( ) : <EOL> form . attrib [ name ] = attr <EOL> form . attrib [ '<STR_LIT:action>' ] = action_url <EOL> form . attrib [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> form . attrib [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> form . attrib [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> for name , value in self . toPostArgs ( ) . iteritems ( ) : <EOL> attrs = { '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:value>' : value } <EOL> form . append ( ElementTree . Element ( '<STR_LIT:input>' , attrs ) ) <EOL> submit = ElementTree . Element ( <EOL> '<STR_LIT:input>' , { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : submit_text } ) <EOL> form . append ( submit ) <EOL> return ElementTree . tostring ( form ) <EOL> def toURL ( self , base_url ) : <EOL> """<STR_LIT>""" <EOL> return oidutil . appendArgs ( base_url , self . toPostArgs ( ) ) <EOL> def toKVForm ( self ) : <EOL> """<STR_LIT>""" <EOL> return kvform . dictToKV ( self . toArgs ( ) ) <EOL> def toURLEncoded ( self ) : <EOL> """<STR_LIT>""" <EOL> args = self . toPostArgs ( ) . items ( ) <EOL> args . sort ( ) <EOL> return urllib . urlencode ( args ) <EOL> def _fixNS ( self , namespace ) : <EOL> """<STR_LIT>""" <EOL> if namespace == OPENID_NS : <EOL> if self . _openid_ns_uri is None : <EOL> raise UndefinedOpenIDNamespace ( '<STR_LIT>' ) <EOL> else : <EOL> namespace = self . _openid_ns_uri <EOL> if namespace != BARE_NS and type ( namespace ) not in [ str , unicode ] : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> % ( namespace , ) ) <EOL> if namespace != BARE_NS and '<STR_LIT::>' not in namespace : <EOL> fmt = '<STR_LIT>' <EOL> warnings . warn ( fmt % ( namespace , ) , DeprecationWarning ) <EOL> if namespace == '<STR_LIT>' : <EOL> fmt = '<STR_LIT>' <EOL> warnings . warn ( fmt % ( SREG_URI , ) , DeprecationWarning , ) <EOL> return SREG_URI <EOL> return namespace <EOL> def hasKey ( self , namespace , ns_key ) : <EOL> namespace = self . _fixNS ( namespace ) <EOL> return ( namespace , ns_key ) in self . args <EOL> def getKey ( self , namespace , ns_key ) : <EOL> """<STR_LIT>""" <EOL> namespace = self . _fixNS ( namespace ) <EOL> if namespace == BARE_NS : <EOL> return ns_key <EOL> ns_alias = self . namespaces . getAlias ( namespace ) <EOL> if ns_alias is None : <EOL> return None <EOL> if ns_alias == NULL_NAMESPACE : <EOL> tail = ns_key <EOL> else : <EOL> tail = '<STR_LIT>' % ( ns_alias , ns_key ) <EOL> return '<STR_LIT>' + tail <EOL> def getArg ( self , namespace , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> namespace = self . _fixNS ( namespace ) <EOL> args_key = ( namespace , key ) <EOL> try : <EOL> return self . args [ args_key ] <EOL> except KeyError : <EOL> if default is no_default : <EOL> raise KeyError ( ( namespace , key ) ) <EOL> else : <EOL> return default <EOL> def getArgs ( self , namespace ) : <EOL> """<STR_LIT>""" <EOL> namespace = self . _fixNS ( namespace ) <EOL> return dict ( [ <EOL> ( ns_key , value ) <EOL> for ( ( pair_ns , ns_key ) , value ) <EOL> in self . args . iteritems ( ) <EOL> if pair_ns == namespace <EOL> ] ) <EOL> def updateArgs ( self , namespace , updates ) : <EOL> """<STR_LIT>""" <EOL> namespace = self . _fixNS ( namespace ) <EOL> for k , v in updates . iteritems ( ) : <EOL> self . setArg ( namespace , k , v ) <EOL> def setArg ( self , namespace , key , value ) : <EOL> """<STR_LIT>""" <EOL> assert key is not None <EOL> assert value is not None <EOL> namespace = self . _fixNS ( namespace ) <EOL> self . args [ ( namespace , key ) ] = value <EOL> if not ( namespace is BARE_NS ) : <EOL> self . namespaces . add ( namespace ) <EOL> def delArg ( self , namespace , key ) : <EOL> namespace = self . _fixNS ( namespace ) <EOL> del self . args [ ( namespace , key ) ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> self . args ) <EOL> def __eq__ ( self , other ) : <EOL> return self . args == other . args <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def getAliasedArg ( self , aliased_key , default = None ) : <EOL> if aliased_key == '<STR_LIT>' : <EOL> return self . getOpenIDNamespace ( ) <EOL> if aliased_key . startswith ( '<STR_LIT>' ) : <EOL> uri = self . namespaces . getNamespaceURI ( aliased_key [ <NUM_LIT:3> : ] ) <EOL> if uri is None : <EOL> return default <EOL> else : <EOL> return uri <EOL> try : <EOL> alias , key = aliased_key . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> ns = None <EOL> else : <EOL> ns = self . namespaces . getNamespaceURI ( alias ) <EOL> if ns is None : <EOL> key = aliased_key <EOL> ns = self . getOpenIDNamespace ( ) <EOL> return self . getArg ( ns , key , default ) <EOL> class NamespaceMap ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . alias_to_namespace = { } <EOL> self . namespace_to_alias = { } <EOL> def getAlias ( self , namespace_uri ) : <EOL> return self . namespace_to_alias . get ( namespace_uri ) <EOL> def getNamespaceURI ( self , alias ) : <EOL> return self . alias_to_namespace . get ( alias ) <EOL> def iterNamespaceURIs ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . namespace_to_alias ) <EOL> def iterAliases ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . alias_to_namespace ) <EOL> def iteritems ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . namespace_to_alias . iteritems ( ) <EOL> def addAlias ( self , namespace_uri , desired_alias ) : <EOL> """<STR_LIT>""" <EOL> assert desired_alias not in OPENID_PROTOCOL_FIELDS , "<STR_LIT>" % ( desired_alias , ) <EOL> if type ( desired_alias ) in [ str , unicode ] : <EOL> assert '<STR_LIT:.>' not in desired_alias , "<STR_LIT>" % ( desired_alias , ) <EOL> current_namespace_uri = self . alias_to_namespace . get ( desired_alias ) <EOL> if ( current_namespace_uri is not None <EOL> and current_namespace_uri != namespace_uri ) : <EOL> fmt = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> msg = fmt % ( <EOL> namespace_uri , <EOL> desired_alias , <EOL> current_namespace_uri , <EOL> desired_alias ) <EOL> raise KeyError ( msg ) <EOL> alias = self . namespace_to_alias . get ( namespace_uri ) <EOL> if alias is not None and alias != desired_alias : <EOL> fmt = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise KeyError ( fmt % ( namespace_uri , desired_alias , alias ) ) <EOL> assert ( desired_alias == NULL_NAMESPACE or <EOL> type ( desired_alias ) in [ str , unicode ] ) , repr ( desired_alias ) <EOL> self . alias_to_namespace [ desired_alias ] = namespace_uri <EOL> self . namespace_to_alias [ namespace_uri ] = desired_alias <EOL> return desired_alias <EOL> def add ( self , namespace_uri ) : <EOL> """<STR_LIT>""" <EOL> alias = self . namespace_to_alias . get ( namespace_uri ) <EOL> if alias is not None : <EOL> return alias <EOL> i = <NUM_LIT:0> <EOL> while True : <EOL> alias = '<STR_LIT>' + str ( i ) <EOL> try : <EOL> self . addAlias ( namespace_uri , alias ) <EOL> except KeyError : <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> return alias <EOL> assert False , "<STR_LIT>" <EOL> def isDefined ( self , namespace_uri ) : <EOL> return namespace_uri in self . namespace_to_alias <EOL> def __contains__ ( self , namespace_uri ) : <EOL> return self . isDefined ( namespace_uri ) </s>
