U
    9_M                     @   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	 ddl
T zd dlmZ W n   dZY nX G d	d
 d
eZdS )    )GtkN)NamedTemporaryFile)TimedOperation   )*)journalFc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ErrorLogFetchc                 C   s`   t | |d | tdtd}t | _| jd || jddd |	||  i | _
d S )NzError log fetchzRetrieve Journal EntrieszNo system journal entries were found. This may be because you are not an administrator. To fetch journal entries please run this command:Fr   )Question__init__Zinitial_vbox_r   ZEntryentryZset_editableZ
pack_startZnew_pagepersistent_answers)selftroubleshooterZpage r   >/usr/share/system-config-printer/troubleshoot/ErrorLogFetch.pyr
   '   s    

zErrorLogFetch.__init__c                    s@  | j j}| j  }i | _|d}|d}|d}d| jksJd| jkrRd }d }dd }tjt d}| j jd	 | _	d
|kr8z t
| j	j|d| _| j }W n tjk
r   Y dS X d|tj< |d }	|	dd|d< d}
dd }z.t
|| j	|f|d| _| j  d| jd< W n tjk
r6   Y nX i | _tr|d k	rdd  t }|| |jdd  fdd|D | jd< |d k	rt
|| j	f|d| _| j }|d k	rt|}|| | }t| ddd |D i| _t| jdg t| jdg  dkr<d||f }| j| dS dS )NZerror_log_checkpointZerror_log_cursorZerror_log_timestampZ	error_logr   c                 S   s   |   }| d |   tdd~}d}z| jd|d d}W n< tjk
r|   zt|j	 W n t
k
rv   Y nX Y nX | | |r|jW  5 Q R  S W 5 Q R X d S )NF)deletez/admin/log/error_log)fileT)Z_get_prompt_allowedZ_set_prompt_allowed_connectr   ZgetFilecupsZ	HTTPErrorosremovenameOSErrorr   )cpromptZtmpfsuccessr   r   r   	fetch_logA   s"    

z(ErrorLogFetch.display.<locals>.fetch_logz%F %T_authenticated_connectionZerror_log_debug_logging_set)parentF0Zcups_server_settingsZ
MaxLogSizeZ2000000c                 S   sT   |  | d}|dkrPztd |   W qPW q tk
rL   |d7 }Y qX qd S )Nr      )ZadminSetServerSettingstimesleepr   RuntimeError)Z
connectionsettingsZattemptr   r   r   set_settingsd   s    

z+ErrorLogFetch.display.<locals>.set_settingsTZerror_log_debug_logging_unsetc              	   S   sP   zd| d  }W n t tfk
r,   d}Y nX |d | d d d | d  S )NZ	XACEWNIDdZPRIORITY Z__REALTIME_TIMESTAMPz[%m/%b/%Y:%T]ZMESSAGE)
IndexError	TypeErrorstrftime)xZpriorityr   r   r   journal_format~   s    
z-ErrorLogFetch.display.<locals>.journal_formatzcups.service)Z_SYSTEMD_UNITc                    s   g | ]} |qS r   r   .0r+   r,   r   r   
<listcomp>   s     z)ErrorLogFetch.display.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )stripr-   r   r   r   r0      s     r   zTsu -c 'journalctl -u cups.service --since="%s" --until="%s"' > troubleshoot-logs.txt)r   answersZ
get_windowgetr   datetimeZfromtimestampr"   r*   authconnr   ZadminGetServerSettingsoprunr   ZIPPErrorZCUPS_SERVER_DEBUG_LOGGINGr   ReaderZseek_cursorZ	add_matchopenseek	readlinesr   r   lenr   Zset_text)r   r2   r   Z
checkpointZcursorZ	timestampr   Znowr%   Zorig_settingsr   r&   rZtmpfnameflinescmdr   r/   r   display4   s    














zErrorLogFetch.displayc                 C   s   | j  }|| j |S )N)r   copyupdater2   )r   r2   r   r   r   collect_answer   s    
zErrorLogFetch.collect_answerc                 C   s4   | j   | jj}|d }| | _| j| jd< d S )NZ!_authenticated_connection_factoryr   )r6   Zcancelr   r2   Zget_connectionr5   )r   r2   factoryr   r   r   cancel_operation   s
    

zErrorLogFetch.cancel_operationN)__name__
__module____qualname__r
   rA   rD   rF   r   r   r   r   r   &   s   or   )Zgi.repositoryr   r   r   Ztempfiler   r4   r"   Ztimedopsr   baseZsystemdr   r	   r   r   r   r   r   <module>   s   
