o
    `                     @   s   d Z ddlZddlmZ ddlm  mZ ddlm	Z	 ddl
mZmZ ddlmZ G dd dZd	ZG d
d dZdZh dZdd ZdddZdd ZedkrUe  dS dS )z5Generate a wrapper class from DBus introspection data    N)indent)Introspectable)open_dbus_connectionProxy)__version__c                   @   s$   e Zd Zdd Zdd Zdd ZdS )Methodc              	   C   s|   |j d | _g | _g | _|dD ]*}z|j d }W n ty+   dt| j}Y nw | j| | j|j d  qd S )Nnamezarg[@direction='in']zarg{}type)	attribr   in_args	signaturefindallKeyErrorformatlenappend)selfxml_nodeargr    r   1/usr/lib/python3/dist-packages/jeepney/bindgen.py__init__   s   zMethod.__init__c                 C   s   dj | jdS )Nz=def {name}(self):
    return new_method_call(self, '{name}')
)r   )r   r   )r   r   r   r   _make_code_noargs   s   zMethod._make_code_noargsc                 C   sV   | j s|  S d| j }d| j}t| j dkrdnd|}dj| j|||dS )Nz,     z({},)z({})zxdef {name}(self, {args}):
    return new_method_call(self, '{name}', '{signature}',
                           {tuple})
)r   argsr   tuple)r   r   joinr   r   r   r   )r   r   r   r   r   r   r   	make_code   s   
zMethod.make_codeN)__name__
__module____qualname__r   r   r   r   r   r   r   r   
   s    r   z
class {cls_name}(MessageGenerator):
    interface = {interface!r}

    def __init__(self, object_path={path!r},
                 bus_name={bus_name!r}):
        super().__init__(object_path=object_path, bus_name=bus_name)
c                   @   s   e Zd Zdd Zdd ZdS )	Interfacec                 C   s2   |j d | _|| _|| _dd |dD | _d S )Nr   c                 S   s   g | ]}t |qS r   )r   ).0noder   r   r   
<listcomp>7   s    z&Interface.__init__.<locals>.<listcomp>method)r
   r   pathbus_namer   methods)r   r   r'   r(   r   r   r   r   3   s   zInterface.__init__c                 C   sT   | j dd }tj|| j | j| jdg}| jD ]}|t|	 d qd
|S )N.)cls_name	interfacer'   r(   z    
)r   splitINTERFACE_CLASS_TEMPLATEr   r'   r(   r)   r   r   r   r   )r   r,   chunksr&   r   r   r   r   9   s   

zInterface.make_codeN)r   r    r!   r   r   r   r   r   r   r"   2   s    r"   z"""Auto-generated DBus bindings

Generated by jeepney version {version}

Object path: {path}
Bus name   : {bus_name}
"""

from jeepney.wrappers import MessageGenerator, new_method_call

>   org.freedesktop.DBus.Peerorg.freedesktop.DBus.Properties#org.freedesktop.DBus.Introspectablec                 C   s   t |ttfr#t|d}t| |||W  d    S 1 sw   Y  t| }|tj	t
||d d}|dD ]}|jd tv rDq:|t|||  |d7 }q:|S )Nw)versionr'   r(   r   r-   r   r   )
isinstancebytesstropencode_from_xmlET
fromstringwriteMODULE_TEMPLATEr   r   r   r
   IGNORE_INTERFACESr"   r   )xmlr'   r(   fhfrootiinterface_noder   r   r   r;   U   s    

r;   SESSIONc                 C   sD   t |}tt| ||}| \}t|| ||}td|| d S )Nz#Written {} interface wrappers to {})r   r   r   
Introspectr;   printr   )r'   r   output_filebusconnintrospectablerA   n_interfacesr   r   r   generateg   s
   
rO   c                  C   s   t  } | jdddd | jdddd | jddd	 | d
d |  }|jp4|jdd  ddd }t|j|j||j	 d S )Nz-nz--nameT)requiredz-pz--pathz--busrG   )defaultz-oz--outputr   /_z.py)
argparseArgumentParseradd_argument
parse_argsoutputr'   replacerO   r   rK   )apr   rX   r   r   r   mainp   s    r[   __main__)rG   )__doc__rT   textwrapr   xml.etree.ElementTreeetreeElementTreer<   jeepney.wrappersr   jeepney.io.blockingr   r   jeepneyr   r   r0   r"   r?   r@   r;   rO   r[   r   r   r   r   r   <module>   s$    	
	
