U
    	^7                     @   s   d dl mZ d dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ejZejZej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%G dd de&Z'dd Z(dS )	   )_gi)string_typeslong_)	TYPE_NONETYPE_INTERFACE	TYPE_CHAR
TYPE_UCHARTYPE_BOOLEANTYPE_INT	TYPE_UINT	TYPE_LONG
TYPE_ULONG
TYPE_INT64TYPE_UINT64	TYPE_ENUM
TYPE_FLAGS
TYPE_FLOATTYPE_DOUBLETYPE_STRINGTYPE_POINTER
TYPE_BOXED
TYPE_PARAMTYPE_OBJECTTYPE_PYOBJECT
TYPE_GTYPE	TYPE_STRVTYPE_VARIANTc                   @   sN  e Zd ZdZeeeeee	e
eeeeeiZedededee ee eeeeediZeeeeededeeeeeeeeiZedededededededededi	Z G dd	 d	e!Z"d
d
d
d
dde#j$d
d
f	ddZ%dd Z&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd  Z/d!d" Z0d#d$ Z1d%d& Z2d'd( Z3d)d* Z4d+d, Z5d
S )-Propertya  Creates a new Property which when used in conjunction with
    GObject subclass will create a Python property accessor for the
    GObject ParamSpec.

    :param callable getter:
        getter to get the value of the property
    :param callable setter:
        setter to set the value of the property
    :param type type:
        type of property
    :param default:
        default value, must match the property type.
    :param str nick:
        short description
    :param str blurb:
        long description
    :param GObject.ParamFlags flags:
        parameter flags
    :keyword minimum:
        minimum allowed value (int, float, long only)
    :keyword maximum:
        maximum allowed value (int, float, long only)

    .. code-block:: python

         class MyObject(GObject.Object):
             prop = GObject.Property(type=str)

         obj = MyObject()
         obj.prop = 'value'

         obj.prop  # now is 'value'

    The API is similar to the builtin :py:func:`property`:

    .. code-block:: python

        class AnotherObject(GObject.Object):
            value = 0

            @GObject.Property
            def prop(self):
                'Read only property.'
                return 1

            @GObject.Property(type=int)
            def propInt(self):
                'Read-write integer property.'
                return self.value

            @propInt.setter
            def propInt(self, value):
                self.value = value
        l         l    l     g        c                   @   s   e Zd Zdd ZdS )zProperty.__metaclass__c                 C   s   dS )Nz<class 'GObject.Property'> selfr    r    4/usr/lib/python3/dist-packages/gi/_propertyhelper.py__repr__   s    zProperty.__metaclass__.__repr__N)__name__
__module____qualname__r$   r    r    r    r#   __metaclass__   s   r(   Nc
           
      C   s4  d | _ |d krt}| || _| || _|   t|tsDt	d|| _
t|ts\t	d|| _|| _|| _|r~|s~| j}n$|r|s| j}n|s|s| j}| j}| | || _|d k	r||  k rt	d| j|  f n|  }|| _|	d k	r|	|  kr$t	d| j|  f n|  }	|	| _d | _d S )Nznick must be a stringzblurb must be a stringz+Minimum for type %s cannot be lower than %dz,Maximum for type %s cannot be higher than %d)nameobject_type_from_pythontype_get_defaultdefault_check_default
isinstancer   	TypeErrornickblurb__doc__flags_readonly_setter_writeonly_getter_default_getter_default_settergetterfset_get_minimumminimum_get_maximummaximum_exc)
r"   r:   setterr,   r.   r2   r3   r5   r=   r?   r    r    r#   __init__   sT    



zProperty.__init__c                 C   s   d| j p
d| jj f S )Nz<GObject Property %s (%s)>z(uninitialized))r)   r,   r!   r    r    r#   r$      s    zProperty.__repr__c                 C   s6   |d kr| S d | _ | |}| j r2| j }d | _ ||S N)r@   fget)r"   instanceklassvalueexcr    r    r#   __get__   s    
zProperty.__get__c                 C   s:   |d krt d | _|| j| | jr6| j}d | _|d S rC   )r1   r@   Zset_propertyr)   )r"   rE   rG   rH   r    r    r#   __set__   s    zProperty.__set__c                 C   s
   |  |S )z;Allows application of the getter along with init arguments.)r:   r"   rD   r    r    r#   __call__   s    zProperty.__call__c                 C   s    |j r|j | _|j | _ || _| S )z8Set the getter function to fget. For use as a decorator.)r4   r3   rD   rK   r    r    r#   r:      s
    zProperty.getterc                 C   s   || _ | js| jj| _| S )z8Set the setter function to fset. For use as a decorator.)r;   r)   rD   r%   )r"   r;   r    r    r#   rA      s    
