3
Z#              
   @   s   d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dlm	Z	m
Z
 ddlT yd dlmZ W n   dZY nX G d	d
 d
eZdS )    )GtkN)NamedTemporaryFile)TimedOperationOperationCanceled   )*)journalFc               @   sT   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S )ErrorLogCheckpointc             C   s   t j| |d | jtdtd}tjjtd}tj }|jd |j	tj
j |j|ddd || _|j|ddd tj | _| jjdd | jjd |j| jddd |j||  i | _d S )NzError log checkpointZ	DebuggingzThis step will enable debugging output from the CUPS scheduler.  This may cause the scheduler to restart.  Click the button below to enable debugging.zEnable Debuggingr   FT)Question__init__Zinitial_vbox_r   ZButtonZnew_with_labelZ
HButtonBoxZset_border_widthZ
set_layoutZButtonBoxStyleZSTARTZ
pack_startbuttonZLabellabelZset_alignmentZset_line_wrapZnew_pagepersistent_answers)selftroubleshooterZpager   Z	buttonbox r   C/usr/share/system-config-printer/troubleshoot/ErrorLogCheckpoint.pyr   &   s     



zErrorLogCheckpoint.__init__c             C   s   | j jddsd S | jjd }|j }|jd |j }tt|j	 dkrPd S d|t
j< | jj}| j d }|jdd|d< |j| d S )	Nerror_log_debug_logging_setF!_authenticated_connection_factoryr   0cups_server_settings
MaxLogSizeZ2000000)r   getr   answersget_connectionZ	_set_lockadminGetServerSettingslenlistkeyscupsCUPS_SERVER_DEBUG_LOGGINGadminSetServerSettings)r   fcsettingsr   Zorig_settingsr   r   r   __del__;   s    


zErrorLogCheckpoint.__del__c             C   s   i | _ | jj }|d sdS |d | _| jj }dd }yt||d| _| jj }W n, tk
rf   dS  tj	k
r~   i }Y nX d| _
| jjd tt|j dkrd	S || jd
< yt|tj dkrdS W n& tk
r   Y n tk
r   Y nX d	S )Ncups_queue_listedF_authenticated_connectionc              S   s(   t jdd  t jd t j } | j S )Nc             S   s   dS )N r   )xr   r   r   <lambda>X   s    zGErrorLogCheckpoint.display.<locals>.getServerSettings.<locals>.<lambda>r)   )r    ZsetPasswordCBZ	setServerZ
Connectionr   )r$   r   r   r   getServerSettingsV   s    
z5ErrorLogCheckpoint.display.<locals>.getServerSettings)parentr)   r   Tr   )r   r   authconn
get_windowr   oprunRuntimeErrorr    IPPErrorforward_allowedr   set_textr   r   r   r   intr!   KeyError
ValueError)r   r   r-   r,   r%   r   r   r   displayM   s6    



zErrorLogCheckpoint.displayc             C   s   | j jd| j|| _d S )NZclicked)r   Zconnectenable_clickedbutton_sigid)r   handlerr   r   r   connect_signalsx   s    z"ErrorLogCheckpoint.connect_signalsc             C   s   | j j| j d S )N)r   Z
disconnectr;   )r   r   r   r   disconnect_signals|   s    z%ErrorLogCheckpoint.disconnect_signalsc          "   C   s  | j j}|d si S | j j }| jj| j d| jkr<| jS t }y(t| jjd|j	f|d| _
| j
j  W n ttjfk
r } z|| jd< W Y d d }~X nT tjk
r } z6|| jd< | j j}|d }|j | _| j| jd< W Y d d }~X nX ytj|j	}W n( tk
r*   dddddddg}Y nX W d Q R X |d	 | jd< |d	 | jd< trtj }|j  |j d
 }|| jd< || jd< tjjtj }	|	jd}
|
| jd< |
| jd< | jS )Nr'   Zerror_log_checkpointz/admin/log/error_log)argsr-   Zerror_log_checkpoint_excr   r(   r      Z__CURSORZerror_log_cursorz%F %TZerror_log_timestamp)r   r   r/   updater   r   r   r.   ZgetFilefiler0   r1   r2   r    r3   Z	HTTPErrorr   osstatOSErrorr   ReaderZ	seek_tailZget_previousdatetimeZfromtimestamptimeZstrftime)r   r   r-   ZtmpfefactoryZstatbufjZcursorZnowZ	timestampr   r   r   collect_answer   sN    




"




z!ErrorLogCheckpoint.collect_answerc             C   s   | j S )N)r4   )r   r   r   r   can_click_forward   s    z$ErrorLogCheckpoint.can_click_forwardc       
   #   C   s  | j j }| j j  y t| jj|d| _| jj }W n2 tj	t
fk
rf   | j j  d| _|| d S X |j | jd< d}yt|tj }W n tk
r   d}Y nX yt|| }W n ttfk
r   d}Y nX |dks|dkrd|tj< d||< d	}d
d }	y8tdt|  t|	| j|f|d| _| jj  d}W n, tj	k
rT   Y n tk
rh   Y nX |rd| jd< | jjtd n| jjtd d| _| j j  || d S )N)r-   Tr   r   r   r   r   1Fc             S   sV   | j | d}xB|dkrPytjd | j  P W q tk
rL   |d7 }Y qX qW d S )Nr      )r"   rH   ZsleepZ_connectr2   )Z
connectionr%   Zattemptr   r   r   set_settings   s    


z7ErrorLogCheckpoint.enable_clicked.<locals>.set_settingszSettings to set: )r?   r-   r   zDebug logging enabled.z"Debug logging was already enabled.)r   r/   Zbusyr   r.   r   r0   r1   r    r3   r   Zreadyr4   copyr   r6   r!   r7   r8   Z
debugprintreprr2   r   r5   r   )
r   r   r<   r-   r%   Z
MAXLOGSIZEZ
prev_debugZprev_logsizeZsuccessrP   r   r   r   r:      sX    










z!ErrorLogCheckpoint.enable_clickedc             C   s4   | j j  | jj}|d }|j | _| j| jd< d S )Nr   r(   )r0   Zcancelr   r   r   r.   )r   r   rJ   r   r   r   cancel_operation   s
    

z#ErrorLogCheckpoint.cancel_operationN)__name__
__module____qualname__r   r&   r9   r=   r>   rL   rM   r:   rT   r   r   r   r   r	   %   s   +1Ar	   )Zgi.repositoryr   r    rC   Ztempfiler   rG   rH   Ztimedopsr   r   baseZsystemdr   r
   r	   r   r   r   r   <module>   s   
