B
    0ªaÞS  ã               @   s  d dl mZ d dl mZ d dl mZ d dl mZ 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 ddlmZ ddlmZ dZdZdZdZdZdZdZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ dS )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Úparseé   )Úast)Úmessage)Úutils)ÚTransformerException)Úto_underscoresé   é   é   é   é    é@   é€   c               @   s   e Zd Zddd„ZdS )ÚIntrospectionBinaryNc             C   s(   || _ |d krt dd¡| _n|| _d S )NÚ ztmp-introspect)ÚargsÚtempfileZmkdtempÚtmpdir)Úselfr   r   © r   ú7/usr/lib/gobject-introspection/giscanner/gdumpparser.pyÚ__init__4   s    zIntrospectionBinary.__init__)N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   2   s   r   c               @   s   e Zd Zdd„ ZdS )Ú
Unresolvedc             C   s
   || _ d S )N)Útarget)r   r!   r   r   r   r   >   s    zUnresolved.__init__N)r   r   r   r   r   r   r   r   r    <   s   r    c               @   s   e Zd ZdS )ÚUnknownTypeErrorN)r   r   r   r   r   r   r   r"   B   s   r"   c               @   sä   e Z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„ 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,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7S )8ÚGDumpParserc             C   s6   || _ |j| _d | _g | _g | _i | _i | _i | _d S )N)	Ú_transformerÚ	namespaceÚ
_namespaceÚ_binaryÚ_get_type_functionsÚ_error_quark_functionsZ_error_domainsÚ_boxed_typesÚ_private_internal_types)r   Útransformerr   r   r   r   H   s    zGDumpParser.__init__c             C   sp   x(| j  ¡ D ]}t|tjƒr|  |¡ qW | j jdksB| j jdkrlx(| j  ¡ D ]}t|tjƒrN|  |¡ qNW dS )zŽDo parsing steps that don't involve the introspection binary

        This does enough work that get_type_functions() can be called.

        ZGObjectZGLibN)	r&   ÚvaluesÚ
isinstancer   ZFunctionÚ_initparse_functionÚnameÚRecordÚ_initparse_gobject_record)r   Únoder   r   r   Ú
init_parseT   s    zGDumpParser.init_parsec             C   s   | j S )N)r(   )r   r   r   r   Úget_get_type_functionse   s    z"GDumpParser.get_get_type_functionsc             C   s   | j S )N)r)   )r   r   r   r   Úget_error_quark_functionsh   s    z%GDumpParser.get_error_quark_functionsc             C   s
   || _ d S )N)r'   )r   Zbinaryr   r   r   Úset_introspection_binaryk   s    z$GDumpParser.set_introspection_binaryc             C   s  |   ¡ }| ¡ }x,|D ]$}|jdkr0|  |¡ q|  |¡ qW x | j ¡ D ]\}}|  |¡ qJW x.| j 	¡ D ] }t
|tjtjfƒrl|  |¡ qlW g }xb| j ¡ D ]T\}}t
|tjƒr |jdk	r |j}|dkrÎq | j |¡\}	}| j |¡}
| |
¡ q W x|D ]}| j |¡ qþW dS )z9Do remaining parsing steps requiring introspection binaryzerror-quarkNÚintern)Ú_execute_binary_get_treeZgetrootÚtagÚ_introspect_error_quarkÚ_introspect_typer*   ÚitemsÚ_pair_boxed_typer&   r-   r.   r   ÚClassÚ	InterfaceÚ_find_class_recordZ
RegisteredÚget_typer$   Úsplit_csymbolÚgetÚappendÚremove)r   ZtreeÚrootÚchildr0   Úboxedr3   Z	to_removeZget_type_nameÚnsZget_type_funcr   r   r   r   n   s,    


zGDumpParser.parsec          
   C   sJ  t j | jjd¡}t|dƒb}x,| jD ]"}| d¡ | |¡ | d¡ q&W x,| jD ]"}| d¡ | |¡ | d¡ qTW W dQ R X t j | jjd¡}g }t j	 
d¡}|r¸| | ¡ ¡ | | jj¡ | d	||f ¡ zPytj|tjtjd
 W n. tjk
