o
    va                     @   sl   d Z ddlZddlZddlZddlZddlmZmZm	Z	 ddl
mZ ddlmZ G dd deZdd	 ZdS )
zCSV interface class.    N)PY3iterkeys
itervalues)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 CSV export module.Nc              
      s6  t t| j||d |j| _tj| jr|jrd}d| _	n<d}zt
| jd| _t| j}W n tyN } ztd| td W Y d}~nd}~ww t|d| _	| j  zt
| j|| _t| j| _W n ty } ztd| td W Y d}~nd}~ww td	| j d
| _d
| _dS )zInit the CSV export IF.)configargswNarz!Cannot open existing CSV file: {}   zCannot create the CSV file: {}zStats exported to CSV file: {}T)superr   __init__Zexport_csv_fileZcsv_filenameospathisfileZexport_csv_overwrite
old_headeropen_csv_filecsv_filecsvreaderIOErrorr   ZcriticalformatsysexitnextclosewriterinfoZexport_enable
first_line)selfr   r	   	file_moder   e	__class__ =/usr/lib/python3/dist-packages/glances/exports/glances_csv.pyr   $   s6   

zExport.__init__c                 C   s   t d| j  | j  dS )zClose the CSV file.zFinalise export interface %sN)r   debugZexport_namer   r   )r!   r&   r&   r'   r   L   s   zExport.exitc                    sN  |j  d}jrdg}tdg} D ]J t|  tr?|  D ]jr7| fddD 7 }|t7 }q%qt|  trbjrZt	|  }| fdd|D 7 }|t|  7 }qjrj
du rqj| j
|krtd td	j
 td
| nd_
d_j
du rj| j  dS dS )z$Update stats in the CSV output file.)Zplugin_listZ	timestampz%Y-%m-%d %H:%M:%Sc                 3   s$    | ]}d   |V  qdS )z{}_{}_{}N)r   Zget_item_key).0itempluginr!   statr&   r'   	<genexpr>a   s    z Export.update.<locals>.<genexpr>c                 3   s    | ]	}d   |V  qdS )z{}_{}N)r   )r)   Z	fieldname)r,   r&   r'   r.   i   s    Nz@Cannot append data to existing CSV file. Headers are differents.zOld header: {}zNew header: {}F)ZgetAllExportsAsDictZplugins_to_exportr    timestrftime
isinstancelistr   dictr   r   r   Zwriterowr   errorr(   r   r   flush)r!   ZstatsZ	all_statsZ
csv_headerZcsv_dataZ
fieldnamesr&   r+   r'   updateQ   sD   



zExport.update)NN)__name__
__module____qualname____doc__r   r   r6   __classcell__r&   r&   r$   r'   r       s
    (r   c                 C   s(   t rt| |dd}|S t| |d }|S )N )newlineb)r   open)	file_namer"   r   r&   r&   r'   r      s
   r   )r:   os.pathr   r   r   r/   Zglances.compatr   r   r   Zglances.loggerr   Zglances.exports.glances_exportr   r   r   r&   r&   r&   r'   <module>   s   e