o
    va                     @   sD   d Z ddlZddlmZmZmZmZ ddlmZ G dd de	Z
dS )z5
I am your father...

...for all Glances exports IF.
    N)NoOptionErrorNoSectionError	iteritemsiterkeys)loggerc                   @   st   e Zd ZdZg dZdddZdd Zdd	 Zd
d ZddgdfddZ	dd Z
dd Zdd Zdd Zdd ZdS )GlancesExportz!Main class for Glances export IF.)ZcpuZpercpuloadZmemZmemswapZnetworkZdiskioZfsZprocesscountZipsystemZuptimeZsensorsZdockerZgpuNc                 C   sR   | j jtdd | _td| j  || _|| _d| _d| _	d| _
|  | _dS )zInit the export class.Zglances_NzInit export module %sF)	__class__
__module__lenexport_namer   debugconfigargsexport_enablehostport_plugins_to_exportexport_list)selfr   r    r   @/usr/lib/python3/dist-packages/glances/exports/glances_export.py__init__7   s   zGlancesExport.__init__c                 C   s   t d| j  dS )zClose the export module.zFinalise export interface %sN)r   r   r   r   r   r   r   exitL   s   zGlancesExport.exitc                 C   s.   | j }|D ]}t| jd| r|| q|S )z%Return the list of plugins to export.Zdisable_)exportable_pluginsgetattrr   remove)r   retpr   r   r   r   P   s   
z GlancesExport._plugins_to_exportc                 C   s   | j S N)r   r   r   r   r   plugins_to_exportX   s   zGlancesExport.plugins_to_exportr   r   c              
      s  |pg } j du rdS z|D ]}t | j || qW n- ty/   td| Y dS  tyJ } ztd|| W Y d}~dS d}~ww |D ]}zt | j || W qM tye   Y qMw td| td| fdd|| D  d	S )
aK  Load the export <section> configuration in the Glances configuration file.

        :param section: name of the export section to load
        :param mandatories: a list of mandatories parameters to load
        :param options: a list of optionnals parameters to load

        :returns: Boolean -- True if section is found
        NFzNo {} configuration foundz"Error in the {} configuration ({})z+Load {} from the Glances configuration filez{} parameters: {}c                    s   i | ]}|t  |qS r   )r   ).0optr   r   r   
<dictcomp>|   s    z+GlancesExport.load_conf.<locals>.<dictcomp>T)	r   setattrZ	get_valuer   r   errorformatr   r   )r   sectionZmandatoriesoptionsr$   er   r   r   	load_conf[   s0   	
$zGlancesExport.load_confc                 C   sJ   z||d  }W n t y   td| Y nw t|tr#|d S |S )z#Return the value of the item 'key'.keyzNo 'key' available in {}r   )KeyErrorr   r'   r(   
isinstancelist)r   itemr   r   r   r   get_item_key   s   
zGlancesExport.get_item_keyc                 C   sP   i }|r&zt dd |dD }W |S  ty%   td| i }Y |S w |S )zParse tags into a dict.

        input tags: a comma separated list of 'key:value' pairs.
            Example: foo:bar,spam:eggs
        output dtags: a dict of tags.
            Example: {'foo': 'bar', 'spam': 'eggs'}
        c                 S   s   g | ]}| d qS ):)split)r#   xr   r   r   
<listcomp>   s    z,GlancesExport.parse_tags.<locals>.<listcomp>,zInvalid tags passed: %s)dictr4   
ValueErrorr   info)r   ZtagsZdtagsr   r   r   
parse_tags   s   zGlancesExport.parse_tagsc                 C   s   | j sdS |j|  d}|j|  d}|  D ]:}t|| tr,|| ||  nt|| trB|| D ]	}|||  q7nq| || \}}| 	||| qdS )zUpdate stats to a server.

        The method builds two lists: names and values
        and calls the export method to export the stats.

        Note: this class can be overwrite (for example in CSV and Graph).
        F)Zplugin_listT)
r   ZgetAllExportsAsDictr"   ZgetAllLimitsAsDictr/   r8   updater0   _GlancesExport__build_exportexport)r   statsZ	all_statsZ
all_limitsZpluginiexport_namesexport_valuesr   r   r   r<      s   zGlancesExport.updatec              	      s6  g }g }t |tr~dt|v r!|d t|v r!d||d  ndt|D ]R\ }t |tr5t|}t |trLz|d }W n t	yK   d}Y nw t |trk| 
|\}} fdd|D }||7 }||7 }q'|    || q'||fS t |tr|D ]}| 
|\}}||7 }||7 }q||fS )zBuild the export lists.r-   z{}. r   c                    s    g | ]}    t| qS r   )lowerstr)r#   r@   r-   Zpre_keyr   r   r6      s     z0GlancesExport.__build_export.<locals>.<listcomp>)r/   r8   r   r(   r   booljsondumpsr0   
IndexErrorr=   appendrD   )r   r?   rA   rB   valueZ
item_namesZitem_valuesr1   r   rF   r   Z__build_export   s:   







zGlancesExport.__build_exportc                 C   s   d S r!   r   )r   namecolumnsZpointsr   r   r   r>      s   zGlancesExport.export)NN)__name__r   __qualname____doc__r   r   r   r   r"   r,   r2   r;   r<   r=   r>   r   r   r   r   r       s    
%&r   )rQ   rH   Zglances.compatr   r   r   r   Zglances.loggerr   objectr   r   r   r   r   <module>   s
   