r  } zt|ƒ‚W dd}~X Y nX t|ƒS t d¡sDt | jj¡ X dS )zwLoad the library (or executable), returning an XML
blob containing data gleaned from GObject's primitive introspection.zfunctions.txtÚwz	get-type:Ú
zerror-quark:Nzdump.xmlZGI_CROSS_LAUNCHERz--introspect-dump=%s,%s)ÚstdoutÚstderrz
save-temps)ÚosÚpathÚjoinr'   r   Úopenr(   Úwriter)   ÚenvironrD   ÚextendÚsplitr   rE   Ú
subprocessZ
check_callÚsysrM   rN   ZCalledProcessErrorÚ
SystemExitr   r
   Zhave_debug_flagÚshutilZrmtree)r   Zin_pathÚfÚfuncZout_pathr   ZlauncherÚer   r   r   r9   •   s2    



z$GDumpParser._execute_binary_get_treec             C   sL   |j }| d¡rd S | d¡s(| d¡r4|  |¡ n| d¡rH|  |¡ d S )NÚ_Ú	_get_typeÚ
_get_gtypeZ_error_quark)ÚsymbolÚ
startswithÚendswithÚ_initparse_get_type_functionÚ_initparse_error_quark_function)r   r\   ra   r   r   r   r/   ¼   s    

zGDumpParser._initparse_functionc             C   s,   |j dkrdS | ¡ r(| j |j ¡ dS dS )NZg_variant_get_gtypeTF)ra   Zis_type_meta_functionr(   rE   )r   r\   r   r   r   rd   Å   s    
z(GDumpParser._initparse_get_type_functionc             C   s$   |j jjdkrdS | j |j¡ dS )NZGQuarkFT)ÚretvalÚtypeÚctyper)   rE   ra   )r   r\   r   r   r   re   Ð   s    z+GDumpParser._initparse_error_quark_functionc             C   sÜ   |j  d¡r”|j dkr”d }|j dkr0tjdd}t|j ƒ ¡ }tj|j ||j|j dd¡d|d}d	|_	|j dkrxd	|_
|  |¡ | jj|d	d
 nD|j dkr¸tjddddd| jd< n |j dkrØ| j d¡j|_d|_d S )NZ	ParamSpec)ZParamSpecPoolZParamSpecClassZParamSpecTypeInfozGObject.ParamSpec)Ztarget_ginameZSpecr   r8   )rh   Ú
gtype_namerB   Úc_symbol_prefixT)ÚreplaceZVariantZGVariantZvariant)ri   rB   rj   ZInitiallyUnownedClassZObjectClassF)r0   rb   r   ÚTyper   Úlowerr?   rh   rk   ÚfundamentalÚis_abstractÚ_add_record_fieldsr&   rE   ÚBoxedr*   rD   ÚfieldsÚ	disguised)r   ÚrecordÚparentÚprefixr3   r   r   r   r2   Ö   s0    






z%GDumpParser._initparse_gobject_recordc             C   s~   |j dkr|  |¡ nd|j dkr,|  |¡ nN|j dkrB|  |¡ n8|j dkrX|  |¡ n"|j dkrn|  |¡ ntd|j ƒ‚d S )N)ÚenumÚflagsÚclassÚ	interfacerI   rn   z"Unhandled introspection XML tag %s)r:   Ú_introspect_enumÚ_introspect_objectÚ_introspect_interfaceÚ_introspect_boxedÚ_introspect_fundamentalÚ
ValueError)r   Úxmlnoder   r   r   r<   ô   s    




zGDumpParser._introspect_typec          
   C   sD  |j d }|  |¡\}}y| j |¡}W n, tk
rT } zt |¡ W d d }~X Y nX i }| j |¡}t	|t
jt
jfƒr”x|jD ]}	|	j||	j< q€W g }
xb| d¡D ]T}	|	j d  dd¡}||krÌ|| }n
|	j d }|
 t
 |||	j d |	j d ¡¡ q¤W |jdkrt
j}nt
j}||||||
|j d d	}| jj|d
d d S )Nr0   ÚmemberZnickú-r^   Úvaluerx   zget-type)ri   rj   ÚmembersrB   T)rk   )ÚattribÚ_split_type_and_symbol_prefixr$   Ústrip_identifierr   r	   Úfatalr&   rD   r.   r   ÚEnumZBitfieldr…   r„   r0   Úfindallrk   rE   ZMemberr:   )r   r   Ú	type_namerB   rj   Z	enum_namer]   Zprevious_valuesZpreviousr‚   r…   r0   r„   Úklassr3   r   r   r   r{     s<    



zGDumpParser._introspect_enumc             C   sh   |j d }| j |¡\}}|dkr>t d|j d | jjf ¡ | d¡rNd}nd}||dt|ƒ … fS )z6Infer the C symbol prefix from the _get_type function.zget-type)rB   r`   z´The GObject name '%s' isn't compatible
with the configured identifier prefixes:
  %r
The class would have no name.  Most likely you want to specify a
different --identifier-prefix.r0   r_   r`   N)	r†   r$   rC   r	   r‰   r&   Zidentifier_prefixesrc   Úlen)r   r   rB   rJ   r0   Ztype_suffixr   r   r   r‡   0  s    

