<s> """<STR_LIT>""" <EOL> import sys <EOL> from zope . interface import Interface , Attribute <EOL> from twisted . plugin import getPlugins <EOL> from twisted . python import usage <EOL> class ICheckerFactory ( Interface ) : <EOL> """<STR_LIT>""" <EOL> authType = Attribute ( <EOL> '<STR_LIT>' ) <EOL> authHelp = Attribute ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> argStringFormat = Attribute ( <EOL> '<STR_LIT>' ) <EOL> credentialInterfaces = Attribute ( <EOL> '<STR_LIT>' ) <EOL> def generateChecker ( argstring ) : <EOL> """<STR_LIT>""" <EOL> class StrcredException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class InvalidAuthType ( StrcredException ) : <EOL> """<STR_LIT>""" <EOL> class InvalidAuthArgumentString ( StrcredException ) : <EOL> """<STR_LIT>""" <EOL> class UnsupportedInterfaces ( StrcredException ) : <EOL> """<STR_LIT>""" <EOL> notSupportedWarning = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def findCheckerFactories ( ) : <EOL> """<STR_LIT>""" <EOL> return getPlugins ( ICheckerFactory ) <EOL> def findCheckerFactory ( authType ) : <EOL> """<STR_LIT>""" <EOL> for factory in findCheckerFactories ( ) : <EOL> if factory . authType == authType : <EOL> return factory <EOL> raise InvalidAuthType ( authType ) <EOL> def makeChecker ( description ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT::>' in description : <EOL> authType , argstring = description . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> else : <EOL> authType = description <EOL> argstring = '<STR_LIT>' <EOL> return findCheckerFactory ( authType ) . generateChecker ( argstring ) <EOL> class AuthOptionMixin : <EOL> """<STR_LIT>""" <EOL> supportedInterfaces = None <EOL> authOutput = sys . stdout <EOL> def supportsInterface ( self , interface ) : <EOL> """<STR_LIT>""" <EOL> return ( self . supportedInterfaces is None <EOL> or interface in self . supportedInterfaces ) <EOL> def supportsCheckerFactory ( self , factory ) : <EOL> """<STR_LIT>""" <EOL> for interface in factory . credentialInterfaces : <EOL> if self . supportsInterface ( interface ) : <EOL> return True <EOL> return False <EOL> def addChecker ( self , checker ) : <EOL> """<STR_LIT>""" <EOL> supported = [ ] <EOL> if self . supportedInterfaces is None : <EOL> supported = checker . credentialInterfaces <EOL> else : <EOL> for interface in checker . credentialInterfaces : <EOL> if self . supportsInterface ( interface ) : <EOL> supported . append ( interface ) <EOL> if not supported : <EOL> raise UnsupportedInterfaces ( checker . credentialInterfaces ) <EOL> if '<STR_LIT>' not in self : <EOL> self [ '<STR_LIT>' ] = { } <EOL> if '<STR_LIT>' not in self : <EOL> self [ '<STR_LIT>' ] = [ ] <EOL> self [ '<STR_LIT>' ] . append ( checker ) <EOL> for interface in supported : <EOL> self [ '<STR_LIT>' ] . setdefault ( interface , [ ] ) . append ( checker ) <EOL> def opt_auth ( self , description ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . addChecker ( makeChecker ( description ) ) <EOL> except UnsupportedInterfaces , e : <EOL> raise usage . UsageError ( <EOL> '<STR_LIT>' % e . args [ <NUM_LIT:0> ] ) <EOL> except InvalidAuthType , e : <EOL> raise usage . UsageError ( <EOL> '<STR_LIT>' % e . args [ <NUM_LIT:0> ] ) <EOL> except Exception , e : <EOL> raise usage . UsageError ( '<STR_LIT>' % e ) <EOL> def _checkerFactoriesForOptHelpAuth ( self ) : <EOL> """<STR_LIT>""" <EOL> for factory in findCheckerFactories ( ) : <EOL> for interface in factory . credentialInterfaces : <EOL> if self . supportsInterface ( interface ) : <EOL> yield factory <EOL> break <EOL> def opt_help_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> self . authOutput . write ( "<STR_LIT>" ) <EOL> self . authOutput . write ( "<STR_LIT>" ) <EOL> self . authOutput . write ( '<STR_LIT:\n>' ) <EOL> firstLength = <NUM_LIT:0> <EOL> for factory in self . _checkerFactoriesForOptHelpAuth ( ) : <EOL> if len ( factory . authType ) > firstLength : <EOL> firstLength = len ( factory . authType ) <EOL> formatString = '<STR_LIT>' % firstLength <EOL> self . authOutput . write ( formatString % ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . authOutput . write ( formatString % ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for factory in self . _checkerFactoriesForOptHelpAuth ( ) : <EOL> self . authOutput . write ( <EOL> formatString % ( factory . authType , factory . argStringFormat ) ) <EOL> self . authOutput . write ( '<STR_LIT:\n>' ) <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> def opt_help_auth_type ( self , authType ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> cf = findCheckerFactory ( authType ) <EOL> except InvalidAuthType : <EOL> raise usage . UsageError ( "<STR_LIT>" % authType ) <EOL> self . authOutput . write ( "<STR_LIT>" % authType ) <EOL> self . authOutput . write ( "<STR_LIT>" % cf . argStringFormat ) <EOL> self . authOutput . write ( '<STR_LIT:\n>' ) <EOL> for line in cf . authHelp . strip ( ) . splitlines ( ) : <EOL> self . authOutput . write ( '<STR_LIT>' % line . rstrip ( ) ) <EOL> self . authOutput . write ( '<STR_LIT:\n>' ) <EOL> if not self . supportsCheckerFactory ( cf ) : <EOL> self . authOutput . write ( '<STR_LIT>' % notSupportedWarning ) <EOL> self . authOutput . write ( '<STR_LIT:\n>' ) <EOL> raise SystemExit ( <NUM_LIT:0> ) </s>
