<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import argparse <EOL> import logging <EOL> import string <EOL> import sys <EOL> import monascaclient <EOL> from monascaclient import client as monasca_client <EOL> from monascaclient . common import utils <EOL> from monascaclient import exc <EOL> from monascaclient import ksclient <EOL> logger = logging . getLogger ( __name__ ) <EOL> class MonascaShell ( object ) : <EOL> def get_base_parser ( self ) : <EOL> parser = argparse . ArgumentParser ( <EOL> prog = '<STR_LIT>' , <EOL> description = __doc__ . strip ( ) , <EOL> epilog = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> add_help = False , <EOL> formatter_class = lambda prog : argparse . HelpFormatter ( <EOL> prog , <EOL> max_help_position = <NUM_LIT> ) <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> action = '<STR_LIT:version>' , <EOL> version = monascaclient . __version__ , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = bool ( utils . env ( '<STR_LIT>' ) ) , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = <NUM_LIT:20> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> default = utils . env ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> return parser <EOL> def get_subcommand_parser ( self , version ) : <EOL> parser = self . get_base_parser ( ) <EOL> self . subcommands = { } <EOL> subparsers = parser . add_subparsers ( metavar = '<STR_LIT>' ) <EOL> submodule = utils . import_versioned_module ( version , '<STR_LIT>' ) <EOL> self . _find_actions ( subparsers , submodule ) <EOL> self . _find_actions ( subparsers , self ) <EOL> self . _add_bash_completion_subparser ( subparsers ) <EOL> return parser <EOL> def _add_bash_completion_subparser ( self , subparsers ) : <EOL> subparser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> add_help = False , <EOL> formatter_class = HelpFormatter <EOL> ) <EOL> self . subcommands [ '<STR_LIT>' ] = subparser <EOL> subparser . set_defaults ( func = self . do_bash_completion ) <EOL> def _find_actions ( self , subparsers , actions_module ) : <EOL> for attr in ( a for a in dir ( actions_module ) if a . startswith ( '<STR_LIT>' ) ) : <EOL> command = attr [ <NUM_LIT:3> : ] . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> callback = getattr ( actions_module , attr ) <EOL> desc = callback . __doc__ or '<STR_LIT>' <EOL> help = desc . strip ( ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> arguments = getattr ( callback , '<STR_LIT>' , [ ] ) <EOL> subparser = subparsers . add_parser ( command , <EOL> help = help , <EOL> description = desc , <EOL> add_help = False , <EOL> formatter_class = HelpFormatter ) <EOL> subparser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> help = argparse . SUPPRESS ) <EOL> self . subcommands [ command ] = subparser <EOL> for ( args , kwargs ) in arguments : <EOL> subparser . add_argument ( * args , ** kwargs ) <EOL> subparser . set_defaults ( func = callback ) <EOL> def _setup_logging ( self , debug ) : <EOL> log_lvl = logging . DEBUG if debug else logging . ERROR <EOL> logging . basicConfig ( <EOL> format = "<STR_LIT>" , <EOL> level = log_lvl ) <EOL> def _setup_verbose ( self , verbose ) : <EOL> if verbose : <EOL> exc . verbose = <NUM_LIT:1> <EOL> def main ( self , argv ) : <EOL> parser = self . get_base_parser ( ) <EOL> ( options , args ) = parser . parse_known_args ( argv ) <EOL> self . _setup_logging ( options . debug ) <EOL> self . _setup_verbose ( options . verbose ) <EOL> api_version = options . monasca_api_version <EOL> subcommand_parser = self . get_subcommand_parser ( api_version ) <EOL> self . parser = subcommand_parser <EOL> if not args and options . help or not argv : <EOL> self . do_help ( options ) <EOL> return <NUM_LIT:0> <EOL> args = subcommand_parser . parse_args ( argv ) <EOL> if args . func == self . do_help : <EOL> self . do_help ( args ) <EOL> return <NUM_LIT:0> <EOL> elif args . func == self . do_bash_completion : <EOL> self . do_bash_completion ( args ) <EOL> return <NUM_LIT:0> <EOL> if not args . os_username and not args . os_auth_token : <EOL> raise exc . CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not args . os_password and not args . os_auth_token : <EOL> raise exc . CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if args . os_no_client_auth : <EOL> if not args . monasca_api_url : <EOL> raise exc . CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> if not args . os_auth_url : <EOL> raise exc . CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if args . os_auth_url and '<STR_LIT>' in args . os_auth_url : <EOL> args . os_auth_url = string . replace ( args . os_auth_url , '<STR_LIT>' , '<STR_LIT>' ) <EOL> kwargs = { <EOL> '<STR_LIT:username>' : args . os_username , <EOL> '<STR_LIT:password>' : args . os_password , <EOL> '<STR_LIT>' : args . os_auth_token , <EOL> '<STR_LIT>' : args . os_auth_url , <EOL> '<STR_LIT>' : args . os_service_type , <EOL> '<STR_LIT>' : args . os_endpoint_type , <EOL> '<STR_LIT>' : args . os_cacert , <EOL> '<STR_LIT>' : args . os_user_domain_id , <EOL> '<STR_LIT>' : args . os_user_domain_name , <EOL> '<STR_LIT>' : args . os_project_id , <EOL> '<STR_LIT>' : args . os_project_name , <EOL> '<STR_LIT>' : args . os_domain_id , <EOL> '<STR_LIT>' : args . os_domain_name , <EOL> '<STR_LIT>' : args . insecure , <EOL> '<STR_LIT>' : args . os_region_name , <EOL> '<STR_LIT>' : args . keystone_timeout <EOL> } <EOL> endpoint = args . monasca_api_url <EOL> if not args . os_no_client_auth : <EOL> _ksclient = ksclient . KSClient ( ** kwargs ) <EOL> if args . os_auth_token : <EOL> token = args . os_auth_token <EOL> else : <EOL> try : <EOL> token = _ksclient . token <EOL> except exc . CommandError : <EOL> raise exc . CommandError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> kwargs = { <EOL> '<STR_LIT>' : token , <EOL> '<STR_LIT>' : args . insecure , <EOL> '<STR_LIT>' : args . os_cacert , <EOL> '<STR_LIT>' : args . cert_file , <EOL> '<STR_LIT>' : args . key_file , <EOL> '<STR_LIT:username>' : args . os_username , <EOL> '<STR_LIT:password>' : args . os_password , <EOL> '<STR_LIT>' : args . os_service_type , <EOL> '<STR_LIT>' : args . os_endpoint_type , <EOL> '<STR_LIT>' : args . os_auth_url , <EOL> '<STR_LIT>' : args . keystone_timeout <EOL> } <EOL> if args . os_user_domain_name : <EOL> kwargs [ '<STR_LIT>' ] = args . os_user_domain_name <EOL> if args . os_user_domain_id : <EOL> kwargs [ '<STR_LIT>' ] = args . os_user_domain_id <EOL> if args . os_region_name : <EOL> kwargs [ '<STR_LIT>' ] = args . os_region_name <EOL> if args . os_project_name : <EOL> kwargs [ '<STR_LIT>' ] = args . os_project_name <EOL> if args . os_project_id : <EOL> kwargs [ '<STR_LIT>' ] = args . os_project_id <EOL> if args . os_domain_name : <EOL> kwargs [ '<STR_LIT>' ] = args . os_domain_name <EOL> if args . os_domain_id : <EOL> kwargs [ '<STR_LIT>' ] = args . os_domain_id <EOL> if not endpoint : <EOL> endpoint = _ksclient . monasca_url <EOL> client = monasca_client . Client ( api_version , endpoint , ** kwargs ) <EOL> args . func ( client , args ) <EOL> def do_bash_completion ( self , args ) : <EOL> """<STR_LIT>""" <EOL> commands = set ( ) <EOL> options = set ( ) <EOL> for sc_str , sc in self . subcommands . items ( ) : <EOL> commands . add ( sc_str ) <EOL> for option in list ( sc . _optionals . _option_string_actions ) : <EOL> options . add ( option ) <EOL> commands . remove ( '<STR_LIT>' ) <EOL> commands . remove ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT:U+0020>' . join ( commands | options ) ) <EOL> @ utils . arg ( '<STR_LIT>' , metavar = '<STR_LIT>' , nargs = '<STR_LIT:?>' , <EOL> help = '<STR_LIT>' ) <EOL> def do_help ( self , args ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( args , '<STR_LIT>' , None ) : <EOL> if args . command in self . subcommands : <EOL> self . subcommands [ args . command ] . print_help ( ) <EOL> else : <EOL> raise exc . CommandError ( "<STR_LIT>" % <EOL> args . command ) <EOL> else : <EOL> self . parser . print_help ( ) <EOL> class HelpFormatter ( argparse . HelpFormatter ) : <EOL> def start_section ( self , heading ) : <EOL> heading = '<STR_LIT>' % ( heading [ <NUM_LIT:0> ] . upper ( ) , heading [ <NUM_LIT:1> : ] ) <EOL> super ( HelpFormatter , self ) . start_section ( heading ) <EOL> def main ( args = None ) : <EOL> try : <EOL> if args is None : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> MonascaShell ( ) . main ( args ) <EOL> except Exception as e : <EOL> if '<STR_LIT>' in args or '<STR_LIT>' in args : <EOL> raise <EOL> else : <EOL> print ( e , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