z)GDumpParser._split_type_and_symbol_prefixc       	   
   C   sÌ   |j d }t|j  dd¡ƒ}|  |¡\}}y| j |¡}W n, tk
rf } zt |¡ W d d }~X Y nX t	j
|d ||||d}|  ||¡ |  ||¡ |  ||¡ |  ||¡ |  |¡ | jj|dd d S )Nr0   ÚabstractF)ri   rB   rj   ro   T)rk   )r†   ÚboolrD   r‡   r$   rˆ   r   r	   r‰   r   r?   Ú_parse_parentsÚ_introspect_propertiesÚ_introspect_signalsÚ"_introspect_implemented_interfacesrp   r&   rE   )	r   r   rŒ   ro   rB   rj   Zobject_namer]   r3   r   r   r   r|   A  s$    

zGDumpParser._introspect_objectc          
   C   s"  |j d }|  |¡\}}y| j |¡}W n, tk
rT } zt |¡ W d d }~X Y nX tj|d |||d}|  	||¡ |  
||¡ x4| d¡D ]&}|j d }	tj |	¡}
|j |
¡ qŽW | j |j¡}t|tjƒrÜ|j|_nt |d|jf ¡ |j d  d¡r|| j|< n| jj|dd d S )	Nr0   )ri   rB   rj   Zprerequisitez+Couldn't find associated structure for '%s'zget-typer^   T)rk   )r†   r‡   r$   rˆ   r   r	   r‰   r   r@   r’   r“   r‹   rl   Úcreate_from_gtype_nameZprerequisitesrE   r&   rD   r0   r.   r1   rh   Z	warn_noderb   r+   )r   r   rŒ   rB   rj   Zinterface_namer]   r3   rH   r0   Zprereqrt   r   r   r   r}   U  s.    


z!GDumpParser._introspect_interfacec             C   s"   t jddddd}|| j|j< d S )NZParamSpecMiniObjectÚGParamSpecMiniObjectZ#gst_param_spec_mini_object_get_typeZparam_spec_mini_object)ri   rB   rj   )r   rq   r*   ri   )r   r   r3   r   r   r   Ú&_introspect_boxed_gstreamer_workaroundv  s    z2GDumpParser._introspect_boxed_gstreamer_workaroundc          
   C   sŽ   |j d }|dkr |  |¡ d S y| j |¡}W n, tk
r\ } zt |¡ W d d }~X Y nX |  |¡\}}tj	||||d}|| j
|j< d S )Nr0   r–   )ri   rB   rj   )r†   r—   r$   rˆ   r   r	   r‰   r‡   r   rq   r*   ri   )r   r   rŒ   r0   r]   rB   rj   r3   r   r   r   r~   |  s    

zGDumpParser._introspect_boxedc             C   s>   g }x.|  d¡D ] }tj |jd ¡}| |¡ qW ||_d S )NZ
implementsr0   )r‹   r   rl   r•   r†   rE   Z
interfaces)r   r3   r   Zgt_interfacesrz   Zgityper   r   r   r”   ‘  s
    z.GDumpParser._introspect_implemented_interfacesc       
      C   s’   x„|  d¡D ]v}|jd }t|jd ƒ}|t@ dk}|t@ dk}|t@ dk}|t@ dk}	|j t	 
|jd t	j |¡||||	¡¡ qW |j|_d S )NÚpropertyrg   rx   r   r0   )r‹   r†   ÚintÚG_PARAM_READABLEÚG_PARAM_WRITABLEÚG_PARAM_CONSTRUCTÚG_PARAM_CONSTRUCT_ONLYZ
propertiesrE   r   ZPropertyrl   r•   )
r   r3   r   Zpspecrh   rx   ÚreadableÚwritableZ	constructZconstruct_onlyr   r   r   r’   ˜  s    


