3 ‰å<_°3ã @sÞdZddlZddlZddlmZmZmZmZmZddlm Z ej dkrTe Z dd„Z neZ e Z dd lmZdd lmZe d ƒZejejƒƒZd d dddddddg ZGdd„dejƒZGdd„dejƒZGdd„dejƒZeZdS)a,GOption command line parser Extends optparse to use the GOptionGroup, GOptionEntry and GOptionContext objects. So it is possible to use the gtk, gnome_program and gstreamer command line groups and contexts. Use this interface instead of the raw wrappers of GOptionContext and GOptionGroup in glib. éN)Ú OptParseErrorÚ OptionErrorÚOptionValueErrorÚBadOptionErrorÚOptionConflictErroré)Úget_introspection_moduleécCs|jƒS)N)Úencode)Ús©r ú/usr/lib64/python3.6/_option.pyÚ&sr)Ú_gi)ÚGErrorÚGLibrrrrrÚOptionÚ OptionGroupÚ OptionParserÚ make_optionc@sPeZdZdZejjdZejjdddgZdej Z dd„Z d d „Z d d „Z d S)ra¹Represents a command line option To use the extended possibilities of the GOption API Option (and make_option) are extended with new types and attributes. Types: filename The supplied arguments are read as filename, GOption parses this type in with the GLib filename encoding. :ivar optional_arg: This does not need a arguement, but it can be supplied. :ivar hidden: The help list does not show this option :ivar in_main: This option apears in the main group, this should only be used for backwards compatibility. Use Option.REMAINING as option name to get all positional arguments. .. NOTE:: Every argument to an option is passed as utf-8 coded string, the only exception are options which use the 'filename' type, its arguments are passed as strings in the GLib filename encoding. For further help, see optparse.Option. ÚfilenameÚhiddenÚin_mainÚ optional_argz--cOsXtjj|f|ž|Ž|js"tdƒ‚t|jƒt|jƒkr>tdƒ‚|jsTtd|jdƒ‚dS)Nz!%s at least one long option name.z;%s at least more long option names than short option names.z%s needs a help message.r)ÚoptparserÚ__init__Ú _long_optsÚ ValueErrorÚlenÚ _short_optsÚhelp)ÚselfÚargsÚkwargsr r r reszOption.__init__cCsF|j|kr|jj|jƒtjj||ƒt|jƒt|jƒkrBtdƒ‚dS)NzCgoption.Option needs more long option names than short option names) Ú REMAININGrÚappendrrÚ_set_opt_stringrrr)r!Zoptsr r r r&qs  zOption._set_opt_stringccsèd}|jr|tjjO}|jr(|tjjO}|jƒrD|jrP|tjjO}n |tjj O}|j dkrf|tjj O}x>t |j |jƒD],\}}|dd…t|dƒ||j|jfVqvWx<|j t|jƒd…D]$}|dd…tdƒ||j|jfVq¼WdS)Nrrérú)rrZ OptionFlagsZHIDDENrZIN_MAINZ takes_valuerZ OPTIONAL_ARGZNO_ARGÚtypeZFILENAMEÚziprrÚ_bytesr Úmetavarr)r!ÚflagsZ long_nameZ short_namer r r Ú_to_goptionentriesys     (zOption._to_goptionentriesN)r)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrZTYPESZATTRSrZOPTION_REMAININGr$rr&r.r r r r r>s  c@s<eZdZdZddd„Zdd„Zdd „Zdd d „Zd d „ZdS)ra÷A group of command line options. :param str name: The groups name, used to create the --help-{name} option :param str description: Shown as title of the groups help view :param str help_description: Shown as help to the --help-{name} option :param list option_list: The options used in this group, must be option.Option() :param dict defaults: A dicitionary of default values :param translation_domain: Sets the translation domain for gettext(). .. NOTE:: This OptionGroup does not exactly map the optparse.OptionGroup interface. There is no parser object to supply, but it is possible to set default values and option_lists. Also the default values and values are not shared with the OptionParser. To pass a OptionGroup into a function which expects a GOptionGroup (e.g. gnome_program_init() ). OptionGroup.get_option_group() can be used. For further help, see optparse.OptionGroup. ÚNcCsZtjj|td|ƒ||_d|_||_|r.||_d|_||_ |rVx|D]}|j |ƒqDWdS)NÚerror) rZOptionContainerrrÚnameÚparserÚhelp_descriptionÚdefaultsÚvaluesÚtranslation_domainZ add_option)r!r5Ú descriptionr7Ú option_listr8r:Úoptionr r r r­s zOptionGroup.__init__cCsg|_|jƒdS)N)r<Z_create_option_mappings)r!r r r Ú_create_option_list¿szOptionGroup._create_option_listcsf‡‡fdd„}tjˆjˆjˆj|ƒ}ˆjr6|jˆjƒg}xˆjD]}|j|j ƒƒqBW|j |ƒ|S)Nc s†|jdƒrˆj|}n ˆj|}y|j||ˆjˆƒWnJtk r€tjƒd}tt |ƒƒ}t |_ t j j|_t |ƒ|_|‚YnXdS)Nz--r)Ú startswithÚ _long_optÚ _short_optÚprocessr9rÚsysÚexc_inforÚstrÚOPTION_CONTEXT_ERROR_QUARKÚdomainrrÚ BAD_VALUEÚcodeÚmessage)Ú option_nameÚ option_valueÚgroupÚoptr4Zgerror)r6r!r r ÚcallbackÄs       z.OptionGroup._to_goptiongroup..callback) rrr5r;r7r:Zset_translation_domainr<Úextendr.Ú add_entries)r!r6rOrMZentriesr=r )r6r!r Ú_to_goptiongroupÃs     zOptionGroup._to_goptiongroupcCs|jƒ|j|ƒS)z€ Returns the corresponding GOptionGroup object. Can be used as parameter for gnome_program_init(), gtk_init(). )Úset_values_to_defaultsrR)r!r6r r r Úget_option_groupászOptionGroup.get_option_groupcCsVxB|jD]8}|jj|jƒ}t|tƒr|jƒ}|j||ƒ|j|j<qWtj |jƒ|_ dS)N) r<r8ÚgetÚdestÚ isinstanceÚ _basestringZget_opt_stringZ check_valuerZValuesr9)r!r=ÚdefaultZopt_strr r r rSés  z"OptionGroup.set_values_to_defaults)r3NNN)N) r/r0r1r2rr>rRrTrSr r r r r’s  c@sJeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z ddd„Z dS)raHCommand line parser with GOption support. :param bool help_enabled: The --help, --help-all and --help-{group} options are enabled (default). :param bool ignore_unknown_options: Do not throw a exception when a option is not knwon, the option will be in the result list. .. NOTE:: The OptionParser interface is not the exactly the same as the optparse.OptionParser interface. Especially the usage parameter is only used to show the metavar of the arguements. OptionParser.add_option_group() does not only accept OptionGroup instances but also glib.OptionGroup, which is returned by gtk_get_option_group(). Only glib.option.OptionGroup and glib.option.Option instances should be passed as groups and options. For further help, see optparse.OptionParser. cOsLd|krt|d<|jddƒ|_|jddƒ|_tjj|f|žddi|—ŽdS)NZ option_classÚ help_enabledTÚignore_unknown_optionsFZadd_help_option)rÚpoprZr[rrr)r!r"r#r r r r s zOptionParser.__init__cCs8|dkrd|_n$|jdƒr.|tdƒd…|_n||_dS)Nr3z%prog)Úusager?r)r!r]r r r Ú set_usages  zOptionParser.set_usagec sΈjrˆjdˆj}nˆj}tj|ƒ}|jˆjƒ|jˆjƒx4ˆjD]*}t |tj ƒr^|}n |j ˆƒ}|j |ƒqHW‡‡fdd„}tj ddd|ƒ}g}xˆj D]} |j| jƒƒq W|j|ƒ|j|ƒ|S)Nz - cs4|jdƒrˆj|}n ˆj|}|j||ˆˆƒdS)Nz--)r?r@rArB)rKrLrMrN)r!r9r r rO+s   z1OptionParser._to_goptioncontext..callback)r;r]rZ OptionContextZset_help_enabledrZZset_ignore_unknown_optionsr[Ú option_groupsrWrrTZ add_groupr<rPr.rQZset_main_group) r!r9Zparameter_stringÚcontextZ option_groupZg_grouprOZ main_groupZ main_entriesr=r )r!r9r Ú_to_goptioncontexts&         zOptionParser._to_goptioncontextcOs²t|dtƒr,tjj|t|f|ž|ŽƒdSt|ƒdkrš| ršt|dtƒrv|djs`||d_|dj|k rvtdƒ‚t|dt jƒrš|j j |dƒdStjj|f|ž|ŽdS)Nrrz"invalid OptionGroup (wrong parser)) rWrXrrÚadd_option_grouprrr6rrr_r%)r!r"r#r r r rb;s  zOptionParser.add_option_groupcCs:|jdd…}x&|jD]}t|tjƒr|j|jƒqW|S)N)r<r_rWrrrP)r!ÚoptionsrMr r r Ú_get_all_optionsKs   zOptionParser._get_all_optionscCs4|j|ƒ}|jtjdg|ƒdd…|dd…<dS)Nrr)raÚparserCÚargv)r!ZlargsZrargsr9r`r r r Ú _process_argsRs zOptionParser._process_argsNc Csî|pg}ytjj|||ƒ\}}Wn|tk rœtjƒd}|jtkrH‚|jt j j krbt |j ƒ‚n6|jt j jkr|t|j ƒ‚n|jt j jkr–t|j ƒ‚n‚YnXx4|jD]*}x$|jjjƒD]\}}|j||ƒq¸Wq¦W|dt|ƒ …}||fS)Nrr')rrÚ parse_argsrrCrDrGrFrIrrrHrrJZUNKNOWN_OPTIONrZFAILEDrr_r9Ú__dict__ÚitemsZ ensure_valuer) r!r"r9Zold_argsrcr4rMÚkeyÚvaluer r r rhZs(      zOptionParser.parse_args)NN) r/r0r1r2rr^rarbrdrgrhr r r r rós  )r r)r2rCrrrrrrÚmodulerÚ version_inforErXr+Z basestringZgirZ gi._errorrrZquark_to_stringZoption_error_quarkrFÚ__all__rrrrr r r r Ús6     Ta