U
    md5                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ d	d
 Zdd Zdd Zdd ZddddddddddddgfddZdS )zr
session_info finds and prints version information for loaded modules in the
current session, Python, and the OS.
    N)datetime)import_module)cleandoc)	cpu_count)Path)stdlib_listc              	   c   sT   | D ]J\}}t |tjr"|jV  qz|jdd V  W q tk
rL   Y qX qdS )zFind modules in an environment..r   N)
isinstancetypes
ModuleType__name__
__module__splitAttributeError)environnameval r   J/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/session_info/main.py_imports   s    
r   c                 C   sn   t | tst | tr| S t | tr:ddd | D }|S t| rfz|  W S  tk
rb   tY qjX ndS dS )z#Find the version number of a moduler   c                 S   s   g | ]}t |qS r   )str).0numr   r   r   
<listcomp>'   s     z!_find_version.<locals>.<listcomp>NAN)r	   r   inttuplejoincallable	Exceptionr   )Zmod_version_attrZjoined_tupler   r   r   _find_version!   s    

r    c            
      C   s   ddl } ddl}ddl}ddlm} t|  j}|ddd dd }|	 D ]}|d dkr|d	 s|j
|d
 d }n|j
|d
 d |d  }||}|D ].}	|	d d |krt|	d d j    S qqTdS )zReturns the absolute path of the Notebook or None if it cannot be determined
    NOTE: works only when the security is token-based or there is also no password
    r   N)notebookapp-   r   token passwordurlzapi/sessionszapi/sessions?token=Zkernelidnotebookpath)	ipykerneljsonurllibr)   r!   r   Zget_connection_filer   r   Zlist_running_serversrequesturlopenloadstem)
