<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import sys , os <EOL> import types <EOL> from Cheetah . Utils . optik . option import Option , NO_DEFAULT <EOL> from Cheetah . Utils . optik . errors import OptionConflictError , OptionValueError , BadOptionError <EOL> def get_prog_name ( ) : <EOL> return os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> SUPPRESS_HELP = "<STR_LIT>" + "<STR_LIT>" <EOL> SUPPRESS_USAGE = "<STR_LIT>" + "<STR_LIT>" <EOL> STD_HELP_OPTION = Option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> STD_VERSION_OPTION = Option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:version>" , <EOL> help = "<STR_LIT>" ) <EOL> class Values : <EOL> def __init__ ( self , defaults = None ) : <EOL> if defaults : <EOL> for ( attr , val ) in defaults . items ( ) : <EOL> setattr ( self , attr , val ) <EOL> def _update_careful ( self , dict ) : <EOL> """<STR_LIT>""" <EOL> for attr in dir ( self ) : <EOL> if dict . has_key ( attr ) : <EOL> dval = dict [ attr ] <EOL> if dval is not None : <EOL> setattr ( self , attr , dval ) <EOL> def _update_loose ( self , dict ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( dict ) <EOL> def _update ( self , dict , mode ) : <EOL> if mode == "<STR_LIT>" : <EOL> self . _update_careful ( dict ) <EOL> elif mode == "<STR_LIT>" : <EOL> self . _update_loose ( dict ) <EOL> else : <EOL> raise ValueError , "<STR_LIT>" % mode <EOL> def read_module ( self , modname , mode = "<STR_LIT>" ) : <EOL> __import__ ( modname ) <EOL> mod = sys . modules [ modname ] <EOL> self . _update ( vars ( mod ) , mode ) <EOL> def read_file ( self , filename , mode = "<STR_LIT>" ) : <EOL> vars = { } <EOL> execfile ( filename , vars ) <EOL> self . _update ( vars , mode ) <EOL> def ensure_value ( self , attr , value ) : <EOL> if not hasattr ( self , attr ) or getattr ( self , attr ) is None : <EOL> setattr ( self , attr , value ) <EOL> return getattr ( self , attr ) <EOL> class OptionParser : <EOL> """<STR_LIT>""" <EOL> standard_option_list = [ STD_HELP_OPTION ] <EOL> def __init__ ( self , <EOL> usage = None , <EOL> option_list = None , <EOL> option_class = Option , <EOL> version = None , <EOL> conflict_handler = "<STR_LIT:error>" ) : <EOL> self . set_usage ( usage ) <EOL> self . option_class = option_class <EOL> self . version = version <EOL> self . set_conflict_handler ( conflict_handler ) <EOL> self . allow_interspersed_args = <NUM_LIT:1> <EOL> self . _create_option_list ( ) <EOL> self . _populate_option_list ( option_list ) <EOL> self . _init_parsing_state ( ) <EOL> def _create_option_list ( self ) : <EOL> self . option_list = [ ] <EOL> self . _short_opt = { } <EOL> self . _long_opt = { } <EOL> self . _long_opts = [ ] <EOL> self . defaults = { } <EOL> def _populate_option_list ( self , option_list ) : <EOL> if self . standard_option_list : <EOL> self . add_options ( self . standard_option_list ) <EOL> if self . version : <EOL> self . add_option ( STD_VERSION_OPTION ) <EOL> if option_list : <EOL> self . add_options ( option_list ) <EOL> def _init_parsing_state ( self ) : <EOL> self . rargs = None <EOL> self . largs = None <EOL> self . values = None <EOL> def set_usage ( self , usage ) : <EOL> if usage is None : <EOL> self . usage = "<STR_LIT>" <EOL> elif usage is SUPPRESS_USAGE : <EOL> self . usage = None <EOL> else : <EOL> self . usage = usage <EOL> def enable_interspersed_args ( self ) : <EOL> self . allow_interspersed_args = <NUM_LIT:1> <EOL> def disable_interspersed_args ( self ) : <EOL> self . allow_interspersed_args = <NUM_LIT:0> <EOL> def set_conflict_handler ( self , handler ) : <EOL> if handler not in ( "<STR_LIT:ignore>" , "<STR_LIT:error>" , "<STR_LIT>" ) : <EOL> raise ValueError , "<STR_LIT>" % handler <EOL> self . conflict_handler = handler <EOL> def set_default ( self , dest , value ) : <EOL> self . defaults [ dest ] = value <EOL> def set_defaults ( self , ** kwargs ) : <EOL> self . defaults . update ( kwargs ) <EOL> def _check_conflict ( self , option ) : <EOL> conflict_opts = [ ] <EOL> for opt in option . _short_opts : <EOL> if self . _short_opt . has_key ( opt ) : <EOL> conflict_opts . append ( ( opt , self . _short_opt [ opt ] ) ) <EOL> for opt in option . _long_opts : <EOL> if self . _long_opt . has_key ( opt ) : <EOL> conflict_opts . append ( ( opt , self . _long_opt [ opt ] ) ) <EOL> if conflict_opts : <EOL> handler = self . conflict_handler <EOL> if handler == "<STR_LIT:ignore>" : <EOL> pass <EOL> elif handler == "<STR_LIT:error>" : <EOL> raise OptionConflictError ( <EOL> "<STR_LIT>" <EOL> % "<STR_LIT:U+002CU+0020>" . join ( [ co [ <NUM_LIT:0> ] for co in conflict_opts ] ) , <EOL> option ) <EOL> elif handler == "<STR_LIT>" : <EOL> for ( opt , c_option ) in conflict_opts : <EOL> if opt . startswith ( "<STR_LIT>" ) : <EOL> c_option . _long_opts . remove ( opt ) <EOL> del self . _long_opt [ opt ] <EOL> else : <EOL> c_option . _short_opts . remove ( opt ) <EOL> del self . _short_opt [ opt ] <EOL> if not ( c_option . _short_opts or c_option . _long_opts ) : <EOL> self . option_list . remove ( c_option ) <EOL> def add_option ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if type ( args [ <NUM_LIT:0> ] ) is types . StringType : <EOL> option = self . option_class ( * args , ** kwargs ) <EOL> elif len ( args ) == <NUM_LIT:1> and not kwargs : <EOL> option = args [ <NUM_LIT:0> ] <EOL> if not isinstance ( option , Option ) : <EOL> raise TypeError , "<STR_LIT>" % option <EOL> else : <EOL> raise TypeError , "<STR_LIT>" <EOL> self . _check_conflict ( option ) <EOL> self . option_list . append ( option ) <EOL> for opt in option . _short_opts : <EOL> self . _short_opt [ opt ] = option <EOL> for opt in option . _long_opts : <EOL> self . _long_opt [ opt ] = option <EOL> self . _long_opts . append ( opt ) <EOL> if option . dest is not None : <EOL> if option . default is not NO_DEFAULT : <EOL> self . defaults [ option . dest ] = option . default <EOL> elif not self . defaults . has_key ( option . dest ) : <EOL> self . defaults [ option . dest ] = None <EOL> def add_options ( self , option_list ) : <EOL> for option in option_list : <EOL> self . add_option ( option ) <EOL> def get_option ( self , opt_str ) : <EOL> return ( self . _short_opt . get ( opt_str ) or <EOL> self . _long_opt . get ( opt_str ) ) <EOL> def has_option ( self , opt_str ) : <EOL> return ( self . _short_opt . has_key ( opt_str ) or <EOL> self . _long_opt . has_key ( opt_str ) ) <EOL> def remove_option ( self , opt_str ) : <EOL> option = self . _short_opt . get ( opt_str ) <EOL> if option is None : <EOL> option = self . _long_opt . get ( opt_str ) <EOL> if option is None : <EOL> raise ValueError ( "<STR_LIT>" % opt_str ) <EOL> for opt in option . _short_opts : <EOL> del self . _short_opt [ opt ] <EOL> for opt in option . _long_opts : <EOL> del self . _long_opt [ opt ] <EOL> self . _long_opts . remove ( opt ) <EOL> self . option_list . remove ( option ) <EOL> def _get_args ( self , args ) : <EOL> if args is None : <EOL> return sys . argv [ <NUM_LIT:1> : ] <EOL> else : <EOL> return args [ : ] <EOL> def parse_args ( self , args = None , values = None ) : <EOL> """<STR_LIT>""" <EOL> rargs = self . _get_args ( args ) <EOL> if values is None : <EOL> values = Values ( self . defaults ) <EOL> self . rargs = rargs <EOL> self . largs = largs = [ ] <EOL> self . values = values <EOL> stop = <NUM_LIT:0> <EOL> while rargs and not stop : <EOL> try : <EOL> stop = self . _process_arg ( largs , rargs , values ) <EOL> except ( BadOptionError , OptionValueError ) , err : <EOL> self . error ( err . msg ) <EOL> args = largs + rargs <EOL> return self . check_values ( values , args ) <EOL> def check_values ( self , values , args ) : <EOL> """<STR_LIT>""" <EOL> return ( values , args ) <EOL> def _process_arg ( self , largs , rargs , values ) : <EOL> """<STR_LIT>""" <EOL> arg = rargs [ <NUM_LIT:0> ] <EOL> if arg == "<STR_LIT>" : <EOL> del rargs [ <NUM_LIT:0> ] <EOL> return <NUM_LIT:1> <EOL> elif arg [ <NUM_LIT:0> : <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> self . _process_long_opt ( rargs , values ) <EOL> elif arg [ : <NUM_LIT:1> ] == "<STR_LIT:->" and len ( arg ) > <NUM_LIT:1> : <EOL> self . _process_short_opts ( rargs , values ) <EOL> else : <EOL> if self . allow_interspersed_args : <EOL> largs . append ( arg ) <EOL> del rargs [ <NUM_LIT:0> ] <EOL> else : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def _match_long_opt ( self , opt ) : <EOL> """<STR_LIT>""" <EOL> return _match_abbrev ( opt , self . _long_opts ) <EOL> def _process_long_opt ( self , rargs , values ) : <EOL> arg = rargs . pop ( <NUM_LIT:0> ) <EOL> if "<STR_LIT:=>" in arg : <EOL> ( opt , next_arg ) = arg . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> rargs . insert ( <NUM_LIT:0> , next_arg ) <EOL> had_explicit_value = <NUM_LIT:1> <EOL> else : <EOL> opt = arg <EOL> had_explicit_value = <NUM_LIT:0> <EOL> opt = self . _match_long_opt ( opt ) <EOL> option = self . _long_opt [ opt ] <EOL> if option . takes_value ( ) : <EOL> nargs = option . nargs <EOL> if len ( rargs ) < nargs : <EOL> if nargs == <NUM_LIT:1> : <EOL> self . error ( "<STR_LIT>" % opt ) <EOL> else : <EOL> self . error ( "<STR_LIT>" <EOL> % ( opt , nargs ) ) <EOL> elif nargs == <NUM_LIT:1> : <EOL> value = rargs . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> value = tuple ( rargs [ <NUM_LIT:0> : nargs ] ) <EOL> del rargs [ <NUM_LIT:0> : nargs ] <EOL> elif had_explicit_value : <EOL> self . error ( "<STR_LIT>" % opt ) <EOL> else : <EOL> value = None <EOL> option . process ( opt , value , values , self ) <EOL> def _process_short_opts ( self , rargs , values ) : <EOL> arg = rargs . pop ( <NUM_LIT:0> ) <EOL> stop = <NUM_LIT:0> <EOL> i = <NUM_LIT:1> <EOL> for ch in arg [ <NUM_LIT:1> : ] : <EOL> opt = "<STR_LIT:->" + ch <EOL> option = self . _short_opt . get ( opt ) <EOL> i += <NUM_LIT:1> <EOL> if not option : <EOL> self . error ( "<STR_LIT>" % opt ) <EOL> if option . takes_value ( ) : <EOL> if i < len ( arg ) : <EOL> rargs . insert ( <NUM_LIT:0> , arg [ i : ] ) <EOL> stop = <NUM_LIT:1> <EOL> nargs = option . nargs <EOL> if len ( rargs ) < nargs : <EOL> if nargs == <NUM_LIT:1> : <EOL> self . error ( "<STR_LIT>" % opt ) <EOL> else : <EOL> self . error ( "<STR_LIT>" <EOL> % ( opt , nargs ) ) <EOL> elif nargs == <NUM_LIT:1> : <EOL> value = rargs . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> value = tuple ( rargs [ <NUM_LIT:0> : nargs ] ) <EOL> del rargs [ <NUM_LIT:0> : nargs ] <EOL> else : <EOL> value = None <EOL> option . process ( opt , value , values , self ) <EOL> if stop : <EOL> break <EOL> def error ( self , msg ) : <EOL> self . print_usage ( sys . stderr ) <EOL> sys . exit ( "<STR_LIT>" % ( get_prog_name ( ) , msg ) ) <EOL> def print_usage ( self , file = None ) : <EOL> if self . usage : <EOL> usage = self . usage . replace ( "<STR_LIT>" , get_prog_name ( ) ) <EOL> print >> file , usage <EOL> print >> file <EOL> def print_version ( self , file = None ) : <EOL> if self . version : <EOL> version = self . version . replace ( "<STR_LIT>" , get_prog_name ( ) ) <EOL> print >> file , version <EOL> def print_help ( self , file = None ) : <EOL> from distutils . fancy_getopt import wrap_text <EOL> if file is None : <EOL> file = sys . stdout <EOL> self . print_usage ( file ) <EOL> print >> file , "<STR_LIT>" <EOL> width = <NUM_LIT> <EOL> option_help = [ ] <EOL> lengths = [ ] <EOL> for option in self . option_list : <EOL> takes_value = option . takes_value ( ) <EOL> if takes_value : <EOL> metavar = option . metavar or option . dest . upper ( ) <EOL> opts = [ ] <EOL> if option . help is SUPPRESS_HELP : <EOL> continue <EOL> if takes_value : <EOL> for sopt in option . _short_opts : <EOL> opts . append ( sopt + metavar ) <EOL> for lopt in option . _long_opts : <EOL> opts . append ( lopt + "<STR_LIT:=>" + metavar ) <EOL> else : <EOL> for opt in option . _short_opts + option . _long_opts : <EOL> opts . append ( opt ) <EOL> opts = "<STR_LIT:U+002CU+0020>" . join ( opts ) <EOL> option_help . append ( ( opts , option . help ) ) <EOL> lengths . append ( len ( opts ) ) <EOL> max_opts = min ( max ( lengths ) , <NUM_LIT:20> ) <EOL> for ( opts , help ) in option_help : <EOL> indent_rest = <NUM_LIT:2> + max_opts + <NUM_LIT:2> <EOL> help_width = width - indent_rest <EOL> if len ( opts ) > max_opts : <EOL> opts = "<STR_LIT:U+0020>" + opts + "<STR_LIT:\n>" <EOL> indent_first = indent_rest <EOL> else : <EOL> opts = "<STR_LIT>" % ( max_opts , opts ) <EOL> indent_first = <NUM_LIT:0> <EOL> file . write ( opts ) <EOL> if help : <EOL> help_lines = wrap_text ( help , help_width ) <EOL> print >> file , "<STR_LIT>" % ( indent_first , "<STR_LIT>" , help_lines [ <NUM_LIT:0> ] ) <EOL> for line in help_lines [ <NUM_LIT:1> : ] : <EOL> print >> file , "<STR_LIT>" % ( indent_rest , "<STR_LIT>" , line ) <EOL> elif opts [ - <NUM_LIT:1> ] != "<STR_LIT:\n>" : <EOL> file . write ( "<STR_LIT:\n>" ) <EOL> def _match_abbrev ( s , words ) : <EOL> """<STR_LIT>""" <EOL> match = None <EOL> for word in words : <EOL> if not word . startswith ( s ) : <EOL> continue <EOL> if s == word : <EOL> return word <EOL> if match : <EOL> raise BadOptionError ( "<STR_LIT>" <EOL> % ( s , match , word ) ) <EOL> match = word <EOL> if match : <EOL> return match <EOL> else : <EOL> raise BadOptionError ( "<STR_LIT>" % s ) </s>
