<s> """<STR_LIT>""" <EOL> import cgi <EOL> import getopt <EOL> import os <EOL> import re <EOL> import string <EOL> import struct <EOL> import sys <EOL> try : <EOL> import fcntl <EOL> except ImportError : <EOL> fcntl = None <EOL> try : <EOL> import termios <EOL> except ImportError : <EOL> termios = None <EOL> import gflags_validators <EOL> _RUNNING_PYCHECKER = '<STR_LIT>' in sys . modules <EOL> def _GetCallingModuleObjectAndName ( ) : <EOL> """<STR_LIT>""" <EOL> for depth in range ( <NUM_LIT:1> , sys . getrecursionlimit ( ) ) : <EOL> if not sys . _getframe ( depth ) . f_globals is globals ( ) : <EOL> globals_for_frame = sys . _getframe ( depth ) . f_globals <EOL> module , module_name = _GetModuleObjectAndName ( globals_for_frame ) <EOL> if module_name is not None : <EOL> return module , module_name <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> def _GetCallingModule ( ) : <EOL> """<STR_LIT>""" <EOL> return _GetCallingModuleObjectAndName ( ) [ <NUM_LIT:1> ] <EOL> def _GetThisModuleObjectAndName ( ) : <EOL> """<STR_LIT>""" <EOL> return _GetModuleObjectAndName ( globals ( ) ) <EOL> class FlagsError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class DuplicateFlag ( FlagsError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CantOpenFlagFileError ( FlagsError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class DuplicateFlagCannotPropagateNoneToSwig ( DuplicateFlag ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class DuplicateFlagError ( DuplicateFlag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , flagname , flag_values , other_flag_values = None ) : <EOL> """<STR_LIT>""" <EOL> self . flagname = flagname <EOL> first_module = flag_values . FindModuleDefiningFlag ( <EOL> flagname , default = '<STR_LIT>' ) <EOL> if other_flag_values is None : <EOL> second_module = _GetCallingModule ( ) <EOL> else : <EOL> second_module = other_flag_values . FindModuleDefiningFlag ( <EOL> flagname , default = '<STR_LIT>' ) <EOL> msg = "<STR_LIT>" % ( <EOL> self . flagname , first_module , second_module ) <EOL> DuplicateFlag . __init__ ( self , msg ) <EOL> class IllegalFlagValue ( FlagsError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnrecognizedFlag ( FlagsError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnrecognizedFlagError ( UnrecognizedFlag ) : <EOL> def __init__ ( self , flagname , flagvalue = '<STR_LIT>' ) : <EOL> self . flagname = flagname <EOL> self . flagvalue = flagvalue <EOL> UnrecognizedFlag . __init__ ( <EOL> self , "<STR_LIT>" % flagname ) <EOL> _exported_flags = { } <EOL> _help_width = <NUM_LIT> <EOL> def GetHelpWidth ( ) : <EOL> """<STR_LIT>""" <EOL> if ( not sys . stdout . isatty ( ) ) or ( termios is None ) or ( fcntl is None ) : <EOL> return _help_width <EOL> try : <EOL> data = fcntl . ioctl ( sys . stdout , termios . TIOCGWINSZ , '<STR_LIT>' ) <EOL> columns = struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:1> ] <EOL> if columns >= <NUM_LIT> : <EOL> return columns <EOL> return int ( os . getenv ( '<STR_LIT>' , _help_width ) ) <EOL> except ( TypeError , IOError , struct . error ) : <EOL> return _help_width <EOL> def CutCommonSpacePrefix ( text ) : <EOL> """<STR_LIT>""" <EOL> text_lines = text . splitlines ( ) <EOL> while text_lines and not text_lines [ - <NUM_LIT:1> ] : <EOL> text_lines = text_lines [ : - <NUM_LIT:1> ] <EOL> if text_lines : <EOL> if text_lines [ <NUM_LIT:0> ] and text_lines [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . isspace ( ) : <EOL> text_first_line = [ ] <EOL> else : <EOL> text_first_line = [ text_lines . pop ( <NUM_LIT:0> ) ] <EOL> common_prefix = os . path . commonprefix ( [ line for line in text_lines if line ] ) <EOL> space_prefix_len = len ( common_prefix ) - len ( common_prefix . lstrip ( ) ) <EOL> if space_prefix_len : <EOL> for index in xrange ( len ( text_lines ) ) : <EOL> if text_lines [ index ] : <EOL> text_lines [ index ] = text_lines [ index ] [ space_prefix_len : ] <EOL> return '<STR_LIT:\n>' . join ( text_first_line + text_lines ) <EOL> return '<STR_LIT>' <EOL> def TextWrap ( text , length = None , indent = '<STR_LIT>' , firstline_indent = None , tabs = '<STR_LIT:U+0020>' ) : <EOL> """<STR_LIT>""" <EOL> if length is None : <EOL> length = GetHelpWidth ( ) <EOL> if indent is None : <EOL> indent = '<STR_LIT>' <EOL> if len ( indent ) >= length : <EOL> raise FlagsError ( '<STR_LIT>' ) <EOL> if firstline_indent is None : <EOL> firstline_indent = '<STR_LIT>' <EOL> line = indent <EOL> else : <EOL> line = firstline_indent <EOL> if len ( firstline_indent ) >= length : <EOL> raise FlagsError ( '<STR_LIT>' ) <EOL> if not tabs or tabs == '<STR_LIT:U+0020>' : <EOL> text = text . replace ( '<STR_LIT:\t>' , '<STR_LIT:U+0020>' ) <EOL> else : <EOL> tabs_are_whitespace = not tabs . strip ( ) <EOL> line_regex = re . compile ( '<STR_LIT>' , re . MULTILINE ) <EOL> result = [ ] <EOL> for text_line in text . splitlines ( ) : <EOL> old_result_len = len ( result ) <EOL> for spaces , current_tabs , word in line_regex . findall ( text_line . rstrip ( ) ) : <EOL> if current_tabs : <EOL> if ( ( ( result and line != indent ) or <EOL> ( not result and line != firstline_indent ) ) and line [ - <NUM_LIT:1> ] == '<STR_LIT:U+0020>' ) : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> if tabs_are_whitespace : <EOL> line += tabs * len ( current_tabs ) <EOL> else : <EOL> word = tabs * len ( current_tabs ) + word <EOL> if len ( line ) + len ( word ) > length and len ( indent ) + len ( word ) <= length : <EOL> result . append ( line . rstrip ( ) ) <EOL> line = indent + word <EOL> word = '<STR_LIT>' <EOL> if len ( line ) + <NUM_LIT:1> >= length : <EOL> result . append ( line . rstrip ( ) ) <EOL> line = indent <EOL> else : <EOL> line += '<STR_LIT:U+0020>' <EOL> while len ( line ) + len ( word ) >= length : <EOL> line += word <EOL> result . append ( line [ : length ] ) <EOL> word = line [ length : ] <EOL> line = indent <EOL> if word : <EOL> line += word + '<STR_LIT:U+0020>' <EOL> if ( result and line != indent ) or ( not result and line != firstline_indent ) : <EOL> result . append ( line . rstrip ( ) ) <EOL> elif len ( result ) == old_result_len : <EOL> result . append ( '<STR_LIT>' ) <EOL> line = indent <EOL> return '<STR_LIT:\n>' . join ( result ) <EOL> def DocToHelp ( doc ) : <EOL> """<STR_LIT>""" <EOL> doc = doc . strip ( ) <EOL> whitespace_only_line = re . compile ( '<STR_LIT>' , re . M ) <EOL> doc = whitespace_only_line . sub ( '<STR_LIT>' , doc ) <EOL> doc = CutCommonSpacePrefix ( doc ) <EOL> doc = re . sub ( '<STR_LIT>' , '<STR_LIT:U+0020>' , doc , re . M ) <EOL> return doc <EOL> def _GetModuleObjectAndName ( globals_dict ) : <EOL> """<STR_LIT>""" <EOL> for name , module in sys . modules . items ( ) : <EOL> if getattr ( module , '<STR_LIT>' , None ) is globals_dict : <EOL> if name == '<STR_LIT:__main__>' : <EOL> name = sys . argv [ <NUM_LIT:0> ] <EOL> return ( module , name ) <EOL> return ( None , None ) <EOL> def _GetMainModule ( ) : <EOL> """<STR_LIT>""" <EOL> deepest_frame = sys . _getframe ( <NUM_LIT:0> ) <EOL> while deepest_frame . f_back is not None : <EOL> deepest_frame = deepest_frame . f_back <EOL> globals_for_main_module = deepest_frame . f_globals <EOL> main_module_name = _GetModuleObjectAndName ( globals_for_main_module ) [ <NUM_LIT:1> ] <EOL> if main_module_name is None : <EOL> main_module_name = sys . argv [ <NUM_LIT:0> ] <EOL> return main_module_name <EOL> class FlagValues : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . __dict__ [ '<STR_LIT>' ] = { } <EOL> self . __dict__ [ '<STR_LIT>' ] = { } <EOL> self . __dict__ [ '<STR_LIT>' ] = { } <EOL> self . __dict__ [ '<STR_LIT>' ] = { } <EOL> self . UseGnuGetOpt ( False ) <EOL> def UseGnuGetOpt ( self , use_gnu_getopt = True ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ [ '<STR_LIT>' ] = use_gnu_getopt <EOL> def IsGnuGetOpt ( self ) : <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def FlagDict ( self ) : <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def FlagsByModuleDict ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def FlagsByModuleIdDict ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def KeyFlagsByModuleDict ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def _RegisterFlagByModule ( self , module_name , flag ) : <EOL> """<STR_LIT>""" <EOL> flags_by_module = self . FlagsByModuleDict ( ) <EOL> flags_by_module . setdefault ( module_name , [ ] ) . append ( flag ) <EOL> def _RegisterFlagByModuleId ( self , module_id , flag ) : <EOL> """<STR_LIT>""" <EOL> flags_by_module_id = self . FlagsByModuleIdDict ( ) <EOL> flags_by_module_id . setdefault ( module_id , [ ] ) . append ( flag ) <EOL> def _RegisterKeyFlagForModule ( self , module_name , flag ) : <EOL> """<STR_LIT>""" <EOL> key_flags_by_module = self . KeyFlagsByModuleDict ( ) <EOL> key_flags = key_flags_by_module . setdefault ( module_name , [ ] ) <EOL> if flag not in key_flags : <EOL> key_flags . append ( flag ) <EOL> def _GetFlagsDefinedByModule ( self , module ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( module , str ) : <EOL> module = module . __name__ <EOL> return list ( self . FlagsByModuleDict ( ) . get ( module , [ ] ) ) <EOL> def _GetKeyFlagsForModule ( self , module ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( module , str ) : <EOL> module = module . __name__ <EOL> key_flags = self . _GetFlagsDefinedByModule ( module ) <EOL> for flag in self . KeyFlagsByModuleDict ( ) . get ( module , [ ] ) : <EOL> if flag not in key_flags : <EOL> key_flags . append ( flag ) <EOL> return key_flags <EOL> def FindModuleDefiningFlag ( self , flagname , default = None ) : <EOL> """<STR_LIT>""" <EOL> for module , flags in self . FlagsByModuleDict ( ) . iteritems ( ) : <EOL> for flag in flags : <EOL> if flag . name == flagname or flag . short_name == flagname : <EOL> return module <EOL> return default <EOL> def FindModuleIdDefiningFlag ( self , flagname , default = None ) : <EOL> """<STR_LIT>""" <EOL> for module_id , flags in self . FlagsByModuleIdDict ( ) . iteritems ( ) : <EOL> for flag in flags : <EOL> if flag . name == flagname or flag . short_name == flagname : <EOL> return module_id <EOL> return default <EOL> def AppendFlagValues ( self , flag_values ) : <EOL> """<STR_LIT>""" <EOL> for flag_name , flag in flag_values . FlagDict ( ) . iteritems ( ) : <EOL> if flag_name == flag . name : <EOL> try : <EOL> self [ flag_name ] = flag <EOL> except DuplicateFlagError : <EOL> raise DuplicateFlagError ( flag_name , self , <EOL> other_flag_values = flag_values ) <EOL> def RemoveFlagValues ( self , flag_values ) : <EOL> """<STR_LIT>""" <EOL> for flag_name in flag_values . FlagDict ( ) : <EOL> self . __delattr__ ( flag_name ) <EOL> def __setitem__ ( self , name , flag ) : <EOL> """<STR_LIT>""" <EOL> fl = self . FlagDict ( ) <EOL> if not isinstance ( flag , Flag ) : <EOL> raise IllegalFlagValue ( flag ) <EOL> if not isinstance ( name , type ( "<STR_LIT>" ) ) : <EOL> raise FlagsError ( "<STR_LIT>" ) <EOL> if len ( name ) == <NUM_LIT:0> : <EOL> raise FlagsError ( "<STR_LIT>" ) <EOL> if ( name in fl and not flag . allow_override and <EOL> not fl [ name ] . allow_override and not _RUNNING_PYCHECKER ) : <EOL> module , module_name = _GetCallingModuleObjectAndName ( ) <EOL> if ( self . FindModuleDefiningFlag ( name ) == module_name and <EOL> id ( module ) != self . FindModuleIdDefiningFlag ( name ) ) : <EOL> return <EOL> raise DuplicateFlagError ( name , self ) <EOL> short_name = flag . short_name <EOL> if short_name is not None : <EOL> if ( short_name in fl and not flag . allow_override and <EOL> not fl [ short_name ] . allow_override and not _RUNNING_PYCHECKER ) : <EOL> raise DuplicateFlagError ( short_name , self ) <EOL> fl [ short_name ] = flag <EOL> fl [ name ] = flag <EOL> global _exported_flags <EOL> _exported_flags [ name ] = flag <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . FlagDict ( ) [ name ] <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> fl = self . FlagDict ( ) <EOL> if name not in fl : <EOL> raise AttributeError ( name ) <EOL> return fl [ name ] . value <EOL> def __setattr__ ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> fl = self . FlagDict ( ) <EOL> fl [ name ] . value = value <EOL> self . _AssertValidators ( fl [ name ] . validators ) <EOL> return value <EOL> def _AssertAllValidators ( self ) : <EOL> all_validators = set ( ) <EOL> for flag in self . FlagDict ( ) . itervalues ( ) : <EOL> for validator in flag . validators : <EOL> all_validators . add ( validator ) <EOL> self . _AssertValidators ( all_validators ) <EOL> def _AssertValidators ( self , validators ) : <EOL> """<STR_LIT>""" <EOL> for validator in sorted ( <EOL> validators , key = lambda validator : validator . insertion_index ) : <EOL> try : <EOL> validator . Verify ( self ) <EOL> except gflags_validators . Error , e : <EOL> message = validator . PrintFlagsWithValues ( self ) <EOL> raise IllegalFlagValue ( '<STR_LIT>' % ( message , str ( e ) ) ) <EOL> def _FlagIsRegistered ( self , flag_obj ) : <EOL> """<STR_LIT>""" <EOL> flag_dict = self . FlagDict ( ) <EOL> name = flag_obj . name <EOL> if flag_dict . get ( name , None ) == flag_obj : <EOL> return True <EOL> short_name = flag_obj . short_name <EOL> if ( short_name is not None and <EOL> flag_dict . get ( short_name , None ) == flag_obj ) : <EOL> return True <EOL> return False <EOL> def __delattr__ ( self , flag_name ) : <EOL> """<STR_LIT>""" <EOL> fl = self . FlagDict ( ) <EOL> if flag_name not in fl : <EOL> raise AttributeError ( flag_name ) <EOL> flag_obj = fl [ flag_name ] <EOL> del fl [ flag_name ] <EOL> if not self . _FlagIsRegistered ( flag_obj ) : <EOL> self . __RemoveFlagFromDictByModule ( self . FlagsByModuleDict ( ) , flag_obj ) <EOL> self . __RemoveFlagFromDictByModule ( self . FlagsByModuleIdDict ( ) , flag_obj ) <EOL> self . __RemoveFlagFromDictByModule ( self . KeyFlagsByModuleDict ( ) , flag_obj ) <EOL> def __RemoveFlagFromDictByModule ( self , flags_by_module_dict , flag_obj ) : <EOL> """<STR_LIT>""" <EOL> for unused_module , flags_in_module in flags_by_module_dict . iteritems ( ) : <EOL> while flag_obj in flags_in_module : <EOL> flags_in_module . remove ( flag_obj ) <EOL> def SetDefault ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> fl = self . FlagDict ( ) <EOL> if name not in fl : <EOL> raise AttributeError ( name ) <EOL> fl [ name ] . SetDefault ( value ) <EOL> self . _AssertValidators ( fl [ name ] . validators ) <EOL> def __contains__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return name in self . FlagDict ( ) <EOL> has_key = __contains__ <EOL> def __iter__ ( self ) : <EOL> return iter ( self . FlagDict ( ) ) <EOL> def __call__ ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> argv = list ( argv ) <EOL> shortopts = "<STR_LIT>" <EOL> longopts = [ ] <EOL> fl = self . FlagDict ( ) <EOL> argv = argv [ : <NUM_LIT:1> ] + self . ReadFlagsFromFiles ( argv [ <NUM_LIT:1> : ] , force_gnu = False ) <EOL> original_argv = list ( argv ) <EOL> shortest_matches = None <EOL> for name , flag in fl . items ( ) : <EOL> if not flag . boolean : <EOL> continue <EOL> if shortest_matches is None : <EOL> shortest_matches = self . ShortestUniquePrefixes ( fl ) <EOL> no_name = '<STR_LIT>' + name <EOL> prefix = shortest_matches [ name ] <EOL> no_prefix = shortest_matches [ no_name ] <EOL> for arg_idx in range ( <NUM_LIT:1> , len ( argv ) ) : <EOL> arg = argv [ arg_idx ] <EOL> if arg . find ( '<STR_LIT:=>' ) >= <NUM_LIT:0> : continue <EOL> if arg . startswith ( '<STR_LIT>' + prefix ) and ( '<STR_LIT>' + name ) . startswith ( arg ) : <EOL> argv [ arg_idx ] = ( '<STR_LIT>' % name ) <EOL> elif arg . startswith ( '<STR_LIT>' + no_prefix ) and ( '<STR_LIT>' + no_name ) . startswith ( arg ) : <EOL> argv [ arg_idx ] = ( '<STR_LIT>' % name ) <EOL> for name , flag in fl . items ( ) : <EOL> longopts . append ( name + "<STR_LIT:=>" ) <EOL> if len ( name ) == <NUM_LIT:1> : <EOL> shortopts += name <EOL> if not flag . boolean : <EOL> shortopts += "<STR_LIT::>" <EOL> longopts . append ( '<STR_LIT>' ) <EOL> undefok_flags = [ ] <EOL> unrecognized_opts = [ ] <EOL> args = argv [ <NUM_LIT:1> : ] <EOL> while True : <EOL> try : <EOL> if self . __dict__ [ '<STR_LIT>' ] : <EOL> optlist , unparsed_args = getopt . gnu_getopt ( args , shortopts , longopts ) <EOL> else : <EOL> optlist , unparsed_args = getopt . getopt ( args , shortopts , longopts ) <EOL> break <EOL> except getopt . GetoptError , e : <EOL> if not e . opt or e . opt in fl : <EOL> raise FlagsError ( e ) <EOL> for arg_index in range ( len ( args ) ) : <EOL> if ( ( args [ arg_index ] == '<STR_LIT>' + e . opt ) or <EOL> ( args [ arg_index ] == '<STR_LIT:->' + e . opt ) or <EOL> ( args [ arg_index ] . startswith ( '<STR_LIT>' + e . opt + '<STR_LIT:=>' ) ) ) : <EOL> unrecognized_opts . append ( ( e . opt , args [ arg_index ] ) ) <EOL> args = args [ <NUM_LIT:0> : arg_index ] + args [ arg_index + <NUM_LIT:1> : ] <EOL> break <EOL> else : <EOL> raise FlagsError ( e ) <EOL> for name , arg in optlist : <EOL> if name == '<STR_LIT>' : <EOL> flag_names = arg . split ( '<STR_LIT:U+002C>' ) <EOL> undefok_flags . extend ( flag_names ) <EOL> undefok_flags . extend ( '<STR_LIT>' + name for name in flag_names ) <EOL> continue <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> name = name [ <NUM_LIT:2> : ] <EOL> short_option = <NUM_LIT:0> <EOL> else : <EOL> name = name [ <NUM_LIT:1> : ] <EOL> short_option = <NUM_LIT:1> <EOL> if name in fl : <EOL> flag = fl [ name ] <EOL> if flag . boolean and short_option : arg = <NUM_LIT:1> <EOL> flag . Parse ( arg ) <EOL> for opt , value in unrecognized_opts : <EOL> if opt not in undefok_flags : <EOL> raise UnrecognizedFlagError ( opt , value ) <EOL> if unparsed_args : <EOL> if self . __dict__ [ '<STR_LIT>' ] : <EOL> ret_val = argv [ : <NUM_LIT:1> ] + unparsed_args <EOL> else : <EOL> ret_val = argv [ : <NUM_LIT:1> ] + original_argv [ - len ( unparsed_args ) : ] <EOL> else : <EOL> ret_val = argv [ : <NUM_LIT:1> ] <EOL> self . _AssertAllValidators ( ) <EOL> return ret_val <EOL> def Reset ( self ) : <EOL> """<STR_LIT>""" <EOL> for f in self . FlagDict ( ) . values ( ) : <EOL> f . Unparse ( ) <EOL> def RegisteredFlags ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . FlagDict ( ) ) <EOL> def FlagValuesDict ( self ) : <EOL> """<STR_LIT>""" <EOL> flag_values = { } <EOL> for flag_name in self . RegisteredFlags ( ) : <EOL> flag = self . FlagDict ( ) [ flag_name ] <EOL> flag_values [ flag_name ] = flag . value <EOL> return flag_values <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . GetHelp ( ) <EOL> def GetHelp ( self , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> helplist = [ ] <EOL> flags_by_module = self . FlagsByModuleDict ( ) <EOL> if flags_by_module : <EOL> modules = sorted ( flags_by_module ) <EOL> main_module = _GetMainModule ( ) <EOL> if main_module in modules : <EOL> modules . remove ( main_module ) <EOL> modules = [ main_module ] + modules <EOL> for module in modules : <EOL> self . __RenderOurModuleFlags ( module , helplist ) <EOL> self . __RenderModuleFlags ( '<STR_LIT>' , <EOL> _SPECIAL_FLAGS . FlagDict ( ) . values ( ) , <EOL> helplist ) <EOL> else : <EOL> self . __RenderFlagList ( <EOL> self . FlagDict ( ) . values ( ) + _SPECIAL_FLAGS . FlagDict ( ) . values ( ) , <EOL> helplist , prefix ) <EOL> return '<STR_LIT:\n>' . join ( helplist ) <EOL> def __RenderModuleFlags ( self , module , flags , output_lines , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( module , str ) : <EOL> module = module . __name__ <EOL> output_lines . append ( '<STR_LIT>' % ( prefix , module ) ) <EOL> self . __RenderFlagList ( flags , output_lines , prefix + "<STR_LIT:U+0020>" ) <EOL> def __RenderOurModuleFlags ( self , module , output_lines , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> flags = self . _GetFlagsDefinedByModule ( module ) <EOL> if flags : <EOL> self . __RenderModuleFlags ( module , flags , output_lines , prefix ) <EOL> def __RenderOurModuleKeyFlags ( self , module , output_lines , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> key_flags = self . _GetKeyFlagsForModule ( module ) <EOL> if key_flags : <EOL> self . __RenderModuleFlags ( module , key_flags , output_lines , prefix ) <EOL> def ModuleHelp ( self , module ) : <EOL> """<STR_LIT>""" <EOL> helplist = [ ] <EOL> self . __RenderOurModuleKeyFlags ( module , helplist ) <EOL> return '<STR_LIT:\n>' . join ( helplist ) <EOL> def MainModuleHelp ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ModuleHelp ( _GetMainModule ( ) ) <EOL> def __RenderFlagList ( self , flaglist , output_lines , prefix = "<STR_LIT:U+0020>" ) : <EOL> fl = self . FlagDict ( ) <EOL> special_fl = _SPECIAL_FLAGS . FlagDict ( ) <EOL> flaglist = [ ( flag . name , flag ) for flag in flaglist ] <EOL> flaglist . sort ( ) <EOL> flagset = { } <EOL> for ( name , flag ) in flaglist : <EOL> if fl . get ( name , None ) != flag and special_fl . get ( name , None ) != flag : <EOL> continue <EOL> if flag in flagset : continue <EOL> flagset [ flag ] = <NUM_LIT:1> <EOL> flaghelp = "<STR_LIT>" <EOL> if flag . short_name : flaghelp += "<STR_LIT>" % flag . short_name <EOL> if flag . boolean : <EOL> flaghelp += "<STR_LIT>" % flag . name + "<STR_LIT::>" <EOL> else : <EOL> flaghelp += "<STR_LIT>" % flag . name + "<STR_LIT::>" <EOL> flaghelp += "<STR_LIT:U+0020>" <EOL> if flag . help : <EOL> flaghelp += flag . help <EOL> flaghelp = TextWrap ( flaghelp , indent = prefix + "<STR_LIT:U+0020>" , <EOL> firstline_indent = prefix ) <EOL> if flag . default_as_str : <EOL> flaghelp += "<STR_LIT:\n>" <EOL> flaghelp += TextWrap ( "<STR_LIT>" % flag . default_as_str , <EOL> indent = prefix + "<STR_LIT:U+0020>" ) <EOL> if flag . parser . syntactic_help : <EOL> flaghelp += "<STR_LIT:\n>" <EOL> flaghelp += TextWrap ( "<STR_LIT>" % flag . parser . syntactic_help , <EOL> indent = prefix + "<STR_LIT:U+0020>" ) <EOL> output_lines . append ( flaghelp ) <EOL> def get ( self , name , default ) : <EOL> """<STR_LIT>""" <EOL> value = self . __getattr__ ( name ) <EOL> if value is not None : <EOL> return value <EOL> else : <EOL> return default <EOL> def ShortestUniquePrefixes ( self , fl ) : <EOL> """<STR_LIT>""" <EOL> sorted_flags = [ ] <EOL> for name , flag in fl . items ( ) : <EOL> sorted_flags . append ( name ) <EOL> if flag . boolean : <EOL> sorted_flags . append ( '<STR_LIT>' % name ) <EOL> sorted_flags . sort ( ) <EOL> shortest_matches = { } <EOL> prev_idx = <NUM_LIT:0> <EOL> for flag_idx in range ( len ( sorted_flags ) ) : <EOL> curr = sorted_flags [ flag_idx ] <EOL> if flag_idx == ( len ( sorted_flags ) - <NUM_LIT:1> ) : <EOL> next = None <EOL> else : <EOL> next = sorted_flags [ flag_idx + <NUM_LIT:1> ] <EOL> next_len = len ( next ) <EOL> for curr_idx in range ( len ( curr ) ) : <EOL> if ( next is None <EOL> or curr_idx >= next_len <EOL> or curr [ curr_idx ] != next [ curr_idx ] ) : <EOL> shortest_matches [ curr ] = curr [ : max ( prev_idx , curr_idx ) + <NUM_LIT:1> ] <EOL> prev_idx = curr_idx <EOL> break <EOL> else : <EOL> shortest_matches [ curr ] = curr <EOL> prev_idx = curr_idx + <NUM_LIT:1> <EOL> return shortest_matches <EOL> def __IsFlagFileDirective ( self , flag_string ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( flag_string , type ( "<STR_LIT>" ) ) : <EOL> if flag_string . startswith ( '<STR_LIT>' ) : <EOL> return <NUM_LIT:1> <EOL> elif flag_string == '<STR_LIT>' : <EOL> return <NUM_LIT:1> <EOL> elif flag_string . startswith ( '<STR_LIT>' ) : <EOL> return <NUM_LIT:1> <EOL> elif flag_string == '<STR_LIT>' : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:0> <EOL> def ExtractFilename ( self , flagfile_str ) : <EOL> """<STR_LIT>""" <EOL> if flagfile_str . startswith ( '<STR_LIT>' ) : <EOL> return os . path . expanduser ( ( flagfile_str [ ( len ( '<STR_LIT>' ) ) : ] ) . strip ( ) ) <EOL> elif flagfile_str . startswith ( '<STR_LIT>' ) : <EOL> return os . path . expanduser ( ( flagfile_str [ ( len ( '<STR_LIT>' ) ) : ] ) . strip ( ) ) <EOL> else : <EOL> raise FlagsError ( '<STR_LIT>' % flagfile_str ) <EOL> def __GetFlagFileLines ( self , filename , parsed_file_list ) : <EOL> """<STR_LIT>""" <EOL> line_list = [ ] <EOL> flag_line_list = [ ] <EOL> try : <EOL> file_obj = open ( filename , '<STR_LIT:r>' ) <EOL> except IOError , e_msg : <EOL> raise CantOpenFlagFileError ( '<STR_LIT>' % e_msg ) <EOL> line_list = file_obj . readlines ( ) <EOL> file_obj . close ( ) <EOL> parsed_file_list . append ( filename ) <EOL> for line in line_list : <EOL> if line . isspace ( ) : <EOL> pass <EOL> elif line . startswith ( '<STR_LIT:#>' ) or line . startswith ( '<STR_LIT>' ) : <EOL> pass <EOL> elif self . __IsFlagFileDirective ( line ) : <EOL> sub_filename = self . ExtractFilename ( line ) <EOL> if not sub_filename in parsed_file_list : <EOL> included_flags = self . __GetFlagFileLines ( sub_filename , <EOL> parsed_file_list ) <EOL> flag_line_list . extend ( included_flags ) <EOL> else : <EOL> sys . stderr . write ( '<STR_LIT>' % <EOL> ( sub_filename , ) ) <EOL> else : <EOL> flag_line_list . append ( line . strip ( ) ) <EOL> return flag_line_list <EOL> def ReadFlagsFromFiles ( self , argv , force_gnu = True ) : <EOL> """<STR_LIT>""" <EOL> parsed_file_list = [ ] <EOL> rest_of_args = argv <EOL> new_argv = [ ] <EOL> while rest_of_args : <EOL> current_arg = rest_of_args [ <NUM_LIT:0> ] <EOL> rest_of_args = rest_of_args [ <NUM_LIT:1> : ] <EOL> if self . __IsFlagFileDirective ( current_arg ) : <EOL> if current_arg == '<STR_LIT>' or current_arg == '<STR_LIT>' : <EOL> if not rest_of_args : <EOL> raise IllegalFlagValue ( '<STR_LIT>' ) <EOL> flag_filename = os . path . expanduser ( rest_of_args [ <NUM_LIT:0> ] ) <EOL> rest_of_args = rest_of_args [ <NUM_LIT:1> : ] <EOL> else : <EOL> flag_filename = self . ExtractFilename ( current_arg ) <EOL> new_argv . extend ( <EOL> self . __GetFlagFileLines ( flag_filename , parsed_file_list ) ) <EOL> else : <EOL> new_argv . append ( current_arg ) <EOL> if current_arg == '<STR_LIT>' : <EOL> break <EOL> if not current_arg . startswith ( '<STR_LIT:->' ) : <EOL> if not force_gnu and not self . __dict__ [ '<STR_LIT>' ] : <EOL> break <EOL> if rest_of_args : <EOL> new_argv . extend ( rest_of_args ) <EOL> return new_argv <EOL> def FlagsIntoString ( self ) : <EOL> """<STR_LIT>""" <EOL> s = '<STR_LIT>' <EOL> for flag in self . FlagDict ( ) . values ( ) : <EOL> if flag . value is not None : <EOL> s += flag . Serialize ( ) + '<STR_LIT:\n>' <EOL> return s <EOL> def AppendFlagsIntoFile ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> out_file = open ( filename , '<STR_LIT:a>' ) <EOL> out_file . write ( self . FlagsIntoString ( ) ) <EOL> out_file . close ( ) <EOL> def WriteHelpInXMLFormat ( self , outfile = None ) : <EOL> """<STR_LIT>""" <EOL> outfile = outfile or sys . stdout <EOL> outfile . write ( '<STR_LIT>' ) <EOL> outfile . write ( '<STR_LIT>' ) <EOL> indent = '<STR_LIT:U+0020>' <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) , <EOL> indent ) <EOL> usage_doc = sys . modules [ '<STR_LIT:__main__>' ] . __doc__ <EOL> if not usage_doc : <EOL> usage_doc = '<STR_LIT>' % sys . argv [ <NUM_LIT:0> ] <EOL> else : <EOL> usage_doc = usage_doc . replace ( '<STR_LIT:%s>' , sys . argv [ <NUM_LIT:0> ] ) <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , usage_doc , indent ) <EOL> key_flags = self . _GetKeyFlagsForModule ( _GetMainModule ( ) ) <EOL> flags_by_module = self . FlagsByModuleDict ( ) <EOL> all_module_names = list ( flags_by_module . keys ( ) ) <EOL> all_module_names . sort ( ) <EOL> for module_name in all_module_names : <EOL> flag_list = [ ( f . name , f ) for f in flags_by_module [ module_name ] ] <EOL> flag_list . sort ( ) <EOL> for unused_flag_name , flag in flag_list : <EOL> is_key = flag in key_flags <EOL> flag . WriteInfoInXMLFormat ( outfile , module_name , <EOL> is_key = is_key , indent = indent ) <EOL> outfile . write ( '<STR_LIT>' ) <EOL> outfile . flush ( ) <EOL> def AddValidator ( self , validator ) : <EOL> """<STR_LIT>""" <EOL> for flag_name in validator . GetFlagsNames ( ) : <EOL> flag = self . FlagDict ( ) [ flag_name ] <EOL> flag . validators . append ( validator ) <EOL> FLAGS = FlagValues ( ) <EOL> def _StrOrUnicode ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return str ( value ) <EOL> except UnicodeEncodeError : <EOL> return unicode ( value ) <EOL> def _MakeXMLSafe ( s ) : <EOL> """<STR_LIT>""" <EOL> s = cgi . escape ( s ) <EOL> s = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , s ) <EOL> s = s . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> return s <EOL> def _WriteSimpleXMLElement ( outfile , name , value , indent ) : <EOL> """<STR_LIT>""" <EOL> value_str = _StrOrUnicode ( value ) <EOL> if isinstance ( value , bool ) : <EOL> value_str = value_str . lower ( ) <EOL> safe_value_str = _MakeXMLSafe ( value_str ) <EOL> outfile . write ( '<STR_LIT>' % ( indent , name , safe_value_str , name ) ) <EOL> class Flag : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parser , serializer , name , default , help_string , <EOL> short_name = None , boolean = <NUM_LIT:0> , allow_override = <NUM_LIT:0> ) : <EOL> self . name = name <EOL> if not help_string : <EOL> help_string = '<STR_LIT>' <EOL> self . help = help_string <EOL> self . short_name = short_name <EOL> self . boolean = boolean <EOL> self . present = <NUM_LIT:0> <EOL> self . parser = parser <EOL> self . serializer = serializer <EOL> self . allow_override = allow_override <EOL> self . value = None <EOL> self . validators = [ ] <EOL> self . SetDefault ( default ) <EOL> def __hash__ ( self ) : <EOL> return hash ( id ( self ) ) <EOL> def __eq__ ( self , other ) : <EOL> return self is other <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , Flag ) : <EOL> return id ( self ) < id ( other ) <EOL> return NotImplemented <EOL> def __GetParsedValueAsString ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> if self . serializer : <EOL> return repr ( self . serializer . Serialize ( value ) ) <EOL> if self . boolean : <EOL> if value : <EOL> return repr ( '<STR_LIT:true>' ) <EOL> else : <EOL> return repr ( '<STR_LIT:false>' ) <EOL> return repr ( _StrOrUnicode ( value ) ) <EOL> def Parse ( self , argument ) : <EOL> try : <EOL> self . value = self . parser . Parse ( argument ) <EOL> except ValueError , e : <EOL> raise IllegalFlagValue ( "<STR_LIT>" % ( self . name , argument , e ) ) <EOL> self . present += <NUM_LIT:1> <EOL> def Unparse ( self ) : <EOL> if self . default is None : <EOL> self . value = None <EOL> else : <EOL> self . Parse ( self . default ) <EOL> self . present = <NUM_LIT:0> <EOL> def Serialize ( self ) : <EOL> if self . value is None : <EOL> return '<STR_LIT>' <EOL> if self . boolean : <EOL> if self . value : <EOL> return "<STR_LIT>" % self . name <EOL> else : <EOL> return "<STR_LIT>" % self . name <EOL> else : <EOL> if not self . serializer : <EOL> raise FlagsError ( "<STR_LIT>" % self . name ) <EOL> return "<STR_LIT>" % ( self . name , self . serializer . Serialize ( self . value ) ) <EOL> def SetDefault ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None and self . allow_override : <EOL> raise DuplicateFlagCannotPropagateNoneToSwig ( self . name ) <EOL> self . default = value <EOL> self . Unparse ( ) <EOL> self . default_as_str = self . __GetParsedValueAsString ( self . value ) <EOL> def Type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . parser . Type ( ) <EOL> def WriteInfoInXMLFormat ( self , outfile , module_name , is_key = False , indent = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> outfile . write ( indent + '<STR_LIT>' ) <EOL> inner_indent = indent + '<STR_LIT:U+0020>' <EOL> if is_key : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT:key>' , '<STR_LIT:yes>' , inner_indent ) <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT:file>' , module_name , inner_indent ) <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT:name>' , self . name , inner_indent ) <EOL> if self . short_name : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , self . short_name , <EOL> inner_indent ) <EOL> if self . help : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , self . help , inner_indent ) <EOL> if self . serializer and not isinstance ( self . default , str ) : <EOL> default_serialized = self . serializer . Serialize ( self . default ) <EOL> else : <EOL> default_serialized = self . default <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT:default>' , default_serialized , inner_indent ) <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , self . value , inner_indent ) <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT:type>' , self . Type ( ) , inner_indent ) <EOL> self . _WriteCustomInfoInXMLFormat ( outfile , inner_indent ) <EOL> outfile . write ( indent + '<STR_LIT>' ) <EOL> def _WriteCustomInfoInXMLFormat ( self , outfile , indent ) : <EOL> """<STR_LIT>""" <EOL> self . parser . WriteCustomInfoInXMLFormat ( outfile , indent ) <EOL> class _ArgumentParserCache ( type ) : <EOL> """<STR_LIT>""" <EOL> _instances = { } <EOL> def __call__ ( mcs , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> return type . __call__ ( mcs , * args , ** kwargs ) <EOL> else : <EOL> instances = mcs . _instances <EOL> key = ( mcs , ) + tuple ( args ) <EOL> try : <EOL> return instances [ key ] <EOL> except KeyError : <EOL> return instances . setdefault ( key , type . __call__ ( mcs , * args ) ) <EOL> except TypeError : <EOL> return type . __call__ ( mcs , * args ) <EOL> class ArgumentParser ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = _ArgumentParserCache <EOL> syntactic_help = "<STR_LIT>" <EOL> def Parse ( self , argument ) : <EOL> """<STR_LIT>""" <EOL> return argument <EOL> def Type ( self ) : <EOL> return '<STR_LIT:string>' <EOL> def WriteCustomInfoInXMLFormat ( self , outfile , indent ) : <EOL> pass <EOL> class ArgumentSerializer : <EOL> """<STR_LIT>""" <EOL> def Serialize ( self , value ) : <EOL> return _StrOrUnicode ( value ) <EOL> class ListSerializer ( ArgumentSerializer ) : <EOL> def __init__ ( self , list_sep ) : <EOL> self . list_sep = list_sep <EOL> def Serialize ( self , value ) : <EOL> return self . list_sep . join ( [ _StrOrUnicode ( x ) for x in value ] ) <EOL> def RegisterValidator ( flag_name , <EOL> checker , <EOL> message = '<STR_LIT>' , <EOL> flag_values = FLAGS ) : <EOL> """<STR_LIT>""" <EOL> flag_values . AddValidator ( gflags_validators . SimpleValidator ( flag_name , <EOL> checker , <EOL> message ) ) <EOL> def MarkFlagAsRequired ( flag_name , flag_values = FLAGS ) : <EOL> """<STR_LIT>""" <EOL> RegisterValidator ( flag_name , <EOL> lambda value : value is not None , <EOL> message = '<STR_LIT>' % flag_name , <EOL> flag_values = flag_values ) <EOL> def _RegisterBoundsValidatorIfNeeded ( parser , name , flag_values ) : <EOL> """<STR_LIT>""" <EOL> if parser . lower_bound is not None or parser . upper_bound is not None : <EOL> def Checker ( value ) : <EOL> if value is not None and parser . IsOutsideBounds ( value ) : <EOL> message = '<STR_LIT>' % ( value , parser . syntactic_help ) <EOL> raise gflags_validators . Error ( message ) <EOL> return True <EOL> RegisterValidator ( name , <EOL> Checker , <EOL> flag_values = flag_values ) <EOL> def DEFINE ( parser , name , default , help , flag_values = FLAGS , serializer = None , <EOL> ** args ) : <EOL> """<STR_LIT>""" <EOL> DEFINE_flag ( Flag ( parser , serializer , name , default , help , ** args ) , <EOL> flag_values ) <EOL> def DEFINE_flag ( flag , flag_values = FLAGS ) : <EOL> """<STR_LIT>""" <EOL> fv = flag_values <EOL> fv [ flag . name ] = flag <EOL> if isinstance ( flag_values , FlagValues ) : <EOL> module , module_name = _GetCallingModuleObjectAndName ( ) <EOL> flag_values . _RegisterFlagByModule ( module_name , flag ) <EOL> flag_values . _RegisterFlagByModuleId ( id ( module ) , flag ) <EOL> def _InternalDeclareKeyFlags ( flag_names , <EOL> flag_values = FLAGS , key_flag_values = None ) : <EOL> """<STR_LIT>""" <EOL> key_flag_values = key_flag_values or flag_values <EOL> module = _GetCallingModule ( ) <EOL> for flag_name in flag_names : <EOL> if flag_name not in flag_values : <EOL> raise UnrecognizedFlagError ( flag_name ) <EOL> flag = flag_values . FlagDict ( ) [ flag_name ] <EOL> key_flag_values . _RegisterKeyFlagForModule ( module , flag ) <EOL> def DECLARE_key_flag ( flag_name , flag_values = FLAGS ) : <EOL> """<STR_LIT>""" <EOL> if flag_name in _SPECIAL_FLAGS : <EOL> _InternalDeclareKeyFlags ( [ flag_name ] , <EOL> flag_values = _SPECIAL_FLAGS , <EOL> key_flag_values = flag_values ) <EOL> return <EOL> _InternalDeclareKeyFlags ( [ flag_name ] , flag_values = flag_values ) <EOL> def ADOPT_module_key_flags ( module , flag_values = FLAGS ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( module , str ) : <EOL> raise FlagsError ( '<STR_LIT>' <EOL> % module ) <EOL> _InternalDeclareKeyFlags ( <EOL> [ f . name for f in flag_values . _GetKeyFlagsForModule ( module . __name__ ) ] , <EOL> flag_values = flag_values ) <EOL> if module == _GetThisModuleObjectAndName ( ) [ <NUM_LIT:0> ] : <EOL> _InternalDeclareKeyFlags ( <EOL> [ f . name for f in _SPECIAL_FLAGS . FlagDict ( ) . values ( ) ] , <EOL> flag_values = _SPECIAL_FLAGS , <EOL> key_flag_values = flag_values ) <EOL> def DEFINE_string ( name , default , help , flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = ArgumentParser ( ) <EOL> serializer = ArgumentSerializer ( ) <EOL> DEFINE ( parser , name , default , help , flag_values , serializer , ** args ) <EOL> class BooleanParser ( ArgumentParser ) : <EOL> """<STR_LIT>""" <EOL> def Convert ( self , argument ) : <EOL> """<STR_LIT>""" <EOL> if type ( argument ) == str : <EOL> if argument . lower ( ) in [ '<STR_LIT:true>' , '<STR_LIT:t>' , '<STR_LIT:1>' ] : <EOL> return True <EOL> elif argument . lower ( ) in [ '<STR_LIT:false>' , '<STR_LIT:f>' , '<STR_LIT:0>' ] : <EOL> return False <EOL> bool_argument = bool ( argument ) <EOL> if argument == bool_argument : <EOL> return bool_argument <EOL> raise ValueError ( '<STR_LIT>' , argument ) <EOL> def Parse ( self , argument ) : <EOL> val = self . Convert ( argument ) <EOL> return val <EOL> def Type ( self ) : <EOL> return '<STR_LIT:bool>' <EOL> class BooleanFlag ( Flag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , default , help , short_name = None , ** args ) : <EOL> p = BooleanParser ( ) <EOL> Flag . __init__ ( self , p , None , name , default , help , short_name , <NUM_LIT:1> , ** args ) <EOL> if not self . help : self . help = "<STR_LIT>" <EOL> def DEFINE_boolean ( name , default , help , flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> DEFINE_flag ( BooleanFlag ( name , default , help , ** args ) , flag_values ) <EOL> DEFINE_bool = DEFINE_boolean <EOL> class HelpFlag ( BooleanFlag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> BooleanFlag . __init__ ( self , "<STR_LIT>" , <NUM_LIT:0> , "<STR_LIT>" , <EOL> short_name = "<STR_LIT:?>" , allow_override = <NUM_LIT:1> ) <EOL> def Parse ( self , arg ) : <EOL> if arg : <EOL> doc = sys . modules [ "<STR_LIT:__main__>" ] . __doc__ <EOL> flags = str ( FLAGS ) <EOL> print doc or ( "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] ) <EOL> if flags : <EOL> print "<STR_LIT>" <EOL> print flags <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class HelpXMLFlag ( BooleanFlag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> BooleanFlag . __init__ ( self , '<STR_LIT>' , False , <EOL> '<STR_LIT>' , <EOL> allow_override = <NUM_LIT:1> ) <EOL> def Parse ( self , arg ) : <EOL> if arg : <EOL> FLAGS . WriteHelpInXMLFormat ( sys . stdout ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class HelpshortFlag ( BooleanFlag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> BooleanFlag . __init__ ( self , "<STR_LIT>" , <NUM_LIT:0> , <EOL> "<STR_LIT>" , allow_override = <NUM_LIT:1> ) <EOL> def Parse ( self , arg ) : <EOL> if arg : <EOL> doc = sys . modules [ "<STR_LIT:__main__>" ] . __doc__ <EOL> flags = FLAGS . MainModuleHelp ( ) <EOL> print doc or ( "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] ) <EOL> if flags : <EOL> print "<STR_LIT>" <EOL> print flags <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class NumericParser ( ArgumentParser ) : <EOL> """<STR_LIT>""" <EOL> def IsOutsideBounds ( self , val ) : <EOL> return ( ( self . lower_bound is not None and val < self . lower_bound ) or <EOL> ( self . upper_bound is not None and val > self . upper_bound ) ) <EOL> def Parse ( self , argument ) : <EOL> val = self . Convert ( argument ) <EOL> if self . IsOutsideBounds ( val ) : <EOL> raise ValueError ( "<STR_LIT>" % ( val , self . syntactic_help ) ) <EOL> return val <EOL> def WriteCustomInfoInXMLFormat ( self , outfile , indent ) : <EOL> if self . lower_bound is not None : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , self . lower_bound , indent ) <EOL> if self . upper_bound is not None : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , self . upper_bound , indent ) <EOL> def Convert ( self , argument ) : <EOL> """<STR_LIT>""" <EOL> return argument <EOL> class FloatParser ( NumericParser ) : <EOL> """<STR_LIT>""" <EOL> number_article = "<STR_LIT:a>" <EOL> number_name = "<STR_LIT>" <EOL> syntactic_help = "<STR_LIT:U+0020>" . join ( ( number_article , number_name ) ) <EOL> def __init__ ( self , lower_bound = None , upper_bound = None ) : <EOL> super ( FloatParser , self ) . __init__ ( ) <EOL> self . lower_bound = lower_bound <EOL> self . upper_bound = upper_bound <EOL> sh = self . syntactic_help <EOL> if lower_bound is not None and upper_bound is not None : <EOL> sh = ( "<STR_LIT>" % ( sh , lower_bound , upper_bound ) ) <EOL> elif lower_bound == <NUM_LIT:0> : <EOL> sh = "<STR_LIT>" % self . number_name <EOL> elif upper_bound == <NUM_LIT:0> : <EOL> sh = "<STR_LIT>" % self . number_name <EOL> elif upper_bound is not None : <EOL> sh = "<STR_LIT>" % ( self . number_name , upper_bound ) <EOL> elif lower_bound is not None : <EOL> sh = "<STR_LIT>" % ( self . number_name , lower_bound ) <EOL> self . syntactic_help = sh <EOL> def Convert ( self , argument ) : <EOL> """<STR_LIT>""" <EOL> return float ( argument ) <EOL> def Type ( self ) : <EOL> return '<STR_LIT:float>' <EOL> def DEFINE_float ( name , default , help , lower_bound = None , upper_bound = None , <EOL> flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = FloatParser ( lower_bound , upper_bound ) <EOL> serializer = ArgumentSerializer ( ) <EOL> DEFINE ( parser , name , default , help , flag_values , serializer , ** args ) <EOL> _RegisterBoundsValidatorIfNeeded ( parser , name , flag_values = flag_values ) <EOL> class IntegerParser ( NumericParser ) : <EOL> """<STR_LIT>""" <EOL> number_article = "<STR_LIT>" <EOL> number_name = "<STR_LIT>" <EOL> syntactic_help = "<STR_LIT:U+0020>" . join ( ( number_article , number_name ) ) <EOL> def __init__ ( self , lower_bound = None , upper_bound = None ) : <EOL> super ( IntegerParser , self ) . __init__ ( ) <EOL> self . lower_bound = lower_bound <EOL> self . upper_bound = upper_bound <EOL> sh = self . syntactic_help <EOL> if lower_bound is not None and upper_bound is not None : <EOL> sh = ( "<STR_LIT>" % ( sh , lower_bound , upper_bound ) ) <EOL> elif lower_bound == <NUM_LIT:1> : <EOL> sh = "<STR_LIT>" % self . number_name <EOL> elif upper_bound == - <NUM_LIT:1> : <EOL> sh = "<STR_LIT>" % self . number_name <EOL> elif lower_bound == <NUM_LIT:0> : <EOL> sh = "<STR_LIT>" % self . number_name <EOL> elif upper_bound == <NUM_LIT:0> : <EOL> sh = "<STR_LIT>" % self . number_name <EOL> elif upper_bound is not None : <EOL> sh = "<STR_LIT>" % ( self . number_name , upper_bound ) <EOL> elif lower_bound is not None : <EOL> sh = "<STR_LIT>" % ( self . number_name , lower_bound ) <EOL> self . syntactic_help = sh <EOL> def Convert ( self , argument ) : <EOL> __pychecker__ = '<STR_LIT>' <EOL> if type ( argument ) == str : <EOL> base = <NUM_LIT:10> <EOL> if len ( argument ) > <NUM_LIT:2> and argument [ <NUM_LIT:0> ] == "<STR_LIT:0>" and argument [ <NUM_LIT:1> ] == "<STR_LIT:x>" : <EOL> base = <NUM_LIT:16> <EOL> return int ( argument , base ) <EOL> else : <EOL> return int ( argument ) <EOL> def Type ( self ) : <EOL> return '<STR_LIT:int>' <EOL> def DEFINE_integer ( name , default , help , lower_bound = None , upper_bound = None , <EOL> flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = IntegerParser ( lower_bound , upper_bound ) <EOL> serializer = ArgumentSerializer ( ) <EOL> DEFINE ( parser , name , default , help , flag_values , serializer , ** args ) <EOL> _RegisterBoundsValidatorIfNeeded ( parser , name , flag_values = flag_values ) <EOL> class EnumParser ( ArgumentParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , enum_values = None ) : <EOL> super ( EnumParser , self ) . __init__ ( ) <EOL> self . enum_values = enum_values <EOL> def Parse ( self , argument ) : <EOL> if self . enum_values and argument not in self . enum_values : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> "<STR_LIT:|>" . join ( self . enum_values ) ) <EOL> return argument <EOL> def Type ( self ) : <EOL> return '<STR_LIT>' <EOL> class EnumFlag ( Flag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , default , help , enum_values = None , <EOL> short_name = None , ** args ) : <EOL> enum_values = enum_values or [ ] <EOL> p = EnumParser ( enum_values ) <EOL> g = ArgumentSerializer ( ) <EOL> Flag . __init__ ( self , p , g , name , default , help , short_name , ** args ) <EOL> if not self . help : self . help = "<STR_LIT>" <EOL> self . help = "<STR_LIT>" % ( "<STR_LIT:|>" . join ( enum_values ) , self . help ) <EOL> def _WriteCustomInfoInXMLFormat ( self , outfile , indent ) : <EOL> for enum_value in self . parser . enum_values : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , enum_value , indent ) <EOL> def DEFINE_enum ( name , default , enum_values , help , flag_values = FLAGS , <EOL> ** args ) : <EOL> """<STR_LIT>""" <EOL> DEFINE_flag ( EnumFlag ( name , default , help , enum_values , ** args ) , <EOL> flag_values ) <EOL> class BaseListParser ( ArgumentParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , token = None , name = None ) : <EOL> assert name <EOL> super ( BaseListParser , self ) . __init__ ( ) <EOL> self . _token = token <EOL> self . _name = name <EOL> self . syntactic_help = "<STR_LIT>" % self . _name <EOL> def Parse ( self , argument ) : <EOL> if isinstance ( argument , list ) : <EOL> return argument <EOL> elif argument == '<STR_LIT>' : <EOL> return [ ] <EOL> else : <EOL> return [ s . strip ( ) for s in argument . split ( self . _token ) ] <EOL> def Type ( self ) : <EOL> return '<STR_LIT>' % self . _name <EOL> class ListParser ( BaseListParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> BaseListParser . __init__ ( self , '<STR_LIT:U+002C>' , '<STR_LIT>' ) <EOL> def WriteCustomInfoInXMLFormat ( self , outfile , indent ) : <EOL> BaseListParser . WriteCustomInfoInXMLFormat ( self , outfile , indent ) <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , repr ( '<STR_LIT:U+002C>' ) , indent ) <EOL> class WhitespaceSeparatedListParser ( BaseListParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> BaseListParser . __init__ ( self , None , '<STR_LIT>' ) <EOL> def WriteCustomInfoInXMLFormat ( self , outfile , indent ) : <EOL> BaseListParser . WriteCustomInfoInXMLFormat ( self , outfile , indent ) <EOL> separators = list ( string . whitespace ) <EOL> separators . sort ( ) <EOL> for ws_char in string . whitespace : <EOL> _WriteSimpleXMLElement ( outfile , '<STR_LIT>' , repr ( ws_char ) , indent ) <EOL> def DEFINE_list ( name , default , help , flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = ListParser ( ) <EOL> serializer = ListSerializer ( '<STR_LIT:U+002C>' ) <EOL> DEFINE ( parser , name , default , help , flag_values , serializer , ** args ) <EOL> def DEFINE_spaceseplist ( name , default , help , flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = WhitespaceSeparatedListParser ( ) <EOL> serializer = ListSerializer ( '<STR_LIT:U+0020>' ) <EOL> DEFINE ( parser , name , default , help , flag_values , serializer , ** args ) <EOL> class MultiFlag ( Flag ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> Flag . __init__ ( self , * args , ** kwargs ) <EOL> self . help += '<STR_LIT>' <EOL> def Parse ( self , arguments ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( arguments , list ) : <EOL> arguments = [ arguments ] <EOL> if self . present : <EOL> values = self . value <EOL> else : <EOL> values = [ ] <EOL> for item in arguments : <EOL> Flag . Parse ( self , item ) <EOL> values . append ( self . value ) <EOL> self . value = values <EOL> def Serialize ( self ) : <EOL> if not self . serializer : <EOL> raise FlagsError ( "<STR_LIT>" % self . name ) <EOL> if self . value is None : <EOL> return '<STR_LIT>' <EOL> s = '<STR_LIT>' <EOL> multi_value = self . value <EOL> for self . value in multi_value : <EOL> if s : s += '<STR_LIT:U+0020>' <EOL> s += Flag . Serialize ( self ) <EOL> self . value = multi_value <EOL> return s <EOL> def Type ( self ) : <EOL> return '<STR_LIT>' + self . parser . Type ( ) <EOL> def DEFINE_multi ( parser , serializer , name , default , help , flag_values = FLAGS , <EOL> ** args ) : <EOL> """<STR_LIT>""" <EOL> DEFINE_flag ( MultiFlag ( parser , serializer , name , default , help , ** args ) , <EOL> flag_values ) <EOL> def DEFINE_multistring ( name , default , help , flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = ArgumentParser ( ) <EOL> serializer = ArgumentSerializer ( ) <EOL> DEFINE_multi ( parser , serializer , name , default , help , flag_values , ** args ) <EOL> def DEFINE_multi_int ( name , default , help , lower_bound = None , upper_bound = None , <EOL> flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = IntegerParser ( lower_bound , upper_bound ) <EOL> serializer = ArgumentSerializer ( ) <EOL> DEFINE_multi ( parser , serializer , name , default , help , flag_values , ** args ) <EOL> def DEFINE_multi_float ( name , default , help , lower_bound = None , upper_bound = None , <EOL> flag_values = FLAGS , ** args ) : <EOL> """<STR_LIT>""" <EOL> parser = FloatParser ( lower_bound , upper_bound ) <EOL> serializer = ArgumentSerializer ( ) <EOL> DEFINE_multi ( parser , serializer , name , default , help , flag_values , ** args ) <EOL> DEFINE_flag ( HelpFlag ( ) ) <EOL> DEFINE_flag ( HelpshortFlag ( ) ) <EOL> DEFINE_flag ( HelpXMLFlag ( ) ) <EOL> _SPECIAL_FLAGS = FlagValues ( ) <EOL> DEFINE_string ( <EOL> '<STR_LIT>' , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> _SPECIAL_FLAGS ) <EOL> DEFINE_string ( <EOL> '<STR_LIT>' , "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , _SPECIAL_FLAGS ) </s>