r+   r,   r-   r!   Zconnection_fileZ	kernel_idZsrvreqsessionssessr   r   r   _notebook_basename;   s    
r5   c                    s6   |rt  fdd| D }nt  fdd| D }|S )Nc                 3   s.   | ]&}| d d  kr| d d V  qdS )r   r   N)r   r   moduleexcludesr   r   	<genexpr>U   s    z!_clean_modules.<locals>.<genexpr>c                 3   s8   | ]0}| d d  kr|ds| d d V  qdS )r   r   _N)r   
startswithr6   r8   r   r   r:   X   s    
)set)modulesprivater9   clean_modulesr   r8   r   _clean_modulesR   s    rA   TFbuiltinsr   c           .         s  |sDzt t dd d}W n tk
r:   t d}Y nX |
| }
dtj k}|rb|	dkrbd}	|sr|dkr|rd}dddd	d
g}g }|D ],}z|t| W q t	k
r   Y qX qdd |D }d
|}nd}ttt d d d }ttt| ||
}d|i}|dk	rd|	s,|rdd}tttj ||
}||}|r`||d< nd}i }|D ]>}|| }|s|r|D ]}|j|kr||j qg }g }g }|D ]}|| tj| }zt|j} W nf tk
rF   zt|j} W n@ tk
r@   zt|j} W n tk
r:   d} Y nX Y nX Y nX ||  qtdd |D   fddt||D }| sdd |D }t|}d
|||< ql|s|dkrr|rr|dkr|rzt  d}W n tk
r   d}Y nX nd}dd |d  dD }!|r.|!| }"n|!}"dd |"D }#t!|d"}$|#D ]}%|$"d#|% qPW 5 Q R X dtj$dd  }&|rt nd }'|rt% d krt&  d!t%  }(nt&  d"}(nd }(d##t'( )d$})d}*|dkrd n|* d%|* | }+|	rxdd&l*m+}, |r4d'|d  d(}-nd }-|,t,d)|d  |- |+ d*|& d|' d|( d*|) d+$d,dS |rd*|d  }-nd }-t-t,d*|d  |- |+ d*|& d|' d|( d*|) $d,d dS )-aR  
    Show version information for loaded modules in the current session,
    Python, and the OS.

    Parameters
    ----------
    na : bool
        Show module name even when no version number is found.
    os : bool
        Show OS information.
    cpu : bool
        Show number of logical CPU cores and info string (if available).
    jupyter : bool
        Show information about the jupyter environment. If `None`, output
        jupyter info only if inside a Jupyter notebook.
    dependencies : bool
        Show information about modules imported by the Python interpreter on
        startup and depency modules imported via other modules. If `None`,
        dependency modules will be included in the HTML output under a
        <details> tag, and excluded from the printed output. Setting `na` to
        `False` could be helpful to reduce verboseness.
    std_lib : bool
        Show information for modules imported from the standard library.
        Tries to detect the Python version to compare with the corresponding
        standard libarary, falls back to Python 3.7 if the version cannot be
        detected.
    private : bool
        Show information for private modules.
    write_req_file: bool
        Create a pip-compatible text file that lists all the module versions.
        If `None`, write dependency files for Jupyter notebooks only. If the
        `notebook` module is installed, `session_info` can automatically find the
        notebook name use it as a prefix for the requirments file. Otherwise
        it falls back to `session_info-requirements.txt`.
        This only writes explicitly imported modules.
    req_file_name : str
        Change the name of the requirements file.
    html: bool
        Format the output as HTML and collapse it in a <details> tag. If `None`,
        HTML will be used only if a Jupyter notebook environment is detected.
        Note that this will not be visible in notebooks shared on GitHub since
        they seemingly do not support the <details> tag. Requires IPython.
    excludes : list
        Do not show version information for these modules.
    N)versionz3.7Zjupyter_coreTZIPythonZjupyter_clientZ
jupyterlabr)   c                 S   s"   g | ]}d |j dd|j qS )z            Z14	)r   __version__r6   r   r   r   r      s   zshow.<locals>.<listcomp>
r#   r   	f_globalsZimportedFZdependr   c                 S   s   g | ]}t |qS r   )len)r   mod_namer   r   r   r      s     c                    s"   g | ]\}}|  d | qS )rE   r   )r   rJ   mod_versionZmax_name_lenr   r   r      s   c                 S   s    g | ]}|d d dks|qS )rC   Nr   r   )r   xr   r   r   r      s      z
            z-requirements.txtzsession_info-requirements.txtc                 S   s   g | ]}| d dqS )z	NAr%   replacer   Zmod_verr   r   r   r      s   c                 S   s    g | ]}| d d ddqS ) r%   rE   z==rN   rP   r   r   r   r     s   wz{}
zPython r%   z logical CPU cores, z logical CPU coresz!Session information updated at {}z%Y-%m-%d %H:%Mz            -----)HTMLz
            -----
            </pre>
            <details>
            <summary>Click to view modules imported as dependencies</summary>
            <pre>
            z|
            </pre>
            </details> <!-- seems like this ends pre, so might as well be explicit -->
            <pre>z
            <details>
            <summary>Click to view session information</summary>
            <pre>
            -----
            z
            -----
            z*
            </pre>
            </details>z

).r   platformpython_version
ValueErrorsysr>   keysappendr   ModuleNotFoundErrorr   dictinspect
getmembersstackrA   r=   r   items
differencer   remover    rF   r   rD   VERSIONmaxzipsortedr5   r   r   openwriteformatrO   	processorr   r   nowstrftimeIPython.displayrS   r   print).naoscpujupyterdependenciesZstd_libr?   Zwrite_req_fileZreq_file_namehtmlr9   Zstd_modulesZin_notebookZjup_mod_namesZjup_modulesZjup_mod_nameZjup_mod_and_verZoutput_jupyterZcaller_globalsZimported_modulesZall_modulesZsys_modulesZdepend_modulesZoutput_modulesmod_typer@   Z
jup_moduleZmod_and_verZ	mod_namesZmod_versionsrJ   modrK   Zmods_na_removedZmods_req_fileZclean_mods_req_filefZ
mod_to_reqZ
sys_outputZ	os_outputZ
cpu_outputZdate_outputnlZoutput_jup_strrS   Zoutput_depend_strr   rL   r   show^   s8   1 









	
  rx   )__doc__rW   r
   rT   r\   r   	importlibr   r   multiprocessingr   pathlibr   r   r   r    r5   rA   rx   r   r   r   r   <module>   s,   
    