U
    	^.1                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ eeeZi ZG dd dejZG d	d
 d
eZdd Zdd ZeZdd Zdd Ze i i edfddZdddZdS )    N)
get_loader)PyGIDeprecationWarning)CallableInfo)	TYPE_NONETYPE_INVALID)extend_pathc                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
OverridesProxyModulez7Wraps a introspection module and contains all overridesc                    s   t t| |j || _d S N)superr   __init____name___introspection_module)selfintrospection_module	__class__ 7/usr/lib/python3/dist-packages/gi/overrides/__init__.pyr      s    
zOverridesProxyModule.__init__c                 C   s   t | j|S r	   )getattrr   )r   namer   r   r   __getattr__   s    z OverridesProxyModule.__getattr__c                 C   s6   t t| j}|| j  |t| j t|S r	   )setdirr   update__dict__keysr   sorted)r   resultr   r   r   __dir__"   s    zOverridesProxyModule.__dir__c                 C   s   dt | j| jf S )Nz<%s %r>)typer   r   )r   r   r   r   __repr__(   s    zOverridesProxyModule.__repr__)	r   
__module____qualname____doc__r   r   r   r    __classcell__r   r   r   r   r      s
   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_DeprecatedAttributezA deprecation descriptor for OverridesProxyModule subclasses.

    Emits a PyGIDeprecationWarning on every access and tries to act as a
    normal instance attribute (can be replaced and deleted).
    c                 C   s$   || _ || _td|||f | _d S )Nz#%s.%s is deprecated; use %s instead)_attr_valuer   _warning)r   	namespaceattrvaluereplacementr   r   r   r   3   s      z_DeprecatedAttribute.__init__c                 C   s(   |d krt | jtj| jdd | jS )N   
stacklevel)AttributeErrorr&   warningswarnr(   r'   )r   instanceownerr   r   r   __get__:   s    
z_DeprecatedAttribute.__get__c                 C   s$   | j }tt|| t||| d S r	   )r&   delattrr   setattr)r   r3   r+   r*   r   r   r   __set__@   s    z_DeprecatedAttribute.__set__c                 C   s   t t|| j d S r	   )r6   r   r&   )r   r3   r   r   r   
__delete__F   s    z_DeprecatedAttribute.__delete__N)r   r!   r"   r#   r   r5   r8   r9   r   r   r   r   r%   ,   s
   r%   c              
   C   s  | j ddd }d| }|tjk}tj|}t|d tfi }|| }|tj|< ddlm} t|dsnt	|||< zLd	| }zt
