<s> import abc <EOL> import argparse <EOL> import os <EOL> import six <EOL> from stevedore import extension <EOL> from rackclient . openstack . common . apiclient import exceptions <EOL> _discovered_plugins = { } <EOL> def discover_auth_systems ( ) : <EOL> """<STR_LIT>""" <EOL> global _discovered_plugins <EOL> _discovered_plugins = { } <EOL> def add_plugin ( ext ) : <EOL> _discovered_plugins [ ext . name ] = ext . plugin <EOL> ep_namespace = "<STR_LIT>" <EOL> mgr = extension . ExtensionManager ( ep_namespace ) <EOL> mgr . map ( add_plugin ) <EOL> def load_auth_system_opts ( parser ) : <EOL> """<STR_LIT>""" <EOL> group = parser . add_argument_group ( "<STR_LIT>" ) <EOL> BaseAuthPlugin . add_common_opts ( group ) <EOL> for name , auth_plugin in six . iteritems ( _discovered_plugins ) : <EOL> group = parser . add_argument_group ( <EOL> "<STR_LIT>" % name , <EOL> conflict_handler = "<STR_LIT>" ) <EOL> auth_plugin . add_opts ( group ) <EOL> def load_plugin ( auth_system ) : <EOL> try : <EOL> plugin_class = _discovered_plugins [ auth_system ] <EOL> except KeyError : <EOL> raise exceptions . AuthSystemNotFound ( auth_system ) <EOL> return plugin_class ( auth_system = auth_system ) <EOL> def load_plugin_from_args ( args ) : <EOL> """<STR_LIT>""" <EOL> auth_system = args . os_auth_system <EOL> if auth_system : <EOL> plugin = load_plugin ( auth_system ) <EOL> plugin . parse_opts ( args ) <EOL> plugin . sufficient_options ( ) <EOL> return plugin <EOL> for plugin_auth_system in sorted ( six . iterkeys ( _discovered_plugins ) ) : <EOL> plugin_class = _discovered_plugins [ plugin_auth_system ] <EOL> plugin = plugin_class ( ) <EOL> plugin . parse_opts ( args ) <EOL> try : <EOL> plugin . sufficient_options ( ) <EOL> except exceptions . AuthPluginOptionsMissing : <EOL> continue <EOL> return plugin <EOL> raise exceptions . AuthPluginOptionsMissing ( [ "<STR_LIT>" ] ) <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class BaseAuthPlugin ( object ) : <EOL> """<STR_LIT>""" <EOL> auth_system = None <EOL> opt_names = [ ] <EOL> common_opt_names = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:username>" , <EOL> "<STR_LIT:password>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> def __init__ ( self , auth_system = None , ** kwargs ) : <EOL> self . auth_system = auth_system or self . auth_system <EOL> self . opts = dict ( ( name , kwargs . get ( name ) ) <EOL> for name in self . opt_names ) <EOL> @ staticmethod <EOL> def _parser_add_opt ( parser , opt ) : <EOL> """<STR_LIT>""" <EOL> dashed_opt = opt . replace ( "<STR_LIT:_>" , "<STR_LIT:->" ) <EOL> env_var = "<STR_LIT>" % opt . upper ( ) <EOL> arg_default = os . environ . get ( env_var , "<STR_LIT>" ) <EOL> arg_help = "<STR_LIT>" % env_var <EOL> parser . add_argument ( <EOL> "<STR_LIT>" % dashed_opt , <EOL> metavar = "<STR_LIT>" % dashed_opt , <EOL> default = arg_default , <EOL> help = arg_help ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" % opt , <EOL> metavar = "<STR_LIT>" % dashed_opt , <EOL> help = argparse . SUPPRESS ) <EOL> @ classmethod <EOL> def add_opts ( cls , parser ) : <EOL> """<STR_LIT>""" <EOL> for opt in cls . opt_names : <EOL> if opt not in BaseAuthPlugin . common_opt_names : <EOL> cls . _parser_add_opt ( parser , opt ) <EOL> @ classmethod <EOL> def add_common_opts ( cls , parser ) : <EOL> """<STR_LIT>""" <EOL> for opt in cls . common_opt_names : <EOL> cls . _parser_add_opt ( parser , opt ) <EOL> @ staticmethod <EOL> def get_opt ( opt_name , args ) : <EOL> """<STR_LIT>""" <EOL> return ( opt_name , getattr ( args , "<STR_LIT>" % opt_name , None ) ) <EOL> def parse_opts ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . opts . update ( dict ( self . get_opt ( opt_name , args ) <EOL> for opt_name in self . opt_names ) ) <EOL> def authenticate ( self , http_client ) : <EOL> """<STR_LIT>""" <EOL> self . sufficient_options ( ) <EOL> self . _do_authenticate ( http_client ) <EOL> @ abc . abstractmethod <EOL> def _do_authenticate ( self , http_client ) : <EOL> """<STR_LIT>""" <EOL> def sufficient_options ( self ) : <EOL> """<STR_LIT>""" <EOL> missing = [ opt <EOL> for opt in self . opt_names <EOL> if not self . opts . get ( opt ) ] <EOL> if missing : <EOL> raise exceptions . AuthPluginOptionsMissing ( missing ) <EOL> @ abc . abstractmethod <EOL> def token_and_endpoint ( self , endpoint_type , service_type ) : <EOL> """<STR_LIT>""" </s>
