<s> "<STR_LIT>" <EOL> import argparse <EOL> import os <EOL> import sys <EOL> try : <EOL> import configparser <EOL> except ImportError : <EOL> import ConfigParser as configparser <EOL> try : <EOL> from inspect import signature <EOL> except ImportError : <EOL> from funcsigs import signature <EOL> from begin import context , extensions , subcommands , utils <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> NODEFAULT = object ( ) <EOL> class CommandLineError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> class DefaultsManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , env_prefix = None , config_file = None , config_section = None ) : <EOL> self . _use_env = env_prefix is not None <EOL> self . _prefix = '<STR_LIT>' if not self . _use_env else env_prefix <EOL> self . _parser = configparser . ConfigParser ( ) <EOL> self . _section = config_section <EOL> if config_file is not None : <EOL> self . _parser . read ( [ config_file , <EOL> os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , config_file ) ] ) <EOL> def metavar ( self , name ) : <EOL> "<STR_LIT>" <EOL> metavar = ( self . _prefix + name ) . upper ( ) <EOL> return metavar <EOL> def from_param ( self , param , default = NODEFAULT ) : <EOL> "<STR_LIT>" <EOL> if param . default is not param . empty : <EOL> default = param . default <EOL> default = self . from_name ( param . name , default ) <EOL> return default <EOL> def from_name ( self , name , default = NODEFAULT , section = None ) : <EOL> "<STR_LIT>" <EOL> if len ( self . _parser . sections ( ) ) > <NUM_LIT:0> : <EOL> section = self . _section if section is None else section <EOL> try : <EOL> default = self . _parser . get ( section , name ) <EOL> except ( configparser . NoSectionError , configparser . NoOptionError ) : <EOL> pass <EOL> if self . _use_env : <EOL> default = os . environ . get ( self . metavar ( name ) , default ) <EOL> return default <EOL> def set_config_section ( self , section ) : <EOL> self . _section = section <EOL> def program_name ( filename , func ) : <EOL> """<STR_LIT>""" <EOL> fullpath = os . path . abspath ( filename ) <EOL> basename , filename = os . path . split ( fullpath ) <EOL> while len ( basename ) > <NUM_LIT:0> : <EOL> if not filename . startswith ( '<STR_LIT>' ) : <EOL> return filename <EOL> basename , filename = os . path . split ( basename ) <EOL> return func . __name__ <EOL> def populate_flag ( parser , param , defaults ) : <EOL> """<STR_LIT>""" <EOL> default = defaults . from_param ( param ) <EOL> if not isinstance ( default , bool ) : <EOL> default = utils . tobool ( default ) <EOL> help = '<STR_LIT>' <EOL> if param . annotation is not param . empty : <EOL> help = param . annotation + '<STR_LIT:U+0020>' <EOL> parser . add_argument ( '<STR_LIT>' + param . name . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) , <EOL> action = '<STR_LIT:store_true>' , default = default , dest = param . name , <EOL> help = ( help + '<STR_LIT>' if not default else '<STR_LIT>' ) ) <EOL> parser . add_argument ( '<STR_LIT>' + param . name . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) , <EOL> action = '<STR_LIT>' , default = default , dest = param . name , <EOL> help = ( help + '<STR_LIT>' if default else '<STR_LIT>' ) ) <EOL> def populate_option ( parser , param , defaults , short_args ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { '<STR_LIT:default>' : defaults . from_param ( param ) } <EOL> kwargs [ '<STR_LIT>' ] = defaults . metavar ( param . name ) <EOL> if param . annotation is not param . empty : <EOL> kwargs [ '<STR_LIT>' ] = param . annotation <EOL> args = [ ] <EOL> if kwargs [ '<STR_LIT:default>' ] is NODEFAULT : <EOL> args . append ( param . name ) <EOL> else : <EOL> args . append ( '<STR_LIT>' + param . name . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) ) <EOL> if short_args : <EOL> args . append ( '<STR_LIT:->' + param . name [ <NUM_LIT:0> ] ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] += '<STR_LIT>' <EOL> parser . add_argument ( * args , ** kwargs ) <EOL> def populate_parser ( parser , defaults , funcsig , short_args , lexical_order ) : <EOL> """<STR_LIT>""" <EOL> params = funcsig . parameters . values ( ) <EOL> if lexical_order : <EOL> params = sorted ( params , key = lambda p : p . name ) <EOL> for param in params : <EOL> if param . kind == param . POSITIONAL_OR_KEYWORD or param . kind == param . KEYWORD_ONLY or param . kind == param . POSITIONAL_ONLY : <EOL> if isinstance ( param . default , bool ) : <EOL> populate_flag ( parser , param , defaults ) <EOL> else : <EOL> populate_option ( parser , param , defaults , short_args ) <EOL> elif param . kind == param . VAR_POSITIONAL : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT:*>' } <EOL> if param . annotation is not param . empty : <EOL> kwargs [ '<STR_LIT>' ] = param . annotation <EOL> parser . add_argument ( param . name , ** kwargs ) <EOL> elif param . kind == param . VAR_KEYWORD : <EOL> msg = '<STR_LIT>' <EOL> raise ValueError ( msg ) <EOL> return parser <EOL> def create_parser ( func , env_prefix = None , config_file = None , config_section = None , <EOL> short_args = True , lexical_order = False , sub_group = None , plugins = None , <EOL> collector = None , formatter_class = argparse . HelpFormatter ) : <EOL> """<STR_LIT>""" <EOL> defaults = DefaultsManager ( env_prefix , config_file , func . __name__ ) <EOL> parser = argparse . ArgumentParser ( <EOL> prog = program_name ( sys . argv [ <NUM_LIT:0> ] , func ) , <EOL> argument_default = NODEFAULT , <EOL> conflict_handler = '<STR_LIT>' , <EOL> description = func . __doc__ , <EOL> formatter_class = formatter_class <EOL> ) <EOL> collector = collector if collector is not None else subcommands . COLLECTORS [ sub_group ] <EOL> if plugins is not None : <EOL> collector . load_plugins ( plugins ) <EOL> if len ( collector ) > <NUM_LIT:0> : <EOL> subparsers = parser . add_subparsers ( title = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' ) <EOL> for subfunc in collector . commands ( ) : <EOL> funcsig = signature ( subfunc ) <EOL> help = None <EOL> if subfunc . __doc__ is not None : <EOL> help = subfunc . __doc__ . splitlines ( ) [ <NUM_LIT:0> ] <EOL> subparser = subparsers . add_parser ( subfunc . __name__ , help = help , <EOL> conflict_handler = '<STR_LIT>' , description = subfunc . __doc__ , <EOL> formatter_class = formatter_class ) <EOL> defaults . set_config_section ( subfunc . __name__ ) <EOL> populate_parser ( subparser , defaults , funcsig , short_args , lexical_order ) <EOL> have_extensions = False <EOL> while hasattr ( func , '<STR_LIT>' ) and not hasattr ( func , '<STR_LIT>' ) : <EOL> if isinstance ( func , extensions . Extension ) : <EOL> func . add_arguments ( parser , defaults ) <EOL> have_extensions = True <EOL> func = getattr ( func , '<STR_LIT>' ) <EOL> funcsig = signature ( func ) <EOL> populate_parser ( parser , defaults , funcsig , short_args , lexical_order ) <EOL> return parser <EOL> def call_function ( func , funcsig , opts ) : <EOL> """<STR_LIT>""" <EOL> def getoption ( opts , name , default = None ) : <EOL> if not hasattr ( opts , name ) : <EOL> msg = "<STR_LIT>" . format ( name ) <EOL> raise CommandLineError ( msg ) <EOL> value = getattr ( opts , name ) <EOL> if value is NODEFAULT : <EOL> if default is None : <EOL> msg = "<STR_LIT>" . format ( name , os . linesep ) <EOL> sys . stderr . write ( msg ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> value = default <EOL> return value <EOL> pargs = [ ] <EOL> kwargs = { } <EOL> for param in funcsig . parameters . values ( ) : <EOL> if param . kind == param . POSITIONAL_OR_KEYWORD or param . kind == param . POSITIONAL_ONLY : <EOL> pargs . append ( getoption ( opts , param . name ) ) <EOL> elif param . kind == param . VAR_POSITIONAL : <EOL> pargs . extend ( getoption ( opts , param . name , [ ] ) ) <EOL> elif param . kind == param . KEYWORD_ONLY : <EOL> kwargs [ param . name ] = getoption ( opts , param . name ) <EOL> elif param . kind == param . VAR_KEYWORD : <EOL> msg = '<STR_LIT>' <EOL> raise CommandLineError ( msg ) <EOL> return func ( * pargs , ** kwargs ) <EOL> def apply_options ( func , opts , run_main = True , sub_group = None , collector = None ) : <EOL> """<STR_LIT>""" <EOL> ext = func <EOL> collector = collector if collector is not None else subcommands . COLLECTORS [ sub_group ] <EOL> while hasattr ( ext , '<STR_LIT>' ) and not hasattr ( ext , '<STR_LIT>' ) : <EOL> if isinstance ( ext , extensions . Extension ) : <EOL> ext . run ( opts ) <EOL> ext = getattr ( ext , '<STR_LIT>' ) <EOL> if run_main : <EOL> with context : <EOL> return_value = call_function ( func , signature ( ext ) , opts ) <EOL> context . return_values += ( return_value , ) <EOL> if hasattr ( opts , '<STR_LIT>' ) : <EOL> subfunc = collector . get ( opts . _subcommand ) <EOL> with context : <EOL> return_value = call_function ( subfunc , signature ( subfunc ) , opts ) <EOL> context . return_values += ( return_value , ) <EOL> return context . last_return </s>
