o
    va"                     @   s(  d Z ddlmZmZ ddlZddlZddlZddlZddlZddl	Z	ddl
mZ ejd dkZerddlZddlmZmZmZ ddlmZ ddlmZmZmZmZmZ dd	lmZmZ dd
lmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z& e&  e'Z'e(Z(e)Z)e*Z+e,Z-e.Z/e0Z1e2Z3e4dZ5e4dZ6e4dZ7dd Z8dd Z9dd Z:dd Z;dd Z<dd Z=dd Z>dd  Z?dRd"d#Z@dRd$d%ZAd&d' ZBdRd(d)ZCd*d+ ZDndd,lEmFZB ddlGZdd-lHmIZ) dd.lmJZmZmZ dd	lmZmZ ddlKmZmZmZmZmZ dd/lLmZm!Z!m"Z" ddl$m$Z$ ddl%m&Z& e&  eMZ'eNZ(ejOe_PeQZ+e*Z-ejRZ/e1Z1e2Z3e4d0Z5e4d1Z6e4d2Z7d3d Z8d4d5 Zd6d Z9d7d Z:d8d Z;d9d Z<d:d Z=d;d Z>d<d  Z?dRd=d#Z@dRd>d%ZAdRd?d)ZCd@d+ ZDdAdB ZSdCdD ZTdEdF ZUdGdH ZVdIdJ ZWdSdLdMZXdNdO ZYdPdQ ZZdS )TzPython 2/3 compatibility shims.    )print_functionunicode_literalsN)logger   )ConfigParserNoOptionErrorNoSectionErrormean)FaultProtocolErrorServerProxy	TransportServer)SimpleXMLRPCRequestHandlerSimpleXMLRPCServer)urlopen)	HTTPErrorURLError)urlparse)monkey_patchkeysvaluesitemsc                 C   s   t | dd dS )1Print and flush (used by stdout* outputs modules)T)flushN)printstring r   0/usr/lib/python3/dist-packages/glances/compat.pyprintandflushB   s   r!   c                 C   s"   t | tr	|  S | dd S )zXConvert the bytes string to a ASCII string
        Usefull to remove accent (diacritics)asciiignore)
isinstancebinary_typedecodeencodesr   r   r    to_asciiF   s   
r*   c                 C      t |  S N)listr   dr   r   r    	listitemsM      r0   c                 C   r+   r,   )r-   r   r.   r   r   r    listkeysP   r1   r2   c                 C   r+   r,   )r-   r   r.   r   r   r    
listvaluesS   r1   r3   c                 C   r+   r,   )iterr   r.   r   r   r    	iteritemsV   r1   r5   c                 C   r+   r,   )r4   r   r.   r   r   r    iterkeysY   r1   r6   c                 C   r+   r,   )r4   r   r.   r   r   r    
itervalues\   r1   r7   replacec                 C      t | tr| S | jd|dS Nutf-8)errors)r$   	text_typer&   r)   r<   r   r   r    u_      
r?   c                 C   r9   r:   r$   r%   r'   r>   r   r   r    bd   r@   rB   c                 C   s   | S )zVOnly in Python 2...
        from future.utils import bytes_to_native_str as n
        r   r(   r   r   r    ni   s   rC   c                 C   2   t | tr| S t | ttfr|  S | jd|dS r:   )r$   r=   intfloat__str__r&   r>   r   r   r    	nativestro   
   
rH   c              
   C   sj   zt j| dt jdjd}W |
 S  ty4 } ztd	| | d}W Y d}~|
 S d}~ww )z7Execute a system command and return the result as a str )stdoutr;   z Can not evaluate command {} ({}) N)
subprocessrunsplitPIPErK   r&   	Exceptionr   debugformatrstripZcommandreser   r   r    system_execw   s   rX   )bytes_to_native_str)imap)SafeConfigParserr   r   )r   r   r   viewkeys
viewvalues	viewitemsc                 C   s   t |  tj  dS )r   N)r   sysrK   r   r   r   r   r    r!      s   c                 C   s   t t| tt| d S )N   )rF   summaxlen)Znumbersr   r   r    r
      s   r
   c                 C   s"   t | tr| S td| ddS )zWConvert the unicode 's' to a ASCII string
        Usefull to remove accent (diacritics)ZNFKDr"   r#   )r$   r%   unicodedata	normalizer'   r(   r   r   r    r*      s   