zProperty.setterc                 C   s   || j kr| j | S t|trBt|tjtjtjtjtj	frB|j
S |tttttttttttttttttttttt fkrz|S t!d|f d S )NzUnsupported type: %r)"_type_from_pytype_lookupr0   r,   
issubclassr   ZGObjectZGEnumZGFlagsZGBoxedZ
GInterface	__gtype__r   r   r   r   r
   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   )r"   Ztype_r    r    r#   r+      sB    



             zProperty._type_from_pythonc                 C   s   |d k	r|S | j | jd S rC   )_default_lookupgetr,   )r"   r.   r    r    r#   r-     s    zProperty._get_defaultc                 C   sz  | j }| j}|tkr.|dkr.td|f nH|tkrJ|d k	rFtdn,|tkrf|d k	rbtdn|tr|d krtdn t	||std||f n|t
rt	||std||f n|tr2|d k	r2t|tstdt| |D ](}t |ttfkrtd	t| qnD|trv|d k	rvt|d
rft	|tsvtd||f d S )N)TFz%default must be True or False, not %rz)object types does not have default valuesz(GType types does not have default valuesz%enum properties needs a default valuez'enum value %s must be an instance of %rz(flags value %s must be an instance of %rzStrv value %s must be a listz'Strv value %s must contain only stringsrO   z*variant value %s must be an instance of %r)r,   r.   r	   r1   r   r   is_ar   r   ZGTyper   r   r0   listreprstrbytesr   hasattr)r"   ptyper.   valr    r    r#   r/     sF    


zProperty._check_defaultc                 C   s   | j | jd S rC   )_min_value_lookuprQ   r,   r!   r    r    r#   r<   6  s    zProperty._get_minimumc                 C   s   | j | jd S rC   )_max_value_lookuprQ   r,   r!   r    r    r#   r>   9  s    zProperty._get_maximumc                 C   s   t |d| j | d S NZ_property_helper_)setattrr)   r"   rE   rG   r    r    r#   r9   @  s    zProperty._default_setterc                 C   s   t |d| j | jS r\   )getattrr)   r.   r"   rE   r    r    r#   r8   C  s    zProperty._default_getterc                 C   s   t d| jt|jf | _d S )Nz%s property of %s is read-onlyr1   r)   r,   r%   r@   r^   r    r    r#   r6   F  s     zProperty._readonly_setterc                 C   s   t d| jt|jf | _d S )Nz%s property of %s is write-onlyra   r`   r    r    r#   r7   J  s     zProperty._writeonly_getterc              	   C   s   | j }|ttttttttfkr0| j	| j
| jf}nl|tks^|tks^|ts^|ts^|trh| jf}n4|ttfkrzd}n"|ts|trd}nt|| j | j| jf| | jf S )Nr    )r,   r
   r   r   r   r   r   r   r   r=   r?   r.   r   r	   rR   r   r   r   r   r   r   r   NotImplementedErrorr2   r3   r5   )r"   rX   argsr    r    r#   get_pspec_argsR  s,    
   
zProperty.get_pspec_args)6r%   r&   r'   r4   r   r   intr
   boolr	   floatr   rU   r   r*   r   rM   r   r   r   r   
G_MAXFLOATG_MAXDOUBLEG_MININT	G_MINLONGr   rZ   	G_MAXUINT
G_MAXULONGG_MAXINT	G_MAXLONGr[   rP   r,   r(   r   ZPARAM_READWRITErB   r$   rI   rJ   rL   r:   rA   r+   r-   r/   r<   r>   r9   r8   r6   r7   rd   r    r    r    r#   r   (   s   8                                  
8	
!r   c                    s    j di }g } j  D ]b\}}t|tr|js:||_|j|krf||j | krXqtd|j | ||j< || q|sdS | _	d j ksd j kr|D ],}|j
|jks|j|jkrtd jf qdd }| _ fd	d
}| _dS )z
    Scans the given class for instances of Property and merges them
    into the classes __gproperties__ dict if it exists or adds it if not.
    __gproperties__z0Property %s was already found in __gproperties__Ndo_get_propertydo_set_propertyzGObject subclass %r defines do_get/set_property and it also uses a property with a custom setter or getter. This is not allowedc                 S   s   |j dd}t| |d S N-_)r)   replacer_   )r"   pspecr)   r    r    r#   obj_get_property  s    z,install_properties.<locals>.obj_get_propertyc                    s.   |j dd}t |d }|r*|| | d S rs   )r)   rv   r_   r;   )r"   rw   rG   r)   propclsr    r#   obj_set_property  s    z,install_properties.<locals>.obj_set_property)__dict__rQ   itemsr0   r   r)   rd   
ValueErrorappendrp   rD   r8   r;   r9   r1   r%   rq   rr   )r{   ZgpropertiesZpropsr)   ry   rx   r|   r    rz   r#   install_propertiese  s6    

r   N))r   r   Z_compatr   r   Z
_constantsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   ri   rj   rn   rl   rk   ro   rm   r*   r   r   r    r    r    r#   <module>   s   h  ?