
    f5                     <   d Z ddl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
 ddlmZmZ ddlmZ  e
d      Zej#                  ej%                               Zg d	Z G d
 dej*                        Z G d dej,                        Z G d dej.                        ZeZy)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)_giPyGIDeprecationWarning)GErrorGLib)	r   r   r   r   r   OptionOptionGroupOptionParsermake_optionc                       e Zd ZdZej
                  j                  dz   Zej
                  j                  g dz   Zdej                  z   Z
d Zd Zd Zy)	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_arg--c                 b   t        j                  dt               t        j                  j
                  | g|i | | j                  st        d      t        | j                        t        | j                        k  rt        d      | j                  st        d| j                  d         y )NzYgi.repository.GLib.option.Option is depecated, use gi.repository.GLib.OptionEntry insteadz!%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   )warningswarnr   optparser   __init__
_long_opts
ValueErrorlen_short_optshelpselfargskwargss      ,/usr/lib/python3/dist-packages/gi/_option.pyr   zOption.__init___   s    g"	
 	  777@AAt#d&6&6"77MO O yy79KLL     c                    | j                   |v r%| j                  j                  | j                          t        j                  j                  | |       t        | j                        t        | j                        kD  rt        d      y )NzCgoption.Option needs more long option names than short option names)		REMAININGr   appendr   r   _set_opt_stringr   r    r   )r#   optss     r&   r+   zOption._set_opt_stringo   sk    >>T!OO""4>>2''d3t 3t#77 8 9 9 8r'   c              #   $  K   d}| j                   r|t        j                  j                  z  }| j                  r|t        j                  j
                  z  }| j                         r*| j                  r;|t        j                  j                  z  }n|t        j                  j                  z  }| j                  dk(  r|t        j                  j                  z  }t        | j                  | j                        D ]K  \  }}|d   }t        |t               s|j#                  d      }|dd  ||| j$                  | j&                  f M | j                  t)        | j                        d  D ]"  }|dd  d|| j$                  | j&                  f $ y w)Nr   r   r   zutf-8       )r   r   OptionFlagsHIDDENr   IN_MAINtakes_valuer   OPTIONAL_ARGNO_ARGtypeFILENAMEzipr   r    
isinstancebytesencoder!   metavarr   )r#   flags	long_name
short_nameshort_bytess        r&   _to_goptionentrieszOption._to_goptionentriesw   sZ    ;;T%%,,,E<<T%%---E  ))666T%%,,,E99
"T%%...E'*4??D<L<L'M 	O#Y
$Q-Kk51)009QR=+udiiNN		O T-=-=)>)?@ 	IIQR=%		4<<HH	Is   FFN)__name__
__module____qualname____doc__r   r   TYPESATTRSr   OPTION_REMAININGr)   r   r+   rA    r'   r&   r   r   8   s`    4 OO!! % E OO!! % E t,,,IM 9Ir'   r   c                   8    e Zd ZdZ	 	 	 ddZd Zd Zd	dZd Zy)
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                    t        j                  dt               t        j                  j                  | t        d|       || _        d | _        || _	        |r|| _
        d | _        || _        |r|D ]  }| j                  |        y y )Nz`gi.repository.GLib.option.OptionGroup is depecated, use gi.repository.GLib.OptionContext insteaderror)r   r   r   r   OptionContainerr   r   nameparserhelp_descriptiondefaultsvaluestranslation_domain
add_option)r#   rN   descriptionrP   option_listrQ   rS   options           r&   r   zOptionGroup.__init__   s     	n"	
 	  ))$M	 0$DM"4% ('( r'   c                 2    g | _         | j                          y N)rV   _create_option_mappings)r#   s    r&   _create_option_listzOptionGroup._create_option_list   s    $$&r'   c                 V     fd}t        j                   j                   j                   j                  |      } j
                  r|j                   j
                         g } j                  D ]!  }|j                  |j                                # |j                  |       |S )Nc                    | j                  d      rj                  |    }nj                  |    }	 |j                  | |j                         y # t
        $ rh t        j                         d   }t        t        |            }t        |_        t        j                  j                  |_        t        |      |_        |w xY w)Nr   r   )
startswith	_long_opt
_short_optprocessrR   r   sysexc_infor   strOPTION_CONTEXT_ERROR_QUARKdomainr   r   	BAD_VALUEcodemessage)option_nameoption_valuegroupoptrL   gerrorrO   r#   s         r&   callbackz.OptionGroup._to_goptiongroup.<locals>.callback   s    %%d+nn[1ook2Kt{{FK# q)E
+ :"..88!$Us   A A1C)r
   r   rN   rU   rP   rS   set_translation_domainrV   extendrA   add_entries)r#   rO   ro   rl   entriesrW   s   ``    r&   _to_goptiongroupzOptionGroup._to_goptiongroup   s    	  		4+;+; $ 5 5xA""(()@)@A&& 	8FNN64467	8 	'"r'   c                 D    | j                          | j                  |      S )z Returns the corresponding GOptionGroup object.

        Can be used as parameter for gnome_program_init(), gtk_init().
        )set_values_to_defaultsrt   )r#   rO   s     r&   get_option_groupzOptionGroup.get_option_group   s!    
 	##%$$V,,r'   c                 L   | j                   D ]q  }| j                  j                  |j                        }t	        |t
              s9|j                         }|j                  ||      | j                  |j                  <   s t        j                  | j                        | _
        y rY   )rV   rQ   getdestr9   rd   get_opt_stringcheck_valuer   ValuesrR   )r#   rW   defaultopt_strs       r&   rv   z"OptionGroup.set_values_to_defaults   s    && 	&Fmm''4G'3' //1-3-?-?W.&fkk*		& oodmm4r'   ) NNNrY   )	rB   rC   rD   rE   r   r[   rt   rw   rv   rI   r'   r&   r   r      s+    4 <>,0$((,'<-5r'   r   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d
d	Z
y)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                     t        j                  dt               d|vr	t        |d<   |j	                  dd      | _        |j	                  dd      | _        t        j                  j                  | g|ddi| y )Nzagi.repository.GLib.option.OptionParser is depecated, use gi.repository.GLib.OptionContext insteadoption_classhelp_enabledTignore_unknown_optionsFadd_help_option)
r   r   r   r   popr   r   r   r   r   r"   s      r&   r   zOptionParser.__init__  s    o"	
 '%+F>""JJ~t<&,jj1I16'8#&&t 	8(,	8U 	806	8r'   c                 p    |d| _         y |j                  d      r|t        d      d  | _         y || _         y )Nr   z%prog)usager^   r   )r#   r   s     r&   	set_usagezOptionParser.set_usage  s5    =DJg&s7|}-DJDJr'   c                      j                   r j                  dz    j                   z   }n j                  }t        j                  |      }|j	                   j
                         |j                   j                          j                  D ]A  }t        |t        j                        r|}n|j                         }|j                  |       C  fd}t        j                  d d d |      }g } j                  D ]!  }	|j                  |	j                                # |j!                  |       |j#                  |       |S )Nz - c                     | j                  d      rj                  |    }nj                  |    }|j                  | |       y )Nr   )r^   r_   r`   ra   )rj   rk   rl   rm   r#   rR   s       r&   ro   z1OptionParser._to_goptioncontext.<locals>.callback4  s?    %%d+nn[1ook2KK\64@r'   )rU   r   r
   OptionContextset_help_enabledr   set_ignore_unknown_optionsr   option_groupsr9   r   rw   	add_grouprV   rq   rA   rr   set_main_group)
