3
Zd&                 @   s   d dl Z d dlZd dlT d dlZG dd deZG dd dZG dd deZed	krd d
lm	Z	 e
d G dd dZedZe	j Zej  ej  edZej  ej  dS )    N)*c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
SemanticOperationsc             C   s
   g | _ d S )N)_operation_stack)self r   -/usr/share/system-config-printer/asyncconn.py__init__   s    zSemanticOperations.__init__c             C   s   | j j| d S )N)r   append)r   Z	operationr   r   r   _begin_operation"   s    z#SemanticOperations._begin_operationc             C   s   | j j  d S )N)r   pop)r   r   r   r   _end_operation%   s    z!SemanticOperations._end_operationc             C   s$   y
| j d S  tk
r   d S X d S )Nr   )r   
IndexError)r   r   r   r   current_operation(   s    
z$SemanticOperations.current_operationN)__name__
__module____qualname__r   r
   r   r   r   r   r   r   r      s   r   c               @   sD   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S )_AsyncMethodCallc             C   s.   || _ || _|| _|| _d| _td|   d S )NFz+%s)_fn_reply_handler_error_handler_auth_handler
_destroyed
debugprint)r   fnreply_handlererror_handlerauth_handlerr   r   r   r   3   s    z_AsyncMethodCall.__init__c             C   s   t d|   d S )Nz-%s)r   )r   r   r   r   __del__;   s    z_AsyncMethodCall.__del__c             C   sD   | j r
d S td|   d| _ d | _d | _d | _d | _d | _d | _d S )NzDESTROY: %sT)r   r   r   r   r   _reply_data_error_data
_auth_data)r   r   r   r   destroy>   s    z_AsyncMethodCall.destroyc             O   sd   |j d| _|j d| _|j d| _| j|d< | j|d< | j|d< td| | jf  | j|| d S )Nr   r   r   z%s: calling %s)	getr   r   r    r   r   r   r   r   )r   argskwdsr   r   r   runK   s    


z_AsyncMethodCall.runc             G   s0   | j s,td| | jf  | j| | jf|  d S )Nz%s: to reply_handler at %s)r   r   r   r   )r   r#   r   r   r   r   U   s    z_AsyncMethodCall.reply_handlerc             G   s0   | j s,td| | jf  | j| | jf|  d S )Nz%s: to error_handler at %s)r   r   r   r   )r   r#   r   r   r   r   [   s    z_AsyncMethodCall.error_handlerc             G   s0   | j s,td| | jf  | j| | jf|  d S )Nz%s: to auth_handler at %s)r   r   r   Z	auth_data)r   r#   r   r   r   r   a   s    z_AsyncMethodCall.auth_handlerN)
r   r   r   r   r   r!   r%   r   r   r   r   r   r   r   r   2   s   
r   c            	       s^   e Zd Z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  ZS )
ConnectionNTc
                s  t tj  d_|d kr$tj }|jds6|dko@tj dk}
