3
ZK              
   @   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 y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 j| |d | jtdtd}tj | _| jjd |j| jddd |j	||  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 j }i | _|jd}|jd}|jd}d| jksJd| jkrRd }d }dd }tjjtj jd}| j jd	 | _	d
|kr6y t
| j	j|d| _| jj }W n tjk
r   dS X d|tj< |d }	|	jdd|d< d}
dd }y.t
|| j	|f|d| _| jj  d| jd< W n tjk
r4   Y nX i | _tr|d k	rdd  tj }|j| |jdd  fdd|D | jd< |d k	rt
|| j	f|d| _| jj }|d k	rt|}|j| |j }tj| ddd |D i| _t| jjdg t| jjdg  dkr:d||f }| jj| dS dS )NZerror_log_checkpointZerror_log_cursorZerror_log_timestampZ	error_logr   c             S   s   | j  }| jd | j  tddr}d}y| jd|j d}W n< tjk
r|   ytj	|j W n t
k
rv   Y nX Y nX | j| |r|jS W d Q R X d S )NF)deletez/admin/log/error_logT)Z_get_prompt_allowedZ_set_prompt_allowed_connectr   ZgetFilefilecupsZ	HTTPErrorosremoveOSError)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   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      )ZadminSetServerSettingstimeZsleepr   RuntimeError)Z
connectionsettingsZattemptr   r   r   set_settingsd   s    


z+ErrorLogFetch.display.<locals>.set_settingsTZerror_log_debug_logging_unsetc             S   sP   yd| d  }W n t tfk
r,   d}Y nX |d | d j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 | ]}|j  qS r   )strip)r+   r)   r   r   r   r,      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opZrunr   ZIPPErrorZCUPS_SERVER_DEBUG_LOGGINGr   ReaderZseek_cursorZ	add_matchopenseek	readlinesr   r   lenr   Zset_text)r   r.   r   Z
checkpointZcursorZ	timestampr   Znowr#   Zorig_settingsr   r$   rZtmpfnameflinescmdr   )r*   r   display4   st    



















zErrorLogFetch.displayc             C   s   | j j }|j| j |S )N)r   copyupdater.   )r   r.   r   r   r   collect_answer   s    
zErrorLogFetch.collect_answerc             C   s4   | j j  | jj}|d }|j | _| j| jd< d S )NZ!_authenticated_connection_factoryr   )r2   Zcancelr   r.   Zget_connectionr1   )r   r.   factoryr   r   r   cancel_operation   s
    

zErrorLogFetch.cancel_operationN)__name__
__module____qualname__r
   r<   r?   rA   r   r   r   r   r   &   s   or   )Zgi.repositoryr   r   r   Ztempfiler   r0   r!   Ztimedopsr   baseZsystemdr   r	   r   r   r   r   r   <module>   s   