|}	W n tk
r   d
}	Y nX |	d
kr| W S t|}
W 5 ||= tj|= |r|tj|< X ||_g }t|
dr|
j}|D ]B}zt|
|}W n  ttfk
r2   Y q Y nX t||| q t|g D ]`\}}zt||}W n" tk
r   t	d| Y nX t|| t||||}t||| qP|S )zLoads overrides for an introspection module.

    Either returns the same module again in case there are no overrides or a
    proxy module including overrides. Doesn't cache the result.
    .   gi.repository.ZProxyModuler-   )modulesr   zgi.overrides.N__all__z1%s was set deprecated but wasn't added to __all__)r   rsplitsysr>   getr   r   importerhasattrAssertionErrorr   r0   	importlibimport_moduleZ_overrides_moduler?   r   	TypeErrorr7   _deprecated_attrspopr6   r%   )r   r)   Z
module_keyZhas_oldZ
old_moduleZ
proxy_typeproxyr>   Zoverride_package_nameZoverride_loaderZoverride_modZoverride_allvaritemr*   r,   r+   deprecated_attrr   r   r   load_overridesK   sb    





   rO   c                    s   t | tr>| }|jddd }tjd|    fdd}|S t | tjrXtd|  nzt	| d}W n" t
k
r   td	| j Y nX | jd
std| | jf | }|tkst|tkr| |_| jddd }tjd|   t | j|  | S dS )a  Decorator for registering an override.

    Other than objects added to __all__, these can get referenced in the same
    override module via the gi.repository module (get_parent_for_object() does
    for example), so they have to be added to the module immediately.
    r:   r;   r<   r=   c                    s   t  | j|  | S r	   )r7   r   )funcmoduler   r   wrapper   s    zoverride.<locals>.wrapperz"func must be a gi function, got %sZ__info__zKCan not override a type %s, which is not in a gobject introspection typelibzgi.overrideszUYou have tried override outside of the overrides module. This is not allowed (%s, %s)N)
isinstancer   r!   r@   rA   r>   typesFunctionTyperH   r   r0   r   
startswithKeyErrorZ
get_g_typer   rE   r   Zpytyper7   )Ztype_rP   r)   rS   infoZg_typer   rQ   r   override   s<    

rZ   c                    s   t   fdd}|S )z7Decorator for marking methods and classes as deprecatedc                     s$   t jd jf tdd  | |S )Nz %s is deprecated; use %s insteadr-   r.   )r1   r2   r   r   )argskwargsfnr,   r   r   wrapped   s
     zdeprecated.<locals>.wrapped	functoolswraps)r^   r,   r_   r   r]   r   
deprecated   s    rc   c                 C   s   t | g ||f dS )a:  Marks a module level attribute as deprecated. Accessing it will emit
    a PyGIDeprecationWarning warning.

    e.g. for ``deprecated_attr("GObject", "STATUS_FOO", "GLib.Status.FOO")``
    accessing GObject.STATUS_FOO will emit:

        "GObject.STATUS_FOO is deprecated; use GLib.Status.FOO instead"

    :param str namespace:
        The namespace of the override this is called in.
    :param str namespace:
        The attribute name (which gets added to __all__).
    :param str replacement:
        The replacement text which will be included in the warning.
    N)rI   
setdefaultappend)r)   r*   r,   r   r   r   rN      s    rN   r-   c                    s    fdd}|S )a  Wrapper for deprecating GObject based __init__ methods which specify
    defaults already available or non-standard defaults.

    :param callable super_init_func:
        Initializer to wrap.
    :param list arg_names:
        Ordered argument name list.
    :param list ignore:
        List of argument names to ignore when calling the wrapped function.
        This is useful for function which take a non-standard keyword that is munged elsewhere.
    :param dict deprecated_aliases:
        Dictionary mapping a keyword alias to the actual g_object_newv keyword.
    :param dict deprecated_defaults:
        Dictionary of non-standard defaults that will be used when the
        keyword is not explicitly passed.
    :param Exception category:
        Exception category of the error.
    :param int stacklevel:
        Stack level for the deprecation passed on to warnings.warn
    :returns: Wrapped version of ``super_init_func`` which gives a deprecation
        warning when non-keyword args or aliases are used.
    :rtype: callable
    c           	         sN  |r:t jdd dt|  d tt |}ni }|| g } D ](\}}||krT||||< |	| qT|rt jddfddt
|D dt
|f d g } D ]&\}}||krȈ| ||< |	| q|r"t jddfd	dt
|D  d D ]}||kr&|| q&| f|S )
zInitializer for a GObject based classes with support for property
        sets through the use of explicit keyword arguments.
        zUsing positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "%s" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecationsz, Nr.   zThe keyword(s) "%s" have been deprecated in favor of "%s" respectively. See: https://wiki.gnome.org/PyGObject/InitializerDeprecationsc                 3   s   | ]} | V  qd S r	   r   .0k)deprecated_aliasesr   r   	<genexpr>(  s     z4deprecated_init.<locals>.new_init.<locals>.<genexpr>zInitializer is relying on deprecated non-standard defaults. Please update to explicitly use: %s See: https://wiki.gnome.org/PyGObject/InitializerDeprecationsc                 3   s   | ]}d | | f V  qdS )z%s=%sNr   rf   )deprecated_defaultsr   r   rj   7  s     )r1   r2   joinlendictzipr   itemsrJ   re   r   )	r   r[   r\   Z
new_kwargsZaliases_usedkeyaliasZdefaults_usedr+   	arg_namescategoryri   rk   ignorer/   super_init_funcr   r   new_init  sP     
  
z!deprecated_init.<locals>.new_initr   )rw   rt   rv   ri   rk   ru   r/   rx   r   rs   r   deprecated_init   s    3ry   c                    s    t  fdd}|S )a  Translate method's return value for stripping off success flag.

    There are a lot of methods which return a "success" boolean and have
    several out arguments. Translate such a method to return the out arguments
    on success and None on failure.
    c                     sL   | |}|d r4t |dkr&|d S |dd  S nrD p@dS d S )Nr   r-   r;   zcall failed)rm   )r[   r\   Zretexc_strexc_typefail_retmethodr   r   r_   K  s    
z%strip_boolean_result.<locals>.wrappedr`   )r~   r|   r{   r}   r_   r   rz   r   strip_boolean_resultD  s    r   )NNN)ra   rU   r1   rF   rA   pkgutilr   Zgir   Zgi._gir   Zgi._constantsr   r   r   __path__r   rI   
ModuleTyper   objectr%   rO   rZ   Zoverridefuncrc   rN   tuplery   r   r   r   r   r   <module>   s2   
V-
 
T