fdd}fdd} fd	d
}|
r|rt	d dd l
}|j||||||d}|_n4t	d dd l}|j|||||||||	d
}|_tjj}tjj}g }xptjD ]b}|jdrqtj|}t||kr:t||kr:qt|st|j| |j| qW |_g _t	d  d S )NF/Z	localhostr   c                s   j d  | d S )N)_subst_reply_handler)connZreply)r   r   r   r   subst_reply_handlerz   s    z0Connection.__init__.<locals>.subst_reply_handlerc                s   j d  | d S )N)_subst_error_handler)r)   exc)r   r   r   r   subst_error_handler}   s    z0Connection.__init__.<locals>.subst_error_handlerc                s   j d  | || d S )N)_subst_auth_handler)promptr)   methodresource)r   r   r   r   subst_auth_handler   s    z/Connection.__init__.<locals>.subst_auth_handlerzUsing polkit-1 connection class)r   r   hostport
encryptionparentzUsing IPP connection class)
r   r   r   r3   r4   r5   r6   try_as_rootprompt_allowedZsemantic_z+%s)superr&   r   r   cupsZ	getServer
startswithosgetuidr   asyncpk1ZPK1Connection_connasyncippZIPPAuthConnectiontypeZgetPrinters
getDevicesdirgetattrhasattrsetattr_make_bindingr	   	_bindings_methodcalls)r   r   r   r   r3   r4   r5   r6   r7   r8   Zuse_pkr*   r-   r2   r?   crA   Z
methodtypeZinstancemethodtypeZbindingsfnamer   )	__class__)r   r   r   r   r   r   m   sZ    
zConnection.__init__c             C   s   t jd|   d S )Nz-%s)debugr   )r   r   r   r   r      s    zConnection.__del__c             C   sl   t d|   d| _y| jj  W n tk
r4   Y nX x| jD ]}|j  q>W x| jD ]}t| | qVW d S )NzDESTROY: %sT)r   r   r@   r!   AttributeErrorrJ   rI   delattr)r   
methodcallZbindingr   r   r   r!      s    zConnection.destroyc                s    fddS )Nc                 s   j  f| |S )N)_call_function)r#   r$   )r   r   r   r   <lambda>   s    z*Connection._make_binding.<locals>.<lambda>r   )r   r   r   )r   r   r   rH      s    zConnection._make_bindingc             O   s0   t || j| j| j}| jj| |j|| d S )N)r   r(   r+   r.   rJ   r	   r%   )r   r   r#   r$   rQ   r   r   r   rR      s    zConnection._call_functionc             G   sZ   |r,|j   | jj|}| j|= |dd  }|rV| j rVtd| |f  || f|  d S )N   z%s: chaining up to %s)r!   rJ   indexr   r   )r   rQ   r   r#   ir   r   r   r(      s    zConnection._subst_reply_handlerc             G   sZ   |r,|j   | jj|}| j|= |dd  }|rV| j rVtd| |f  || f|  d S )NrT   z%s: chaining up to %s)r!   rJ   rU   r   r   )r   rQ   r   r#   rV   r   r   r   r+      s    zConnection._subst_error_handlerc             C   sN   |r |j   | jj|}| j|= |rJ| j rJtd| |f  ||| || d S )Nz%s: chaining up to %s)r!   rJ   rU   r   r   )r   rQ   r   r/   r0   r1   rV   r   r   r   r.      s    zConnection._subst_auth_handlerc             C   s   | j j| dS )z*Call this from your auth_handler function.N)Zthreadset_auth_info)r   Zpasswordr   r   r   rW      s    zConnection.set_auth_info)	NNNNNNNTT)r   r   r   r   r   r!   rH   rR   r(   r+   r.   rW   __classcell__r   r   )rM   r   r&   l   s     <

	r&   __main__)GObjectTc               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )Testc             C   s   t  | _|| _td|   d S )Nz+%s)r&   r@   _quitr   )r   quitr   r   r   r      s    zTest.__init__c             C   s   t jd|   d S )Nz-%s)rN   r   )r   r   r   r   r      s    zTest.__del__c             C   s(   t d|   | jj  | jr$tj  d S )NzDESTROY: %s)r   r@   r!   r\   loopr]   )r   r   r   r   r!      s    
zTest.destroyc             C   s   | j j| j| jd d S )N)r   r   )r@   rC   getDevices_replygetDevices_error)r   r   r   r   rC      s    
zTest.getDevicesc             C   s   t || | j  d S )N)printr!   )r   r)   resultr   r   r   r_      s    
zTest.getDevices_replyc             C   s   t t| | j  d S )N)ra   reprr!   )r   r)   r,   r   r   r   r`     s    zTest.getDevices_errorN)	r   r   r   r   r   r!   rC   r_   r`   r   r   r   r   r[      s   r[   F)r;   r=   rN   objectr   r   r&   r   Zgi.repositoryrZ   Zset_debuggingr[   tZMainLoopr^   rC   r!   r%   r   r   r   r   <module>   s"   :|