o
    va                     @   sP   d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 G dd de	Z
dS )zJSON interface class.    N)PY3listkeys)logger)GlancesExportc                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
Exportz*This class manages the JSON export module.Nc              
      s   t t| j||d |j| _ztrt| jd| _| j  nt| jd| _| j  W n t	yJ } zt
d| td W Y d}~nd}~ww t
d| j d| _i | _dS )	zInit the JSON export IF.)configargswwbzCannot create the JSON file: {}   NzExporting stats to file: {}T)superr   __init__Zexport_json_filejson_filenamer   open	json_filecloseIOErrorr   ZcriticalformatsysexitinfoZexport_enablebuffer)selfr   r   e	__class__ >/usr/lib/python3/dist-packages/glances/exports/glances_json.pyr      s"   

zExport.__init__c                 C   s   t d| j  | j  dS )zClose the JSON file.zFinalise export interface %sN)r   debugZexport_namer   r   )r   r   r   r   r   )   s   zExport.exitc                 C   s   ||   d krh| ji krhtdt| j| j trAt| jd| _	| j	
dt| j W d   n1 s;w   Y  n$t| jd| _	| j	
dt| j W d   n1 s`w   Y  i | _tt||| j|< dS )z"Export the stats to the JSON file.r   z&Exporting stats ({}) to JSON file ({})r	   z{}
Nr
   )Zplugins_to_exportr   r   r   r   r   r   r   r   r   writejsondumpsdictzip)r   namecolumnsZpointsr   r   r   export.   s   zExport.export)NN)__name__
__module____qualname____doc__r   r   r&   __classcell__r   r   r   r   r      s
    r   )r*   r   r    Zglances.compatr   r   Zglances.loggerr   Zglances.exports.glances_exportr   r   r   r   r   r   <module>   s    