???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
‰å<_°3  ã            	   @   sÞ   d Z ddlZddlZddlmZmZmZmZmZ ddlm	Z	 ej
dkrTeZdd„ ZneZeZdd	lmZ dd
lmZ e	dƒZejejƒ ƒZdddddddddg	ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZeZdS )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é   c             C   s   | j ƒ S )N)Úencode)Ús© r   ú/usr/lib64/python3.6/_option.pyÚ<lambda>&   s    r   )Ú_gi)ÚGErrorÚGLibr   r   r   r   r   ÚOptionÚOptionGroupÚOptionParserÚmake_optionc               @   sP   e Zd ZdZejjd Zejjdddg Zdej	 Z
dd„ Zd	d
„ Zdd„ ZdS )r   a¹  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--c             O   sX   t jj| f|ž|Ž | js"tdƒ‚t| jƒt| jƒk r>tdƒ‚| jsTtd| jd ƒ‚d S )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   r   e   s    zOption.__init__c             C   sF   | j |kr| jj| j ƒ tjj| |ƒ t| jƒt| jƒkrBtdƒ‚d S )NzCgoption.Option needs more long option names than short option names)	Ú	REMAININGr   Úappendr   r   Ú_set_opt_stringr   r   r   )r!   Zoptsr   r   r   r&   q   s
    
zOption._set_opt_stringc             c   sè   d}| j r|tjjO }| jr(|tjjO }| jƒ rD| jrP|tjjO }n|tjj	O }| j
dkrf|tjjO }x>t| j| jƒD ],\}}|dd … t|d ƒ|| j| jfV  qvW x<| jt| jƒd … D ]$}|dd … tdƒ|| j| jfV  q¼W d S )Nr   r   é   r   ú )r   r   ZOptionFlagsZHIDDENr   ZIN_MAINZtakes_valuer   ZOPTIONAL_ARGZNO_ARGÚtypeZFILENAMEÚzipr   r   Ú_bytesr    Úmetavarr   )r!   ÚflagsZ	long_nameZ
short_namer   r   r   Ú_to_goptionentriesy   s    
(zOption._to_goptionentriesN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ZTYPESZATTRSr   ZOPTION_REMAININGr$   r   r&   r.   r   r   r   r   r   >   s   
c               @   s<   e Zd ZdZddd„Zdd„ Zdd	„ Zdd
d„Zdd„ ZdS )r   a÷  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.
    Ú Nc             C   sZ   t jj| td|ƒ || _d | _|| _|r.|| _d | _|| _	|rVx|D ]}| j
|ƒ qDW d S )NÚerror)r   ZOptionContainerr   r   ÚnameÚparserÚhelp_descriptionÚdefaultsÚvaluesÚtranslation_domainZ
add_option)r!   r5   Údescriptionr7   Úoption_listr8   r:   Úoptionr   r   r   r   ­   s    
zOptionGroup.__init__c             C   s   g | _ | jƒ  d S )N)r<   Z_create_option_mappings)r!   r   r   r   Ú_create_option_list¿   s    zOptionGroup._create_option_listc                sf   ‡ ‡fdd„}t jˆjˆjˆj|ƒ}ˆjr6|jˆjƒ g }xˆjD ]}|j|j	ƒ ƒ qBW |j
|ƒ |S )Nc                s†   | j dƒrˆj|  }n
ˆj|  }y|j| |ˆjˆ ƒ W nJ tk
r€   tjƒ d }tt	|ƒƒ}t
|_tjj|_t	|ƒ|_|‚Y nX d S )Nz--r   )Ú
startswithÚ	_long_optÚ
_short_optÚprocessr9   r   ÚsysÚexc_infor   ÚstrÚOPTION_CONTEXT_ERROR_QUARKÚdomainr   r   Ú	BAD_VALUEÚcodeÚmessage)Úoption_nameÚoption_valueÚgroupÚoptr4   Zgerror)r6   r!   r   r   ÚcallbackÄ   s    



z.OptionGroup._to_goptiongroup.<locals>.callback)r   r   r5   r;   r7   r:   Zset_translation_domainr<   Úextendr.   Úadd_entries)r!   r6   rO   rM   Zentriesr=   r   )r6   r!   r   Ú_to_goptiongroupÃ   s    

