3
d0                 @   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	Z	ddl
ZddlmZmZ ddlmZ ddlmZ dddZdZdd ZG dd deZejdd ZdS )zy
Created on 20 Apr 2010

logging options : INFO, DEBUG
hash_method : content, timestamp

@author: Chris Filo Gorgolewski
    N)warn)LooseVersion)loaddump   )str2bool)SoftFileLockmonitoring.enabled1.0logging.utils_level)Zprofile_runtimeZfilemanip_levelaA  [logging]
workflow_level = INFO
utils_level = INFO
interface_level = INFO
log_to_file = false
log_directory = {log_dir}
log_size = 16384000
log_rotate = 4

[execution]
create_report = true
crashdump_dir = {crashdump_dir}
hash_method = timestamp
job_finished_timeout = 5
keep_inputs = false
local_hash_check = true
matplotlib_backend = Agg
plugin = Linear
remove_node_directories = false
remove_unnecessary_outputs = true
try_hard_link_datasink = true
single_thread_matlab = true
crashfile_format = pklz
stop_on_first_crash = false
stop_on_first_rerun = false
use_relative_paths = false
stop_on_unknown_version = false
write_provenance = false
parameterize_dirs = true
poll_sleep_duration = 2
xvfb_max_wait = 10
check_version = true

[monitoring]
enabled = false
sample_frequency = 1
summary_append = true

[check]
interval = 1209600
c             C   sR   yt j|  W n> tk
rL } z"|jtjkr:t jj| r:n W Y d d }~X nX d S )N)osmakedirsOSErrorerrnoEEXISTpathisdir)r   exc r   5/tmp/pip-build-7vycvbft/nipype/nipype/utils/config.pymkdir_pL   s    r   c               @   s   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	d,ddZ
dd Zdd Zdd Zedd Zdd Zdd Zdd Zdd Zdd  Zed!d" Zejd#d" Zd$d% Zd&d' Zd(d) Zd*d+ ZdS )-NipypeConfigzBase nipype config classc             O   s   t j | _d | _tjjtjddd}tjj|d| _	| j
  d | _d | _| jjtjj|ddg x^tD ]V}xPdD ]H}| j||rtt| d	 jd
\}}| j||st| j||| j|| qtW qjW d S )NZNIPYPE_CONFIG_DIRz	~/.nipype)defaultznipype.jsonz
nipype.cfg	executionlogging
monitoringr   .)r   r   r   )configparserConfigParser_config_cwdr   r   
expandusergetenvjoin	data_fileset_default_config_display_resource_monitorreadCONFIG_DEPRECATIONS
has_optionsplitsetget)selfargskwargsZ
config_diroptionsectionZnew_sectionZ
new_optionr   r   r   __init__Y   s    


zNipypeConfig.__init__c             C   sN   | j dkrHytj | _ W n. tk
rF   tdjtjddt  Y nX | j S )z$Cache current working directory ASAPNz7Trying to run Nipype from a nonexistent directory "{}".ZPWDunknown)r    r   getcwdr   r   formatr"   RuntimeWarning)r.   r   r   r   cwdp   s    
zNipypeConfig.cwdc             C   s^   t jtjjd| jd}y| jj| W n0 tk
rX   ddl	m
} | jj|| Y nX dS )z9Read default settings template and set into config object~)log_dirZcrashdump_dirr   )StringION)DEFAULT_CONFIG_TPLr6   r   r   r!   r8   r   read_stringAttributeErrorior;   readfp)r.   Zdefault_cfgr;   r   r   r   r%      s    

zNipypeConfig.set_default_configc             C   s|   ddl m} | jjddd | jjddd | jjddd | jjd	d
d | jjd	dd | jjd	dd |j| j dS )zEnables debug configuration   )r   r   Zstop_on_first_crashtrueZremove_unnecessary_outputsfalseZkeep_inputsr   Zworkflow_levelDEBUGZinterface_levelZutils_levelN) r   r   r,   Zupdate_logging)r.   r   r   r   r   enable_debug_mode   s    zNipypeConfig.enable_debug_modec             C   s   | j jdd| dS )zSets logging directory

        This should be the first thing that is done before any nipype class
        with logging is imported.
        r   Zlog_directoryN)r   r,   )r.   r:   r   r   r   set_log_dir   s    zNipypeConfig.set_log_dirNc             C   sd   |t krDd|t | d t | d f }t| t | d jd\}}| jj||r`| jj||S |S )zGet an optionzPConfig option "%s" has been deprecated as of nipype %s. Please use "%s" instead.r   r   r   )r)   r   r+   r   r*   r-   )r.   r2   r1   r   msgr   r   r   r-      s    zNipypeConfig.getc             C   sf   t |trt|}|tkrVd|t| d t| d f }t| t| d jd\}}| jj|||S )zSet new value on optionzPConfig option "%s" has been deprecated as of nipype %s. Please use "%s" instead.r   r   r   )
isinstanceboolstrr)   r   r+   r   r,   )r.   r2   r1   valuerH   r   r   r   r,      s    
zNipypeConfig.setc             C   s   | j j||S )z!Get a boolean option from section)r   
getboolean)r.   r2   r1   r   r   r   rM      s    zNipypeConfig.getbooleanc             C   s   | j j||S )z!Check if option exists in section)r   r*   )r.   r2   r1   r   r   r   r*      s    zNipypeConfig.has_optionc             C   s   | j jS )N)r   	_sections)r.   r   r   r   rN      s    zNipypeConfig._sectionsc             C   s`   t jj| jsdS td| j & t| jd}t|}W dQ R X W dQ R X ||kr\|| S dS )zRead options fileNz%s.lockrt)r   r   existsr$   r   openr   )r.   keyfiledatadictr   r   r   get_data   s    zNipypeConfig.get_datac             C   s   i }t jj| jrNtd| j & t| jd}t|}W dQ R X W dQ R X n"t jj| j}t jj|spt| td| j 0 t| jd}|||< t	|| W dQ R X W dQ R X dS )zStore config fliez%s.lockrO   Nwt)
r   r   rP   r$   r   rQ   r   dirnamer   r   )r.   rR   rL   rT   rS   rW   r   r   r   	save_data   s    zNipypeConfig.save_datac             C   sV   xPdD ]H}||krx:t || j D ]&\}}|jds$| jj||t| q$W qW dS )z+Extend internal dictionary with config_dictr   r   check__N)r   r   rY   )listitems
startswithr   r,   rK   )r.   config_dictr2   rR   valr   r   r   update_config   s
    