z"GDumpParser._introspect_propertiesc             C   s4  x$|  d¡D ]}|jd }tj |¡}t |¡}g }|j d¡}|j dd¡dk}	|j dd¡dk}
|j dd¡dk}|j d	d¡dk}xjt|  d
¡ƒD ]X\}}|dkr²d}nd|d f }|jd }tj |¡}t ||¡}tj	|_
| |¡ qœW tj|jd ||||	|
||d}|j |¡ qW |j|_d S )NÚsignalÚreturnÚwhenz
no-recurseÚ0Ú1ÚdetailedÚactionzno-hooksÚparamr   Úobjectzp%sr   rg   r0   )r¢   Ú
no_recurser¥   r¦   Úno_hooks)r‹   r†   r   rl   r•   ZReturnrD   Ú	enumerateZ	ParameterZPARAM_TRANSFER_NONEZtransferrE   ZSignalZsignals)r   r3   r   Zsignal_infoZrctypeZrtypeZreturn_Ú
parametersr¢   r©   r¥   r¦   rª   ÚiZ	parameterÚargnameZpctypeZptyper§   r    r   r   r   r“   ¦  s0    



zGDumpParser._introspect_signalsc             C   s>   |j  dd¡}|dkr0ttdd„ | d¡ƒƒ}ng }||_d S )NÚparentsr   c             S   s   t j | ¡S )N)r   rl   r•   )Úsr   r   r   Ú<lambda>Ä  ó    z,GDumpParser._parse_parents.<locals>.<lambda>ú,)r†   rD   ÚlistÚmaprV   Zparent_chain)r   r   r3   Zparents_strZparent_typesr   r   r   r‘   Á  s    
zGDumpParser._parse_parentsc       	   
   C   sº   |j d }t|j  dd¡ƒ}|  |¡\}}y| j |¡}W n, tk
rf } zt |¡ d S d }~X Y nX t	j
|d ||||d}|  ||¡ d|_|  ||¡ |  |¡ | jj|dd d S )Nr0   r   F)ri   rB   rj   ro   T)rk   )r†   r   rD   r‡   r$   rˆ   r   r	   Úwarnr   r?   r‘   rn   r”   rp   r&   rE   )	r   r   rŒ   ro   rB   rj   Zfundamental_namer]   r3   r   r   r   r   Ê  s$    


z#GDumpParser._introspect_fundamentalc             C   sT   | j  |j¡}t|tjƒsd S |j|_|j|_x |jD ]}t|tjƒr6d|_	q6W d S )NF)
r&   rD   r0   r.   r   r1   rh   rr   ZFieldrŸ   )r   r3   rt   Zfieldr   r   r   rp   á  s    zGDumpParser._add_record_fieldsc             C   s^   |j d }|j d }| j |¡}|d kr,d S t |j|j|j|j|j	|¡}| jj
|dd d S )NÚfunctionZdomainT)rk   )r†   r&   Zget_by_symbolr   ZErrorQuarkFunctionr0   rf   r¬   Zthrowsra   rE   )r   r   ra   Zerror_domainr·   r3   r   r   r   r;   î  s    


z#GDumpParser._introspect_error_quarkc          
   C   s˜   y| j  |j¡}W n, tk
r> } zt |¡ W d d }~X Y nX | j |¡}|s^| j |¡ n6t	|t
jt
jfƒr| |j|j¡ |j|_d|_ndS d S )NF)r$   rˆ   ri   r   r	   r‰   r&   rD   rE   r.   r   r1   ZUnionZ	add_gtyperB   rj   rs   )r   rI   r0   r]   Z	pair_noder   r   r   r>   ú  s    zGDumpParser._pair_boxed_typec             C   s€   d }t |tjƒr$| j |jd ¡}n&x$dD ]}| j |j| ¡}|r*P q*W |rZt |tjƒs^d S | ¡ |_| 	|¡ | ¡ |_
d S )Nr?   )ZIfacer@   )r.   r   r?   r&   rD   r0   r1   Zcreate_typeZglib_type_structZinherit_file_positionsZis_gtype_struct_for)r   ÚclsZpair_recordÚsuffixr   r   r   rA     s    


zGDumpParser._find_class_recordN)r   r   r   r   r4   r5   r6   r7   r   r9   r/   rd   re   r2   r<   r{   r‡   r|   r}   r—   r~   r”   r’   r“   r‘   r   rp   r;   r>   rA   r   r   r   r   r#   F   s6   ''	.!	r#   )!Z
__future__r   r   r   r   rO   rX   r   rZ   rW   Zxml.etree.cElementTreer   r   r   r	   r
   r,   r   r   rš   r›   rœ   r   ZG_PARAM_LAX_VALIDATIONZG_PARAM_STATIC_NAMEZG_PARAM_STATIC_NICKZG_PARAM_STATIC_BLURBr¨   r   r    Ú	Exceptionr"   r#   r   r   r   r   Ú<module>   s4   