zOptionGroup._to_goptiongroupc             C   s   | 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!   r6   r   r   r   Úget_option_groupá   s    zOptionGroup.get_option_groupc             C   sV   xB| j D ]8}| jj|jƒ}t|tƒr|jƒ }|j||ƒ| j|j< qW tj	| jƒ| _
d S )N)r<   r8   ÚgetÚdestÚ
isinstanceÚ_basestringZget_opt_stringZcheck_valuer   ZValuesr9   )r!   r=   ÚdefaultZopt_strr   r   r   rS   é   s    
z"OptionGroup.set_values_to_defaults)r3   NNN)N)	r/   r0   r1   r2   r   r>   rR   rT   rS   r   r   r   r   r   ’   s     

c               @   sJ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
dS )r   aH  Command 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.
    c             O   sL   d|krt |d< |jddƒ| _|jddƒ| _tjj| f|žddi|—Ž d S )NZoption_classÚhelp_enabledTÚignore_unknown_optionsFZadd_help_option)r   ÚpoprZ   r[   r   r   r   )r!   r"   r#   r   r   r   r   
  s    
zOptionParser.__init__c             C   s8   |d krd| _ n$|jdƒr.|tdƒd … | _ n|| _ d S )Nr3   z%prog)Úusager?   r   )r!   r]   r   r   r   Ú	set_usage  s
    
zOptionParser.set_usagec       
         sÎ   ˆ j rˆ jd ˆ j  }nˆ j}tj|ƒ}|jˆ jƒ |jˆ jƒ x4ˆ jD ]*}t	|tj
ƒr^|}n
|jˆ ƒ}|j|ƒ qHW ‡ ‡fdd„}tj
d d d |ƒ}g }xˆ jD ]}	|j|	jƒ ƒ q W |j|ƒ |j|ƒ |S )Nz - c                s4   | j dƒrˆ j|  }n
ˆ j|  }|j| |ˆˆ ƒ d S )Nz--)r?   r@   rA   rB   )rK   rL   rM   rN   )r!   r9   r   r   rO   +  s    

z1OptionParser._to_goptioncontext.<locals>.callback)r;   r]   r   ZOptionContextZset_help_enabledrZ   Zset_ignore_unknown_optionsr[   Úoption_groupsrW   r   rT   Z	add_groupr<   rP   r.   rQ   Zset_main_group)
r!   r9   Zparameter_stringÚcontextZoption_groupZg_grouprO   Z
main_groupZmain_entriesr=   r   )r!   r9   r   Ú_to_goptioncontext  s&    



zOptionParser._to_goptioncontextc             O   s²   t |d tƒr,tjj| t| f|ž|Žƒ d S t|ƒdkrš| ršt |d tƒrv|d js`| |d _|d j| k	rvtdƒ‚t |d t	jƒrš| j
j|d ƒ d S tjj| f|ž|Ž d S )Nr   r   z"invalid OptionGroup (wrong parser))rW   rX   r   r   Úadd_option_groupr   r   r6   r   r   r_   r%   )r!   r"   r#   r   r   r   rb   ;  s    

zOptionParser.add_option_groupc             C   s:   | j d d … }x&| jD ]}t|tjƒr|j|j ƒ qW |S )N)r<   r_   rW   r   r   rP   )r!   ÚoptionsrM   r   r   r   Ú_get_all_optionsK  s
    zOptionParser._get_all_optionsc             C   s4   | j |ƒ}|jtjd g| ƒdd … |d d …< d S )Nr   r   )ra   ÚparserC   Úargv)r!   ZlargsZrargsr9   r`   r   r   r   Ú_process_argsR  s    
zOptionParser._process_argsNc       	      C   sî   |pg }yt jj| ||ƒ\}}W n| tk
rœ   tjƒ d }|jtkrH‚ |jt	j
jkrbt|jƒ‚n6|jt	j
jkr|t|jƒ‚n|jt	j
jkr–t|jƒ‚n‚ Y nX x4| jD ]*}x$|jjjƒ D ]\}}|j||ƒ q¸W q¦W |dt|ƒ … }||fS )Nr   r'   )r   r   Ú
parse_argsr   rC   rD   rG   rF   rI   r   r   rH   r   rJ   ZUNKNOWN_OPTIONr   ZFAILEDr   r_   r9   Ú__dict__ÚitemsZensure_valuer   )	r!   r"   r9   Zold_argsrc   r4   rM   ÚkeyÚvaluer   r   r   rh   Z  s(    
zOptionParser.parse_args)NN)r/   r0   r1   r2   r   r^   ra   rb   rd   rg   rh   r   r   r   r   r   ó   s   	 )r	   r   )r2   rC   r   r   r   r   r   r   Úmoduler   Úversion_inforE   rX   r+   Z
basestringZgir   Z	gi._errorr   r   Zquark_to_stringZoption_error_quarkrF   Ú__all__r   r   r   r   r   r   r   r   Ú<module>   s6   

Ta 