c                 C      |   S r,   )r   r.   r   r   r    r0         c                 C   rf   r,   )r   r.   r   r   r    r2      rg   c                 C   rf   r,   )r   r.   r   r   r    r3      rg   c                 C   rf   r,   )r5   r.   r   r   r    r5      rg   c                 C   rf   r,   )r6   r.   r   r   r    r6      rg   c                 C   rf   r,   )r7   r.   r   r   r    r7      rg   c                 C   s&   t | tr| jd|dS | jd|dS r:   )r$   r=   r'   r&   r>   r   r   r    r?      s   
c                 C   r9   r:   rA   r>   r   r   r    rB      r@   c                 C   rD   r:   )r$   r%   rE   rF   rG   r'   r>   r   r   r    rH      rI   c              
   C   s\   zt | d}W | S  ty- } ztd| | d}W Y d}~| S d}~ww )z6Execute a system command and return the resul as a strrJ   zCan not execute command {} ({})rL   N)rM   Zcheck_outputrO   rQ   r   rR   rS   rT   rU   r   r   r    rX      s   c                    sB   t  |kr S ttt  t|  fddtd|D S )zCompute a simple mean subsampling.

    Data should be a list of numerical itervalues

    Return a subsampled list of sampling lenght
    c                    s(   g | ]}t  | |d    qS r`   r	   .0r)   datasampling_lengthr   r    
<listcomp>      ( zsubsample.<locals>.<listcomp>r   )rc   rE   roundrF   range)rl   samplingr   rk   r    	subsample   s   rs   c                    s   t | |kr| S dd | D dd | D ttt | t|   fddtd|D } fddtd|D }tt||S )zCompute a simple mean subsampling.

    Data should be a list of set (time, value)

    Return a subsampled list of sampling length
    c                 S      g | ]}|d  qS )r   r   rj   tr   r   r    rn          z(time_serie_subsample.<locals>.<listcomp>c                 S   rt   rh   r   ru   r   r   r    rn      rw   c                    s(   g | ]}|  |d     d qS )r`   r   r   ri   )rm   rv   r   r    rn      ro   r   c                    s(   g | ]}t |  |d     qS rh   r	   ri   )rm   vr   r    rn      ro   )rc   rE   rp   rF   rq   r-   zip)rl   rr   Zt_subsampledZv_subsampledr   )rm   rv   rx   r    time_serie_subsample   s   rz   c                 C   s   | d d S )zConvert Celsius to Fahrenheit.g?    r   )Zcelsiusr   r   r    to_fahrenheit   s   r|   c               
   C   sd   t jdkr,ddl} ddl}z| jj W S  ty+ } z|  W Y d}~dS d}~ww t 	 dkS )aC  
    https://stackoverflow.com/a/19719292
    @return: True if the current user is an 'Admin' whatever that
    means (root on Unix), otherwise False.
    Warning: The inner function fails unless you have Windows XP SP2 or
    higher. The failure causes a traceback to be printed and this
    function to return False.
    ntr   NF)
osnamectypes	tracebackZwindllZshell32ZIsUserAnAdminrQ   	print_excgetuid)r   r   rW   r   r   r    is_admin  s   

r   c                 C   s   | |v o	||  d uS r,   r   )kr/   r   r   r    key_exist_value_not_none  s   r   rL   c                 C   s    | |v o||  d uo||  |kS r,   r   )r   r/   rx   r   r   r    key_exist_value_not_none_not_v   s    r   c                 C   $   t | d| d t | d| d dS )z2Set disable_<var> to True in the class class_name.enable_Fdisable_TNsetattr
class_namevarr   r   r    disable(     r   c                 C   r   )z3Set disable_<var> to False in the class class_name.r   Tr   FNr   r   r   r   r    enable.  r   r   )r8   )rL   )[__doc__Z
__future__r   r   operatorr_   rd   typesrM   r~   Zglances.loggerr   version_infoZPY3queueconfigparserr   r   r   Z
statisticsr
   Zxmlrpc.clientr   r   r   r   r   Zxmlrpc.serverr   r   Zurllib.requestr   Zurllib.errorr   r   Zurllib.parser   Zdefusedxml.xmlrpcr   inputrq   mapstrr=   bytesr%   boolZ	bool_typerE   ZlongOSErrorPermissionErrormethodcallerr\   r]   r^   r!   r*   r0   r2   r3   r5   r6   r7   r?   rB   rC   rH   rX   Zfuture.utilsrY   ZQueue	itertoolsrZ   r[   Z	xmlrpclibZurllib2Z	raw_inputZxrangeZreadfpZ	read_fileZunicodeZBooleanTypers   rz   r|   r   r   r   r   r   r   r   r   r    <module>   s   













