<s> """<STR_LIT>""" <EOL> from openid . message import registerNamespaceAlias , NamespaceAliasRegistrationError <EOL> from openid . extension import Extension <EOL> import logging <EOL> try : <EOL> str <EOL> except NameError : <EOL> str = ( str , str ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> data_fields = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def checkFieldName ( field_name ) : <EOL> """<STR_LIT>""" <EOL> if field_name not in data_fields : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( field_name , ) ) <EOL> ns_uri_1_0 = '<STR_LIT>' <EOL> ns_uri_1_1 = '<STR_LIT>' <EOL> ns_uri = ns_uri_1_1 <EOL> try : <EOL> registerNamespaceAlias ( ns_uri_1_1 , '<STR_LIT>' ) <EOL> except NamespaceAliasRegistrationError as e : <EOL> logging . exception ( '<STR_LIT>' % ( ns_uri_1_1 , <EOL> '<STR_LIT>' , str ( e ) , ) ) <EOL> def supportsSReg ( endpoint ) : <EOL> """<STR_LIT>""" <EOL> return ( endpoint . usesExtension ( ns_uri_1_1 ) or <EOL> endpoint . usesExtension ( ns_uri_1_0 ) ) <EOL> class SRegNamespaceError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> def getSRegNS ( message ) : <EOL> """<STR_LIT>""" <EOL> for sreg_ns_uri in [ ns_uri_1_1 , ns_uri_1_0 ] : <EOL> alias = message . namespaces . getAlias ( sreg_ns_uri ) <EOL> if alias is not None : <EOL> break <EOL> else : <EOL> sreg_ns_uri = ns_uri_1_1 <EOL> try : <EOL> message . namespaces . addAlias ( ns_uri_1_1 , '<STR_LIT>' ) <EOL> except KeyError as why : <EOL> raise SRegNamespaceError ( why ) <EOL> return sreg_ns_uri <EOL> class SRegRequest ( Extension ) : <EOL> """<STR_LIT>""" <EOL> ns_alias = '<STR_LIT>' <EOL> def __init__ ( self , required = None , optional = None , policy_url = None , <EOL> sreg_ns_uri = ns_uri ) : <EOL> """<STR_LIT>""" <EOL> Extension . __init__ ( self ) <EOL> self . required = [ ] <EOL> self . optional = [ ] <EOL> self . policy_url = policy_url <EOL> self . ns_uri = sreg_ns_uri <EOL> if required : <EOL> self . requestFields ( required , required = True , strict = True ) <EOL> if optional : <EOL> self . requestFields ( optional , required = False , strict = True ) <EOL> _getSRegNS = staticmethod ( getSRegNS ) <EOL> def fromOpenIDRequest ( cls , request ) : <EOL> """<STR_LIT>""" <EOL> self = cls ( ) <EOL> message = request . message . copy ( ) <EOL> self . ns_uri = self . _getSRegNS ( message ) <EOL> args = message . getArgs ( self . ns_uri ) <EOL> self . parseExtensionArgs ( args ) <EOL> return self <EOL> fromOpenIDRequest = classmethod ( fromOpenIDRequest ) <EOL> def parseExtensionArgs ( self , args , strict = False ) : <EOL> """<STR_LIT>""" <EOL> for list_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> required = ( list_name == '<STR_LIT>' ) <EOL> items = args . get ( list_name ) <EOL> if items : <EOL> for field_name in items . split ( '<STR_LIT:U+002C>' ) : <EOL> try : <EOL> self . requestField ( field_name , required , strict ) <EOL> except ValueError : <EOL> if strict : <EOL> raise <EOL> self . policy_url = args . get ( '<STR_LIT>' ) <EOL> def allRequestedFields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . required + self . optional <EOL> def wereFieldsRequested ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . allRequestedFields ( ) ) <EOL> def __contains__ ( self , field_name ) : <EOL> """<STR_LIT>""" <EOL> return ( field_name in self . required or <EOL> field_name in self . optional ) <EOL> def requestField ( self , field_name , required = False , strict = False ) : <EOL> """<STR_LIT>""" <EOL> checkFieldName ( field_name ) <EOL> if strict : <EOL> if field_name in self . required or field_name in self . optional : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> if field_name in self . required : <EOL> return <EOL> if field_name in self . optional : <EOL> if required : <EOL> self . optional . remove ( field_name ) <EOL> else : <EOL> return <EOL> if required : <EOL> self . required . append ( field_name ) <EOL> else : <EOL> self . optional . append ( field_name ) <EOL> def requestFields ( self , field_names , required = False , strict = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( field_names , str ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( type ( field_names ) , ) ) <EOL> for field_name in field_names : <EOL> self . requestField ( field_name , required , strict = strict ) <EOL> def getExtensionArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> args = { } <EOL> if self . required : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( self . required ) <EOL> if self . optional : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( self . optional ) <EOL> if self . policy_url : <EOL> args [ '<STR_LIT>' ] = self . policy_url <EOL> return args <EOL> class SRegResponse ( Extension ) : <EOL> """<STR_LIT>""" <EOL> ns_alias = '<STR_LIT>' <EOL> def __init__ ( self , data = None , sreg_ns_uri = ns_uri ) : <EOL> Extension . __init__ ( self ) <EOL> if data is None : <EOL> self . data = { } <EOL> else : <EOL> self . data = data <EOL> self . ns_uri = sreg_ns_uri <EOL> def extractResponse ( cls , request , data ) : <EOL> """<STR_LIT>""" <EOL> self = cls ( ) <EOL> self . ns_uri = request . ns_uri <EOL> for field in request . allRequestedFields ( ) : <EOL> value = data . get ( field ) <EOL> if value is not None : <EOL> self . data [ field ] = value <EOL> return self <EOL> extractResponse = classmethod ( extractResponse ) <EOL> _getSRegNS = staticmethod ( getSRegNS ) <EOL> def fromSuccessResponse ( cls , success_response , signed_only = True ) : <EOL> """<STR_LIT>""" <EOL> self = cls ( ) <EOL> self . ns_uri = self . _getSRegNS ( success_response . message ) <EOL> if signed_only : <EOL> args = success_response . getSignedNS ( self . ns_uri ) <EOL> else : <EOL> args = success_response . message . getArgs ( self . ns_uri ) <EOL> if not args : <EOL> return None <EOL> for field_name in data_fields : <EOL> if field_name in args : <EOL> self . data [ field_name ] = args [ field_name ] <EOL> return self <EOL> fromSuccessResponse = classmethod ( fromSuccessResponse ) <EOL> def getExtensionArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data <EOL> def get ( self , field_name , default = None ) : <EOL> """<STR_LIT>""" <EOL> checkFieldName ( field_name ) <EOL> return self . data . get ( field_name , default ) <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . data . items ( ) ) <EOL> def iteritems ( self ) : <EOL> return iter ( self . data . items ( ) ) <EOL> def keys ( self ) : <EOL> return list ( self . data . keys ( ) ) <EOL> def iterkeys ( self ) : <EOL> return iter ( self . data . keys ( ) ) <EOL> def has_key ( self , key ) : <EOL> return key in self <EOL> def __contains__ ( self , field_name ) : <EOL> checkFieldName ( field_name ) <EOL> return field_name in self . data <EOL> def __iter__ ( self ) : <EOL> return iter ( self . data ) <EOL> def __getitem__ ( self , field_name ) : <EOL> checkFieldName ( field_name ) <EOL> return self . data [ field_name ] <EOL> def __bool__ ( self ) : <EOL> return bool ( self . data ) </s>
