1313import os
1414import errno
1515import atexit
16+ from warnings import warn
1617from io import StringIO
1718from distutils .version import LooseVersion
1819import configparser
1920import numpy as np
2021
21- < << << << HEAD
22- from builtins import str , object , open
23- from ..external import portalocker
24- import configparser
25-
26- from future import standard_library
27- standard_library .install_aliases ()
28- == == == =
2922from builtins import bytes , str , object , open
30-
3123from simplejson import load , dump
3224from future import standard_library
33- from .. external import portalocker
25+
3426from .misc import str2bool
27+ from ..external import portalocker
3528
3629standard_library .install_aliases ()
3730
4033 'profile_runtime' : ('resource_monitor' , '1.0' ),
4134 'filemanip_level' : ('utils_level' , '1.0' ),
4235}
43- > >> >> >> upstream / master
4436
4537NUMPY_MMAP = LooseVersion (np .__version__ ) >= LooseVersion ('1.12.0' )
4638
@@ -97,21 +89,17 @@ def mkdir_p(path):
9789
9890
9991class NipypeConfig (object ):
100- """
101- Base nipype config class
102- """
92+ """Base nipype config class"""
10393
10494 def __init__ (self , * args , ** kwargs ):
10595 self ._config = configparser .ConfigParser ()
10696 config_dir = os .path .expanduser ('~/.nipype' )
10797 config_file = os .path .join (config_dir , 'nipype.cfg' )
10898 self .data_file = os .path .join (config_dir , 'nipype.json' )
10999 self ._config .readfp (StringIO (default_cfg ))
110- < << << << HEAD
111100 self ._display = None
112- == == == =
113101 self ._resource_monitor = None
114- >> >> >> > upstream / master
102+
115103 if os .path .exists (config_dir ):
116104 self ._config .read ([config_file , 'nipype.cfg' ])
117105
@@ -127,8 +115,7 @@ def set_default_config(self):
127115 self ._config .readfp (StringIO (default_cfg ))
128116
129117 def enable_debug_mode (self ):
130- """Enables debug configuration
131- """
118+ """Enables debug configuration"""
132119 self ._config .set ('execution' , 'stop_on_first_crash' , 'true' )
133120 self ._config .set ('execution' , 'remove_unnecessary_outputs' , 'false' )
134121 self ._config .set ('execution' , 'keep_inputs' , 'true' )
@@ -144,6 +131,7 @@ def set_log_dir(self, log_dir):
144131 self ._config .set ('logging' , 'log_directory' , log_dir )
145132
146133 def get (self , section , option , default = None ):
134+ """Get an option"""
147135 if option in CONFIG_DEPRECATIONS :
148136 msg = ('Config option "%s" has been deprecated as of nipype %s. Please use '
149137 '"%s" instead.' ) % (option , CONFIG_DEPRECATIONS [option ][1 ],
@@ -156,6 +144,7 @@ def get(self, section, option, default=None):
156144 return default
157145
158146 def set (self , section , option , value ):
147+ """Set new value on option"""
159148 if isinstance (value , bool ):
160149 value = str (value )
161150
@@ -169,16 +158,19 @@ def set(self, section, option, value):
169158 return self ._config .set (section , option , value )
170159
171160 def getboolean (self , section , option ):
161+ """Get a boolean option from section"""
172162 return self ._config .getboolean (section , option )
173163
174164 def has_option (self , section , option ):
165+ """Check if option exists in section"""
175166 return self ._config .has_option (section , option )
176167
177168 @property
178169 def _sections (self ):
179170 return self ._config ._sections
180171
181172 def get_data (self , key ):
173+ """Read options file"""
182174 if not os .path .exists (self .data_file ):
183175 return None
184176 with open (self .data_file , 'rt' ) as file :
@@ -189,6 +181,7 @@ def get_data(self, key):
189181 return None
190182
191183 def save_data (self , key , value ):
184+ """Store config flie"""
192185 datadict = {}
193186 if os .path .exists (self .data_file ):
194187 with open (self .data_file , 'rt' ) as file :
@@ -204,21 +197,63 @@ def save_data(self, key, value):
204197 dump (datadict , file )
205198
206199 def update_config (self , config_dict ):
200+ """Extend internal dictionary with config_dict"""
207201 for section in ['execution' , 'logging' , 'check' ]:
208202 if section in config_dict :
209203 for key , val in list (config_dict [section ].items ()):
210204 if not key .startswith ('__' ):
211205 self ._config .set (section , key , str (val ))
212206
213207 def update_matplotlib (self ):
208+ """Set backend on matplotlib from options"""
214209 import matplotlib
215210 matplotlib .use (self .get ('execution' , 'matplotlib_backend' ))
216211
217212 def enable_provenance (self ):
213+ """Sets provenance storing on"""
218214 self ._config .set ('execution' , 'write_provenance' , 'true' )
219215 self ._config .set ('execution' , 'hash_method' , 'content' )
220216
221- < << << << HEAD
217+ @property
218+ def resource_monitor (self ):
219+ """Check if resource_monitor is available"""
220+ if self ._resource_monitor is not None :
221+ return self ._resource_monitor
222+
223+ # Cache config from nipype config
224+ self .resource_monitor = self ._config .get (
225+ 'execution' , 'resource_monitor' ) or False
226+ return self ._resource_monitor
227+
228+ @resource_monitor .setter
229+ def resource_monitor (self , value ):
230+ # Accept string true/false values
231+ if isinstance (value , (str , bytes )):
232+ value = str2bool (value .lower ())
233+
234+ if value is False :
235+ self ._resource_monitor = False
236+ elif value is True :
237+ if not self ._resource_monitor :
238+ # Before setting self._resource_monitor check psutil availability
239+ self ._resource_monitor = False
240+ try :
241+ import psutil
242+ self ._resource_monitor = LooseVersion (
243+ psutil .__version__ ) >= LooseVersion ('5.0' )
244+ except ImportError :
245+ pass
246+ finally :
247+ if not self ._resource_monitor :
248+ warn ('Could not enable the resource monitor: psutil>=5.0'
249+ ' could not be imported.' )
250+ self ._config .set ('execution' , 'resource_monitor' ,
251+ ('%s' % self ._resource_monitor ).lower ())
252+
253+ def enable_resource_monitor (self ):
254+ """Sets the resource monitor on"""
255+ self .resource_monitor = True
256+
222257 def get_display (self ):
223258 """Returns the first display available"""
224259
@@ -277,47 +312,8 @@ def stop_display(self):
277312
278313@atexit .register
279314def free_display ():
315+ """Stop virtual display (if it is up)"""
280316 from nipype import config
281317 from nipype import logging
282318 config .stop_display ()
283319 logging .getLogger ('interface' ).info ('Closing display (if virtual)' )
284- == == == =
285- @property
286- def resource_monitor (self ):
287- """Check if resource_monitor is available"""
288- if self ._resource_monitor is not None :
289- return self ._resource_monitor
290-
291- # Cache config from nipype config
292- self .resource_monitor = self ._config .get (
293- 'execution' , 'resource_monitor' ) or False
294- return self ._resource_monitor
295-
296- @resource_monitor .setter
297- def resource_monitor (self , value ):
298- # Accept string true/false values
299- if isinstance (value , (str , bytes )):
300- value = str2bool (value .lower ())
301-
302- if value is False :
303- self ._resource_monitor = False
304- elif value is True :
305- if not self ._resource_monitor :
306- # Before setting self._resource_monitor check psutil availability
307- self ._resource_monitor = False
308- try :
309- import psutil
310- self ._resource_monitor = LooseVersion (
311- psutil .__version__ ) >= LooseVersion ('5.0' )
312- except ImportError :
313- pass
314- finally :
315- if not self ._resource_monitor :
316- warn ('Could not enable the resource monitor: psutil>=5.0'
317- ' could not be imported.' )
318- self ._config .set ('execution' , 'resource_monitor' ,
319- ('%s' % self ._resource_monitor ).lower ())
320-
321- def enable_resource_monitor (self ):
322- self .resource_monitor = True
323- >> >> >> > upstream / master
0 commit comments