<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> __all__ = [ '<STR_LIT>' , <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> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> __copyright__ = """<STR_LIT>""" <EOL> import sys , os <EOL> import types <EOL> import textwrap <EOL> def _repr ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , id ( self ) , self ) <EOL> try : <EOL> from gettext import gettext <EOL> except ImportError : <EOL> def gettext ( message ) : <EOL> return message <EOL> _ = gettext <EOL> class OptParseError ( Exception ) : <EOL> def __init__ ( self , msg ) : <EOL> self . msg = msg <EOL> def __str__ ( self ) : <EOL> return self . msg <EOL> class OptionError ( OptParseError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg , option ) : <EOL> self . msg = msg <EOL> self . option_id = str ( option ) <EOL> def __str__ ( self ) : <EOL> if self . option_id : <EOL> return "<STR_LIT>" % ( self . option_id , self . msg ) <EOL> else : <EOL> return self . msg <EOL> class OptionConflictError ( OptionError ) : <EOL> """<STR_LIT>""" <EOL> class OptionValueError ( OptParseError ) : <EOL> """<STR_LIT>""" <EOL> class BadOptionError ( OptParseError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , opt_str ) : <EOL> self . opt_str = opt_str <EOL> def __str__ ( self ) : <EOL> return _ ( "<STR_LIT>" ) % self . opt_str <EOL> class AmbiguousOptionError ( BadOptionError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , opt_str , possibilities ) : <EOL> BadOptionError . __init__ ( self , opt_str ) <EOL> self . possibilities = possibilities <EOL> def __str__ ( self ) : <EOL> return ( _ ( "<STR_LIT>" ) <EOL> % ( self . opt_str , "<STR_LIT:U+002CU+0020>" . join ( self . possibilities ) ) ) <EOL> class HelpFormatter : <EOL> """<STR_LIT>""" <EOL> NO_DEFAULT_VALUE = "<STR_LIT:none>" <EOL> def __init__ ( self , <EOL> indent_increment , <EOL> max_help_position , <EOL> width , <EOL> short_first ) : <EOL> self . parser = None <EOL> self . indent_increment = indent_increment <EOL> self . help_position = self . max_help_position = max_help_position <EOL> if width is None : <EOL> try : <EOL> width = int ( os . environ [ '<STR_LIT>' ] ) <EOL> except ( KeyError , ValueError ) : <EOL> width = <NUM_LIT> <EOL> width -= <NUM_LIT:2> <EOL> self . width = width <EOL> self . current_indent = <NUM_LIT:0> <EOL> self . level = <NUM_LIT:0> <EOL> self . help_width = None <EOL> self . short_first = short_first <EOL> self . default_tag = "<STR_LIT>" <EOL> self . option_strings = { } <EOL> self . _short_opt_fmt = "<STR_LIT>" <EOL> self . _long_opt_fmt = "<STR_LIT>" <EOL> def set_parser ( self , parser ) : <EOL> self . parser = parser <EOL> def set_short_opt_delimiter ( self , delim ) : <EOL> if delim not in ( "<STR_LIT>" , "<STR_LIT:U+0020>" ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % delim ) <EOL> self . _short_opt_fmt = "<STR_LIT:%s>" + delim + "<STR_LIT:%s>" <EOL> def set_long_opt_delimiter ( self , delim ) : <EOL> if delim not in ( "<STR_LIT:=>" , "<STR_LIT:U+0020>" ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % delim ) <EOL> self . _long_opt_fmt = "<STR_LIT:%s>" + delim + "<STR_LIT:%s>" <EOL> def indent ( self ) : <EOL> self . current_indent += self . indent_increment <EOL> self . level += <NUM_LIT:1> <EOL> def dedent ( self ) : <EOL> self . current_indent -= self . indent_increment <EOL> assert self . current_indent >= <NUM_LIT:0> , "<STR_LIT>" <EOL> self . level -= <NUM_LIT:1> <EOL> def format_usage ( self , usage ) : <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> def format_heading ( self , heading ) : <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> def _format_text ( self , text ) : <EOL> """<STR_LIT>""" <EOL> text_width = self . width - self . current_indent <EOL> indent = "<STR_LIT:U+0020>" * self . current_indent <EOL> return textwrap . fill ( text , <EOL> text_width , <EOL> initial_indent = indent , <EOL> subsequent_indent = indent ) <EOL> def format_description ( self , description ) : <EOL> if description : <EOL> return self . _format_text ( description ) + "<STR_LIT:\n>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def format_epilog ( self , epilog ) : <EOL> if epilog : <EOL> return "<STR_LIT:\n>" + self . _format_text ( epilog ) + "<STR_LIT:\n>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def expand_default ( self , option ) : <EOL> if self . parser is None or not self . default_tag : <EOL> return option . help <EOL> default_value = self . parser . defaults . get ( option . dest ) <EOL> if default_value is NO_DEFAULT or default_value is None : <EOL> default_value = self . NO_DEFAULT_VALUE <EOL> return option . help . replace ( self . default_tag , str ( default_value ) ) <EOL> def format_option ( self , option ) : <EOL> result = [ ] <EOL> opts = self . option_strings [ option ] <EOL> opt_width = self . help_position - self . current_indent - <NUM_LIT:2> <EOL> if len ( opts ) > opt_width : <EOL> opts = "<STR_LIT>" % ( self . current_indent , "<STR_LIT>" , opts ) <EOL> indent_first = self . help_position <EOL> else : <EOL> opts = "<STR_LIT>" % ( self . current_indent , "<STR_LIT>" , opt_width , opts ) <EOL> indent_first = <NUM_LIT:0> <EOL> result . append ( opts ) <EOL> if option . help : <EOL> help_text = self . expand_default ( option ) <EOL> help_lines = textwrap . wrap ( help_text , self . help_width ) <EOL> result . append ( "<STR_LIT>" % ( indent_first , "<STR_LIT>" , help_lines [ <NUM_LIT:0> ] ) ) <EOL> result . extend ( [ "<STR_LIT>" % ( self . help_position , "<STR_LIT>" , line ) <EOL> for line in help_lines [ <NUM_LIT:1> : ] ] ) <EOL> elif opts [ - <NUM_LIT:1> ] != "<STR_LIT:\n>" : <EOL> result . append ( "<STR_LIT:\n>" ) <EOL> return "<STR_LIT>" . join ( result ) <EOL> def store_option_strings ( self , parser ) : <EOL> self . indent ( ) <EOL> max_len = <NUM_LIT:0> <EOL> for opt in parser . option_list : <EOL> strings = self . format_option_strings ( opt ) <EOL> self . option_strings [ opt ] = strings <EOL> max_len = max ( max_len , len ( strings ) + self . current_indent ) <EOL> self . indent ( ) <EOL> for group in parser . option_groups : <EOL> for opt in group . option_list : <EOL> strings = self . format_option_strings ( opt ) <EOL> self . option_strings [ opt ] = strings <EOL> max_len = max ( max_len , len ( strings ) + self . current_indent ) <EOL> self . dedent ( ) <EOL> self . dedent ( ) <EOL> self . help_position = min ( max_len + <NUM_LIT:2> , self . max_help_position ) <EOL> self . help_width = self . width - self . help_position <EOL> def format_option_strings ( self , option ) : <EOL> """<STR_LIT>""" <EOL> if option . takes_value ( ) : <EOL> metavar = option . metavar or option . dest . upper ( ) <EOL> short_opts = [ self . _short_opt_fmt % ( sopt , metavar ) <EOL> for sopt in option . _short_opts ] <EOL> long_opts = [ self . _long_opt_fmt % ( lopt , metavar ) <EOL> for lopt in option . _long_opts ] <EOL> else : <EOL> short_opts = option . _short_opts <EOL> long_opts = option . _long_opts <EOL> if self . short_first : <EOL> opts = short_opts + long_opts <EOL> else : <EOL> opts = long_opts + short_opts <EOL> return "<STR_LIT:U+002CU+0020>" . join ( opts ) <EOL> class IndentedHelpFormatter ( HelpFormatter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> indent_increment = <NUM_LIT:2> , <EOL> max_help_position = <NUM_LIT> , <EOL> width = None , <EOL> short_first = <NUM_LIT:1> ) : <EOL> HelpFormatter . __init__ ( <EOL> self , indent_increment , max_help_position , width , short_first ) <EOL> def format_usage ( self , usage ) : <EOL> return _ ( "<STR_LIT>" ) % usage <EOL> def format_heading ( self , heading ) : <EOL> return "<STR_LIT>" % ( self . current_indent , "<STR_LIT>" , heading ) <EOL> class TitledHelpFormatter ( HelpFormatter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> indent_increment = <NUM_LIT:0> , <EOL> max_help_position = <NUM_LIT> , <EOL> width = None , <EOL> short_first = <NUM_LIT:0> ) : <EOL> HelpFormatter . __init__ ( <EOL> self , indent_increment , max_help_position , width , short_first ) <EOL> def format_usage ( self , usage ) : <EOL> return "<STR_LIT>" % ( self . format_heading ( _ ( "<STR_LIT>" ) ) , usage ) <EOL> def format_heading ( self , heading ) : <EOL> return "<STR_LIT>" % ( heading , "<STR_LIT>" [ self . level ] * len ( heading ) ) <EOL> def _parse_num ( val , type ) : <EOL> if val [ : <NUM_LIT:2> ] . lower ( ) == "<STR_LIT>" : <EOL> radix = <NUM_LIT:16> <EOL> elif val [ : <NUM_LIT:2> ] . lower ( ) == "<STR_LIT>" : <EOL> radix = <NUM_LIT:2> <EOL> val = val [ <NUM_LIT:2> : ] or "<STR_LIT:0>" <EOL> elif val [ : <NUM_LIT:1> ] == "<STR_LIT:0>" : <EOL> radix = <NUM_LIT:8> <EOL> else : <EOL> radix = <NUM_LIT:10> <EOL> return type ( val , radix ) <EOL> def _parse_int ( val ) : <EOL> return _parse_num ( val , int ) <EOL> def _parse_long ( val ) : <EOL> return _parse_num ( val , long ) <EOL> _builtin_cvt = { "<STR_LIT:int>" : ( _parse_int , _ ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : ( _parse_long , _ ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT:float>" : ( float , _ ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : ( complex , _ ( "<STR_LIT>" ) ) } <EOL> def check_builtin ( option , opt , value ) : <EOL> ( cvt , what ) = _builtin_cvt [ option . type ] <EOL> try : <EOL> return cvt ( value ) <EOL> except ValueError : <EOL> raise OptionValueError ( <EOL> _ ( "<STR_LIT>" ) % ( opt , what , value ) ) <EOL> def check_choice ( option , opt , value ) : <EOL> if value in option . choices : <EOL> return value <EOL> else : <EOL> choices = "<STR_LIT:U+002CU+0020>" . join ( map ( repr , option . choices ) ) <EOL> raise OptionValueError ( <EOL> _ ( "<STR_LIT>" ) <EOL> % ( opt , value , choices ) ) <EOL> NO_DEFAULT = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class Option : <EOL> """<STR_LIT>""" <EOL> ATTRS = [ '<STR_LIT:action>' , <EOL> '<STR_LIT:type>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:default>' , <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> ACTIONS = ( "<STR_LIT:store>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:store_true>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:count>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:version>" ) <EOL> STORE_ACTIONS = ( "<STR_LIT:store>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:store_true>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:count>" ) <EOL> TYPED_ACTIONS = ( "<STR_LIT:store>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> ALWAYS_TYPED_ACTIONS = ( "<STR_LIT:store>" , <EOL> "<STR_LIT>" ) <EOL> CONST_ACTIONS = ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> TYPES = ( "<STR_LIT:string>" , "<STR_LIT:int>" , "<STR_LIT>" , "<STR_LIT:float>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> TYPE_CHECKER = { "<STR_LIT:int>" : check_builtin , <EOL> "<STR_LIT>" : check_builtin , <EOL> "<STR_LIT:float>" : check_builtin , <EOL> "<STR_LIT>" : check_builtin , <EOL> "<STR_LIT>" : check_choice , <EOL> } <EOL> CHECK_METHODS = None <EOL> def __init__ ( self , * opts , ** attrs ) : <EOL> self . _short_opts = [ ] <EOL> self . _long_opts = [ ] <EOL> opts = self . _check_opt_strings ( opts ) <EOL> self . _set_opt_strings ( opts ) <EOL> self . _set_attrs ( attrs ) <EOL> for checker in self . CHECK_METHODS : <EOL> checker ( self ) <EOL> def _check_opt_strings ( self , opts ) : <EOL> opts = filter ( None , opts ) <EOL> if not opts : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return opts <EOL> def _set_opt_strings ( self , opts ) : <EOL> for opt in opts : <EOL> if len ( opt ) < <NUM_LIT:2> : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % opt , self ) <EOL> elif len ( opt ) == <NUM_LIT:2> : <EOL> if not ( opt [ <NUM_LIT:0> ] == "<STR_LIT:->" and opt [ <NUM_LIT:1> ] != "<STR_LIT:->" ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % opt , <EOL> self ) <EOL> self . _short_opts . append ( opt ) <EOL> else : <EOL> if not ( opt [ <NUM_LIT:0> : <NUM_LIT:2> ] == "<STR_LIT>" and opt [ <NUM_LIT:2> ] != "<STR_LIT:->" ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % opt , <EOL> self ) <EOL> self . _long_opts . append ( opt ) <EOL> def _set_attrs ( self , attrs ) : <EOL> for attr in self . ATTRS : <EOL> if attr in attrs : <EOL> setattr ( self , attr , attrs [ attr ] ) <EOL> del attrs [ attr ] <EOL> else : <EOL> if attr == '<STR_LIT:default>' : <EOL> setattr ( self , attr , NO_DEFAULT ) <EOL> else : <EOL> setattr ( self , attr , None ) <EOL> if attrs : <EOL> attrs = attrs . keys ( ) <EOL> attrs . sort ( ) <EOL> raise OptionError ( <EOL> "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( attrs ) , <EOL> self ) <EOL> def _check_action ( self ) : <EOL> if self . action is None : <EOL> self . action = "<STR_LIT:store>" <EOL> elif self . action not in self . ACTIONS : <EOL> raise OptionError ( "<STR_LIT>" % self . action , self ) <EOL> def _check_type ( self ) : <EOL> if self . type is None : <EOL> if self . action in self . ALWAYS_TYPED_ACTIONS : <EOL> if self . choices is not None : <EOL> self . type = "<STR_LIT>" <EOL> else : <EOL> self . type = "<STR_LIT:string>" <EOL> else : <EOL> import __builtin__ <EOL> if ( type ( self . type ) is types . TypeType or <EOL> ( hasattr ( self . type , "<STR_LIT>" ) and <EOL> getattr ( __builtin__ , self . type . __name__ , None ) is self . type ) ) : <EOL> self . type = self . type . __name__ <EOL> if self . type == "<STR_LIT:str>" : <EOL> self . type = "<STR_LIT:string>" <EOL> if self . type not in self . TYPES : <EOL> raise OptionError ( "<STR_LIT>" % self . type , self ) <EOL> if self . action not in self . TYPED_ACTIONS : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . action , self ) <EOL> def _check_choice ( self ) : <EOL> if self . type == "<STR_LIT>" : <EOL> if self . choices is None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" , self ) <EOL> elif type ( self . choices ) not in ( types . TupleType , types . ListType ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> % str ( type ( self . choices ) ) . split ( "<STR_LIT:'>" ) [ <NUM_LIT:1> ] , self ) <EOL> elif self . choices is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . type , self ) <EOL> def _check_dest ( self ) : <EOL> takes_value = ( self . action in self . STORE_ACTIONS or <EOL> self . type is not None ) <EOL> if self . dest is None and takes_value : <EOL> if self . _long_opts : <EOL> self . dest = self . _long_opts [ <NUM_LIT:0> ] [ <NUM_LIT:2> : ] . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> else : <EOL> self . dest = self . _short_opts [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def _check_const ( self ) : <EOL> if self . action not in self . CONST_ACTIONS and self . const is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . action , <EOL> self ) <EOL> def _check_nargs ( self ) : <EOL> if self . action in self . TYPED_ACTIONS : <EOL> if self . nargs is None : <EOL> self . nargs = <NUM_LIT:1> <EOL> elif self . nargs is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . action , <EOL> self ) <EOL> def _check_callback ( self ) : <EOL> if self . action == "<STR_LIT>" : <EOL> if not hasattr ( self . callback , '<STR_LIT>' ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . callback , self ) <EOL> if ( self . callback_args is not None and <EOL> type ( self . callback_args ) is not types . TupleType ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> % self . callback_args , self ) <EOL> if ( self . callback_kwargs is not None and <EOL> type ( self . callback_kwargs ) is not types . DictType ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> % self . callback_kwargs , self ) <EOL> else : <EOL> if self . callback is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> % self . callback , self ) <EOL> if self . callback_args is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" , self ) <EOL> if self . callback_kwargs is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" , self ) <EOL> CHECK_METHODS = [ _check_action , <EOL> _check_type , <EOL> _check_choice , <EOL> _check_dest , <EOL> _check_const , <EOL> _check_nargs , <EOL> _check_callback ] <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:/>" . join ( self . _short_opts + self . _long_opts ) <EOL> __repr__ = _repr <EOL> def takes_value ( self ) : <EOL> return self . type is not None <EOL> def get_opt_string ( self ) : <EOL> if self . _long_opts : <EOL> return self . _long_opts [ <NUM_LIT:0> ] <EOL> else : <EOL> return self . _short_opts [ <NUM_LIT:0> ] <EOL> def check_value ( self , opt , value ) : <EOL> checker = self . TYPE_CHECKER . get ( self . type ) <EOL> if checker is None : <EOL> return value <EOL> else : <EOL> return checker ( self , opt , value ) <EOL> def convert_value ( self , opt , value ) : <EOL> if value is not None : <EOL> if self . nargs == <NUM_LIT:1> : <EOL> return self . check_value ( opt , value ) <EOL> else : <EOL> return tuple ( [ self . check_value ( opt , v ) for v in value ] ) <EOL> def process ( self , opt , value , values , parser ) : <EOL> value = self . convert_value ( opt , value ) <EOL> return self . take_action ( <EOL> self . action , self . dest , opt , value , values , parser ) <EOL> def take_action ( self , action , dest , opt , value , values , parser ) : <EOL> if action == "<STR_LIT:store>" : <EOL> setattr ( values , dest , value ) <EOL> elif action == "<STR_LIT>" : <EOL> setattr ( values , dest , self . const ) <EOL> elif action == "<STR_LIT:store_true>" : <EOL> setattr ( values , dest , True ) <EOL> elif action == "<STR_LIT>" : <EOL> setattr ( values , dest , False ) <EOL> elif action == "<STR_LIT>" : <EOL> values . ensure_value ( dest , [ ] ) . append ( value ) <EOL> elif action == "<STR_LIT>" : <EOL> values . ensure_value ( dest , [ ] ) . append ( self . const ) <EOL> elif action == "<STR_LIT:count>" : <EOL> setattr ( values , dest , values . ensure_value ( dest , <NUM_LIT:0> ) + <NUM_LIT:1> ) <EOL> elif action == "<STR_LIT>" : <EOL> args = self . callback_args or ( ) <EOL> kwargs = self . callback_kwargs or { } <EOL> self . callback ( self , opt , value , parser , * args , ** kwargs ) <EOL> elif action == "<STR_LIT>" : <EOL> parser . print_help ( ) <EOL> parser . exit ( ) <EOL> elif action == "<STR_LIT:version>" : <EOL> parser . print_version ( ) <EOL> parser . exit ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % self . action ) <EOL> return <NUM_LIT:1> <EOL> SUPPRESS_HELP = "<STR_LIT>" + "<STR_LIT>" <EOL> SUPPRESS_USAGE = "<STR_LIT>" + "<STR_LIT>" <EOL> try : <EOL> basestring <EOL> except NameError : <EOL> def isbasestring ( x ) : <EOL> return isinstance ( x , ( types . StringType , types . UnicodeType ) ) <EOL> else : <EOL> def isbasestring ( x ) : <EOL> return isinstance ( x , basestring ) <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 __str__ ( self ) : <EOL> return str ( self . __dict__ ) <EOL> __repr__ = _repr <EOL> def __cmp__ ( self , other ) : <EOL> if isinstance ( other , Values ) : <EOL> return cmp ( self . __dict__ , other . __dict__ ) <EOL> elif isinstance ( other , types . DictType ) : <EOL> return cmp ( self . __dict__ , other ) <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def _update_careful ( self , dict ) : <EOL> """<STR_LIT>""" <EOL> for attr in dir ( self ) : <EOL> if attr in dict : <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 OptionContainer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , option_class , conflict_handler , description ) : <EOL> self . _create_option_list ( ) <EOL> self . option_class = option_class <EOL> self . set_conflict_handler ( conflict_handler ) <EOL> self . set_description ( description ) <EOL> def _create_option_mappings ( self ) : <EOL> self . _short_opt = { } <EOL> self . _long_opt = { } <EOL> self . defaults = { } <EOL> def _share_option_mappings ( self , parser ) : <EOL> self . _short_opt = parser . _short_opt <EOL> self . _long_opt = parser . _long_opt <EOL> self . defaults = parser . defaults <EOL> def set_conflict_handler ( self , handler ) : <EOL> if handler not in ( "<STR_LIT:error>" , "<STR_LIT>" ) : <EOL> raise ValueError , "<STR_LIT>" % handler <EOL> self . conflict_handler = handler <EOL> def set_description ( self , description ) : <EOL> self . description = description <EOL> def get_description ( self ) : <EOL> return self . description <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . _short_opt <EOL> del self . _long_opt <EOL> del self . defaults <EOL> def _check_conflict ( self , option ) : <EOL> conflict_opts = [ ] <EOL> for opt in option . _short_opts : <EOL> if opt in self . _short_opt : <EOL> conflict_opts . append ( ( opt , self . _short_opt [ opt ] ) ) <EOL> for opt in option . _long_opts : <EOL> if opt in self . _long_opt : <EOL> conflict_opts . append ( ( opt , self . _long_opt [ opt ] ) ) <EOL> if conflict_opts : <EOL> handler = self . conflict_handler <EOL> if 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> c_option . container . option_list . remove ( c_option ) <EOL> def add_option ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if type ( args [ <NUM_LIT:0> ] ) in types . StringTypes : <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> option . container = self <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> if option . dest is not None : <EOL> if option . default is not NO_DEFAULT : <EOL> self . defaults [ option . dest ] = option . default <EOL> elif option . dest not in self . defaults : <EOL> self . defaults [ option . dest ] = None <EOL> return option <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 ( opt_str in self . _short_opt or <EOL> opt_str in self . _long_opt ) <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> option . container . option_list . remove ( option ) <EOL> def format_option_help ( self , formatter ) : <EOL> if not self . option_list : <EOL> return "<STR_LIT>" <EOL> result = [ ] <EOL> for option in self . option_list : <EOL> if not option . help is SUPPRESS_HELP : <EOL> result . append ( formatter . format_option ( option ) ) <EOL> return "<STR_LIT>" . join ( result ) <EOL> def format_description ( self , formatter ) : <EOL> return formatter . format_description ( self . get_description ( ) ) <EOL> def format_help ( self , formatter ) : <EOL> result = [ ] <EOL> if self . description : <EOL> result . append ( self . format_description ( formatter ) ) <EOL> if self . option_list : <EOL> result . append ( self . format_option_help ( formatter ) ) <EOL> return "<STR_LIT:\n>" . join ( result ) <EOL> class OptionGroup ( OptionContainer ) : <EOL> def __init__ ( self , parser , title , description = None ) : <EOL> self . parser = parser <EOL> OptionContainer . __init__ ( <EOL> self , parser . option_class , parser . conflict_handler , description ) <EOL> self . title = title <EOL> def _create_option_list ( self ) : <EOL> self . option_list = [ ] <EOL> self . _share_option_mappings ( self . parser ) <EOL> def set_title ( self , title ) : <EOL> self . title = title <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> OptionContainer . destroy ( self ) <EOL> del self . option_list <EOL> def format_help ( self , formatter ) : <EOL> result = formatter . format_heading ( self . title ) <EOL> formatter . indent ( ) <EOL> result += OptionContainer . format_help ( self , formatter ) <EOL> formatter . dedent ( ) <EOL> return result <EOL> class OptionParser ( OptionContainer ) : <EOL> """<STR_LIT>""" <EOL> standard_option_list = [ ] <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> description = None , <EOL> formatter = None , <EOL> add_help_option = True , <EOL> prog = None , <EOL> epilog = None ) : <EOL> OptionContainer . __init__ ( <EOL> self , option_class , conflict_handler , description ) <EOL> self . set_usage ( usage ) <EOL> self . prog = prog <EOL> self . version = version <EOL> self . allow_interspersed_args = True <EOL> self . process_default_values = True <EOL> if formatter is None : <EOL> formatter = IndentedHelpFormatter ( ) <EOL> self . formatter = formatter <EOL> self . formatter . set_parser ( self ) <EOL> self . epilog = epilog <EOL> self . _populate_option_list ( option_list , <EOL> add_help = add_help_option ) <EOL> self . _init_parsing_state ( ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> OptionContainer . destroy ( self ) <EOL> for group in self . option_groups : <EOL> group . destroy ( ) <EOL> del self . option_list <EOL> del self . option_groups <EOL> del self . formatter <EOL> def _create_option_list ( self ) : <EOL> self . option_list = [ ] <EOL> self . option_groups = [ ] <EOL> self . _create_option_mappings ( ) <EOL> def _add_help_option ( self ) : <EOL> self . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT>" , <EOL> help = _ ( "<STR_LIT>" ) ) <EOL> def _add_version_option ( self ) : <EOL> self . add_option ( "<STR_LIT>" , <EOL> action = "<STR_LIT:version>" , <EOL> help = _ ( "<STR_LIT>" ) ) <EOL> def _populate_option_list ( self , option_list , add_help = True ) : <EOL> if self . standard_option_list : <EOL> self . add_options ( self . standard_option_list ) <EOL> if option_list : <EOL> self . add_options ( option_list ) <EOL> if self . version : <EOL> self . _add_version_option ( ) <EOL> if add_help : <EOL> self . _add_help_option ( ) <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> elif usage . lower ( ) . startswith ( "<STR_LIT>" ) : <EOL> self . usage = usage [ <NUM_LIT:7> : ] <EOL> else : <EOL> self . usage = usage <EOL> def enable_interspersed_args ( self ) : <EOL> """<STR_LIT>""" <EOL> self . allow_interspersed_args = True <EOL> def disable_interspersed_args ( self ) : <EOL> """<STR_LIT>""" <EOL> self . allow_interspersed_args = False <EOL> def set_process_default_values ( self , process ) : <EOL> self . process_default_values = process <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 _get_all_options ( self ) : <EOL> options = self . option_list [ : ] <EOL> for group in self . option_groups : <EOL> options . extend ( group . option_list ) <EOL> return options <EOL> def get_default_values ( self ) : <EOL> if not self . process_default_values : <EOL> return Values ( self . defaults ) <EOL> defaults = self . defaults . copy ( ) <EOL> for option in self . _get_all_options ( ) : <EOL> default = defaults . get ( option . dest ) <EOL> if isbasestring ( default ) : <EOL> opt_str = option . get_opt_string ( ) <EOL> defaults [ option . dest ] = option . check_value ( opt_str , default ) <EOL> return Values ( defaults ) <EOL> def add_option_group ( self , * args , ** kwargs ) : <EOL> if type ( args [ <NUM_LIT:0> ] ) is types . StringType : <EOL> group = OptionGroup ( self , * args , ** kwargs ) <EOL> elif len ( args ) == <NUM_LIT:1> and not kwargs : <EOL> group = args [ <NUM_LIT:0> ] <EOL> if not isinstance ( group , OptionGroup ) : <EOL> raise TypeError , "<STR_LIT>" % group <EOL> if group . parser is not self : <EOL> raise ValueError , "<STR_LIT>" <EOL> else : <EOL> raise TypeError , "<STR_LIT>" <EOL> self . option_groups . append ( group ) <EOL> return group <EOL> def get_option_group ( self , opt_str ) : <EOL> option = ( self . _short_opt . get ( opt_str ) or <EOL> self . _long_opt . get ( opt_str ) ) <EOL> if option and option . container is not self : <EOL> return option . container <EOL> return None <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 = self . get_default_values ( ) <EOL> self . rargs = rargs <EOL> self . largs = largs = [ ] <EOL> self . values = values <EOL> try : <EOL> stop = self . _process_args ( largs , rargs , values ) <EOL> except ( BadOptionError , OptionValueError ) , err : <EOL> self . error ( str ( err ) ) <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_args ( self , largs , rargs , values ) : <EOL> """<STR_LIT>""" <EOL> while rargs : <EOL> arg = rargs [ <NUM_LIT:0> ] <EOL> if arg == "<STR_LIT>" : <EOL> del rargs [ <NUM_LIT:0> ] <EOL> return <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> elif self . allow_interspersed_args : <EOL> largs . append ( arg ) <EOL> del rargs [ <NUM_LIT:0> ] <EOL> else : <EOL> return <EOL> def _match_long_opt ( self , opt ) : <EOL> """<STR_LIT>""" <EOL> return _match_abbrev ( opt , self . _long_opt ) <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 = True <EOL> else : <EOL> opt = arg <EOL> had_explicit_value = False <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 = False <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> raise BadOptionError ( opt ) <EOL> if option . takes_value ( ) : <EOL> if i < len ( arg ) : <EOL> rargs . insert ( <NUM_LIT:0> , arg [ i : ] ) <EOL> stop = True <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 get_prog_name ( self ) : <EOL> if self . prog is None : <EOL> return os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return self . prog <EOL> def expand_prog_name ( self , s ) : <EOL> return s . replace ( "<STR_LIT>" , self . get_prog_name ( ) ) <EOL> def get_description ( self ) : <EOL> return self . expand_prog_name ( self . description ) <EOL> def exit ( self , status = <NUM_LIT:0> , msg = None ) : <EOL> if msg : <EOL> sys . stderr . write ( msg ) <EOL> sys . exit ( status ) <EOL> def error ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . print_usage ( sys . stderr ) <EOL> self . exit ( <NUM_LIT:2> , "<STR_LIT>" % ( self . get_prog_name ( ) , msg ) ) <EOL> def get_usage ( self ) : <EOL> if self . usage : <EOL> return self . formatter . format_usage ( <EOL> self . expand_prog_name ( self . usage ) ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def print_usage ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> if self . usage : <EOL> print >> file , self . get_usage ( ) <EOL> def get_version ( self ) : <EOL> if self . version : <EOL> return self . expand_prog_name ( self . version ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def print_version ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> if self . version : <EOL> print >> file , self . get_version ( ) <EOL> def format_option_help ( self , formatter = None ) : <EOL> if formatter is None : <EOL> formatter = self . formatter <EOL> formatter . store_option_strings ( self ) <EOL> result = [ ] <EOL> result . append ( formatter . format_heading ( _ ( "<STR_LIT>" ) ) ) <EOL> formatter . indent ( ) <EOL> if self . option_list : <EOL> result . append ( OptionContainer . format_option_help ( self , formatter ) ) <EOL> result . append ( "<STR_LIT:\n>" ) <EOL> for group in self . option_groups : <EOL> result . append ( group . format_help ( formatter ) ) <EOL> result . append ( "<STR_LIT:\n>" ) <EOL> formatter . dedent ( ) <EOL> return "<STR_LIT>" . join ( result [ : - <NUM_LIT:1> ] ) <EOL> def format_epilog ( self , formatter ) : <EOL> return formatter . format_epilog ( self . epilog ) <EOL> def format_help ( self , formatter = None ) : <EOL> if formatter is None : <EOL> formatter = self . formatter <EOL> result = [ ] <EOL> if self . usage : <EOL> result . append ( self . get_usage ( ) + "<STR_LIT:\n>" ) <EOL> if self . description : <EOL> result . append ( self . format_description ( formatter ) + "<STR_LIT:\n>" ) <EOL> result . append ( self . format_option_help ( formatter ) ) <EOL> result . append ( self . format_epilog ( formatter ) ) <EOL> return "<STR_LIT>" . join ( result ) <EOL> def _get_encoding ( self , file ) : <EOL> encoding = getattr ( file , "<STR_LIT>" , None ) <EOL> if not encoding : <EOL> encoding = sys . getdefaultencoding ( ) <EOL> return encoding <EOL> def print_help ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> if file is None : <EOL> file = sys . stdout <EOL> encoding = self . _get_encoding ( file ) <EOL> file . write ( self . format_help ( ) . encode ( encoding , "<STR_LIT:replace>" ) ) <EOL> def _match_abbrev ( s , wordmap ) : <EOL> """<STR_LIT>""" <EOL> if s in wordmap : <EOL> return s <EOL> else : <EOL> possibilities = [ word for word in wordmap . keys ( ) <EOL> if word . startswith ( s ) ] <EOL> if len ( possibilities ) == <NUM_LIT:1> : <EOL> return possibilities [ <NUM_LIT:0> ] <EOL> elif not possibilities : <EOL> raise BadOptionError ( s ) <EOL> else : <EOL> possibilities . sort ( ) <EOL> raise AmbiguousOptionError ( s , possibilities ) <EOL> make_option = Option </s>