zNipypeConfig.update_configc             C   s   ddl }|j| jdd dS )z&Set backend on matplotlib from optionsr   Nr   Zmatplotlib_backend)
matplotlibZuser-   )r.   ra   r   r   r   update_matplotlib   s    zNipypeConfig.update_matplotlibc             C   s$   | j jddd | j jddd dS )zSets provenance storing onr   Zwrite_provenancerB   Zhash_methodcontentN)r   r,   )r.   r   r   r   enable_provenance   s    zNipypeConfig.enable_provenancec             C   s.   | j dk	r| j S t| jjddp$d| _| j S )z&Check if resource_monitor is availableNr   enabledF)r'   r   r   r-   resource_monitor)r.   r   r   r   rf      s    
zNipypeConfig.resource_monitorc             C   s   t |ttfrt|j }|dkr*d| _nz|dkr| jsd| _z:y dd l}t|jtdk| _W n t	k
rt   Y nX W d | jst
d | jjddd| j j  X d S )	NFTr   z5.0zICould not enable the resource monitor: psutil>=5.0 could not be imported.r   re   z%s)rI   rK   bytesr   lowerr'   psutilr   __version__ImportErrorr   r   r,   )r.   rL   ri   r   r   r   rf     s&    
c             C   s
   d| _ dS )zSets the resource monitor onTN)rf   )r.   r   r   r   enable_resource_monitor&  s    z$NipypeConfig.enable_resource_monitorc             C   s
   d| _ dS )zSets the resource monitor offFN)rf   )r.   r   r   r   disable_resource_monitor*  s    z%NipypeConfig.disable_resource_monitorc             C   s,  | j dk	rd| j j S d}| jjddr6| jjdd}|pBtjd}|rddlm} dd	 }|j	d
d j	dd }|dddg}|t
||| _ | j S dtjkrtd|dkrtjd= yddlm} W n tk
r   tdY nX |dd| _ | j j  t| j ds t| j d| j j | j S dS )z#Returns the first display availableNz:%dr   Zdisplay_variableZDISPLAYr   )
namedtuplec               S   s   d S )Nr   r   r   r   r   _mockC  s    z'NipypeConfig.get_display.<locals>._mock:r   r   Xvfbnew_displaystopdarwinzXvfb requires root permissions to run in OSX. Please make sure that an X server is listening and set the appropriate config on either $DISPLAY or nipype's "display_variable" config. Valid X servers include VNC, XQuartz, or manually started Xvfb.rE   )rq   zZA display server was required, but $DISPLAY is not defined and Xvfb could not be imported.Ztcp)Znolisten)r&   rr   r   r*   r-   r   r"   collectionsrn   r+   intget_displaysysplatformRuntimeErrorenvironZxvfbwrapperrq   rk   starthasattrsetattrZvdisplay_num)r.   Z
sysdisplayrn   ro   Zndisprq   r   r   r   rx   .  s8    



zNipypeConfig.get_displayc             C   s4   | j dk	r0ddlm} | j j  |jdjd dS )zCloses the display if startedNrA   )r   znipype.interfacezClosing display (if virtual))r&   rE   r   rs   	getLoggerdebug)r.   r   r   r   r   stop_displayh  s    

zNipypeConfig.stop_display)N)__name__
__module____qualname____doc__r3   propertyr8   r%   rF   rG   r-   r,   rM   r*   rN   rU   rX   r`   rb   rd   rf   setterrl   rm   rx   r   r   r   r   r   r   V   s,   
:r   c              C   s   ddl m}  | j  dS )z"Stop virtual display (if it is up)rA   )configN)rE   r   r   )r   r   r   r   free_displayq  s    r   )r	   r
   )r   r
   )r   r   ry   r   atexitwarningsr   distutils.versionr   r   numpynpZ
simplejsonr   r   Zmiscr   Zfilelockr   r)   r<   r   objectr   registerr   r   r   r   r   <module>   s&   -
  