r#   rR   parameter_stringcontextoption_groupg_groupro   
main_groupmain_entriesrW   s
   ``        r&   _to_goptioncontextzOptionParser._to_goptioncontext$  s   #zzE1D4D4DD#zz##$45  !2!23**4+F+FG .. 	'L,8&&77=g&	'	A __T4x@
&& 	=F 9 9 ;<	=|,z*r'   c                    t        |d   t              r.t        j                  j	                  | t        | g|i |       y t        |      dk(  r|st        |d   t
              r5|d   j                  s
| |d   _        |d   j                  | urt        d      t        |d   t        j
                        r| j                  j                  |d          y t        j                  j                  | g|i | y )Nr   r   z"invalid OptionGroup (wrong parser))r9   rd   r   r   add_option_groupr   r   rO   r   r
   r   r*   r"   s      r&   r   zOptionParser.add_option_groupD  s    d1gs#!!2243>t3Ud3Uf3UWY!^F$q';/Aw~~%)DGN7>>-$%IJJ$q'3??3""))$q'2..tEdEfEr'   c                     | j                   d d  }| j                  D ]8  }t        |t        j                        s|j                  |j                          : |S rY   )rV   r   r9   r   r   rq   )r#   optionsrl   s      r&   _get_all_optionszOptionParser._get_all_optionsT  sP    ""1%'' 	2E%!5!56u001	2 r'   c                 ~    | j                  |      }|j                  t        j                  d   g|z         dd  |d d  y )Nr   r   )r   parserb   argv)r#   largsrargsrR   r   s        r&   _process_argszOptionParser._process_args[  s<    ))&1
 ==#((1+!67;ar'   Nc                    	 t         j                  j                  | ||      \  }}| j$                  D ]@  }|j&                  j(                  j+                         D ]  \  }}|j-                  ||        B ||fS # t        $ r t	        j
                         d   }|j                  t        k7  r |j                  t        j                  j                  k(  rt        |j                        |j                  t        j                  j                  k(  rt        |j                        |j                  t        j                  j                   k(  rt#        |j                         w xY w)Nr   )r   r   
parse_argsr   rb   rc   rf   re   rh   r   r   rg   r   ri   UNKNOWN_OPTIONr   FAILEDr   r   rR   __dict__itemsensure_value)r#   r$   rR   r   rL   rl   keyvalues           r&   r   zOptionParser.parse_argsc  s*   	$11<<dF$MGT '' 	1E#ll3399; 1
U$$S%01	1 }#  	LLN1%E||99zzT--777&u}}55t//>>>$U]]33t//666#EMM22	s   $A9 9C*E#)NN)rB   rC   rD   rE   r   r   r   r   r   r   r   rI   r'   r&   r   r      s,    ,8@F <r'   r   )rE   rb   r   r   r   r   r   r   r   moduler	   gir
   r   	gi._errorr   r   quark_to_stringoption_error_quarkre   __all__r   r   r   r   rI   r'   r&   <module>r      s   (   ( ( , * '!11$2I2I2KL 
XIX__ XIvb5(&& b5J@8(( @F r'   