Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c1956f

Browse files
committed
code reorganization to remove active call to 'get_data_path' from otherwise static list of rcdefault values
svn path=/trunk/matplotlib/; revision=3430
1 parent 4387660 commit 0c1956f

File tree

1 file changed

+113
-102
lines changed

1 file changed

+113
-102
lines changed

lib/matplotlib/__init__.py

Lines changed: 113 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -309,107 +309,6 @@ def ge(self, level):
309309

310310
verbose=Verbose()
311311

312-
def _get_home():
313-
"""Find user's home directory if possible.
314-
Otherwise raise error.
315-
316-
:see: http://mail.python.org/pipermail/python-list/2005-February/263921.html
317-
"""
318-
path=''
319-
try:
320-
path=os.path.expanduser("~")
321-
except:
322-
pass
323-
if not os.path.isdir(path):
324-
for evar in ('HOME', 'USERPROFILE', 'TMP'):
325-
try:
326-
path = os.environ[evar]
327-
if os.path.isdir(path):
328-
break
329-
except: pass
330-
if path:
331-
return path
332-
else:
333-
raise RuntimeError('please define environment variable $HOME')
334-
335-
336-
337-
get_home = verbose.wrap('$HOME=%s', _get_home, always=False)
338-
339-
def _get_configdir():
340-
"""
341-
Return the string representing the configuration dir.
342-
343-
default is HOME/.matplotlib. you can override this with the
344-
MPLCONFIGDIR environment variable
345-
"""
346-
347-
configdir = os.environ.get('MPLCONFIGDIR')
348-
if configdir is not None:
349-
if not _is_writable_dir(configdir):
350-
raise RuntimeError('Could not write to MPLCONFIGDIR="%s"'%configdir)
351-
return configdir
352-
353-
h = get_home()
354-
p = os.path.join(get_home(), '.matplotlib')
355-
356-
if os.path.exists(p):
357-
if not _is_writable_dir(p):
358-
raise RuntimeError("'%s' is not a writable dir; you must set %s/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored "%h)
359-
else:
360-
if not _is_writable_dir(h):
361-
raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
362-
363-
os.mkdir(p)
364-
365-
return p
366-
get_configdir = verbose.wrap('CONFIGDIR=%s', _get_configdir, always=False)
367-
368-
369-
def _get_data_path():
370-
'get the path to matplotlib data'
371-
372-
if os.environ.has_key('MATPLOTLIBDATA'):
373-
path = os.environ['MATPLOTLIBDATA']
374-
if os.path.isdir(path): return path
375-
376-
path = os.sep.join([os.path.dirname(__file__), 'mpl-data'])
377-
if os.path.isdir(path): return path
378-
379-
# setuptools' namespace_packages may highjack this init file
380-
# so need to try something known to be in matplotlib, not basemap
381-
import matplotlib.afm
382-
path = os.sep.join([os.path.dirname(matplotlib.afm.__file__), 'mpl-data'])
383-
if os.path.isdir(path): return path
384-
385-
# py2exe zips pure python, so still need special check
386-
if getattr(sys,'frozen',None):
387-
path = os.path.join(os.path.split(sys.path[0])[0], 'matplotlibdata')
388-
if os.path.isdir(path): return path
389-
else:
390-
# Try again assuming sys.path[0] is a dir not a exe
391-
path = os.path.join(sys.path[0], 'matplotlibdata')
392-
if os.path.isdir(path): return path
393-
394-
raise RuntimeError('Could not find the matplotlib data files')
395-
396-
get_data_path = verbose.wrap('matplotlib data path %s', _get_data_path, always=False)
397-
398-
def get_py2exe_datafiles():
399-
datapath = get_data_path()
400-
head, tail = os.path.split(datapath)
401-
d = {}
402-
for root, dirs, files in os.walk(datapath):
403-
# Need to explicitly remove cocoa_agg files or py2exe complains
404-
# NOTE I dont know why, but do as previous version
405-
if 'Matplotlib.nib' in files:
406-
files.remove('Matplotlib.nib')
407-
files = [os.path.join(root, filename) for filename in files]
408-
root = root.replace(tail, 'matplotlibdata')
409-
root = root[root.index('matplotlibdata'):]
410-
d[root] = files
411-
return d.items()
412-
413312
def checkdep_dvipng():
414313
try:
415314
stdin, stdout = os.popen4('dvipng -version')
@@ -763,7 +662,7 @@ def __call__(self, s):
763662
'numerix' : ['numpy', validate_numerix],
764663
'maskedarray' : [False, validate_bool],
765664
'toolbar' : ['toolbar2', validate_toolbar],
766-
'datapath' : [get_data_path(), validate_path_exists],
665+
'datapath' : [None, validate_path_exists], # handled by _get_data_path_cached
767666
'units' : [False, validate_bool],
768667
'interactive' : [False, validate_bool],
769668
'timezone' : ['UTC', str],
@@ -927,6 +826,115 @@ def __call__(self, s):
927826
}
928827

929828

829+
def _get_home():
830+
"""Find user's home directory if possible.
831+
Otherwise raise error.
832+
833+
:see: http://mail.python.org/pipermail/python-list/2005-February/263921.html
834+
"""
835+
path=''
836+
try:
837+
path=os.path.expanduser("~")
838+
except:
839+
pass
840+
if not os.path.isdir(path):
841+
for evar in ('HOME', 'USERPROFILE', 'TMP'):
842+
try:
843+
path = os.environ[evar]
844+
if os.path.isdir(path):
845+
break
846+
except: pass
847+
if path:
848+
return path
849+
else:
850+
raise RuntimeError('please define environment variable $HOME')
851+
852+
853+
854+
get_home = verbose.wrap('$HOME=%s', _get_home, always=False)
855+
856+
def _get_configdir():
857+
"""
858+
Return the string representing the configuration dir.
859+
860+
default is HOME/.matplotlib. you can override this with the
861+
MPLCONFIGDIR environment variable
862+
"""
863+
864+
configdir = os.environ.get('MPLCONFIGDIR')
865+
if configdir is not None:
866+
if not _is_writable_dir(configdir):
867+
raise RuntimeError('Could not write to MPLCONFIGDIR="%s"'%configdir)
868+
return configdir
869+
870+
h = get_home()
871+
p = os.path.join(get_home(), '.matplotlib')
872+
873+
if os.path.exists(p):
874+
if not _is_writable_dir(p):
875+
raise RuntimeError("'%s' is not a writable dir; you must set %s/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored "%h)
876+
else:
877+
if not _is_writable_dir(h):
878+
raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
879+
880+
os.mkdir(p)
881+
882+
return p
883+
get_configdir = verbose.wrap('CONFIGDIR=%s', _get_configdir, always=False)
884+
885+
886+
def _get_data_path():
887+
'get the path to matplotlib data'
888+
889+
if os.environ.has_key('MATPLOTLIBDATA'):
890+
path = os.environ['MATPLOTLIBDATA']
891+
if not os.path.isdir(path):
892+
raise RuntimeError('Path in environment MATPLOTLIBDATA not a directory')
893+
return path
894+
895+
path = os.sep.join([os.path.dirname(__file__), 'mpl-data'])
896+
if os.path.isdir(path): return path
897+
898+
# setuptools' namespace_packages may highjack this init file
899+
# so need to try something known to be in matplotlib, not basemap
900+
import matplotlib.afm
901+
path = os.sep.join([os.path.dirname(matplotlib.afm.__file__), 'mpl-data'])
902+
if os.path.isdir(path): return path
903+
904+
# py2exe zips pure python, so still need special check
905+
if getattr(sys,'frozen',None):
906+
path = os.path.join(os.path.split(sys.path[0])[0], 'matplotlibdata')
907+
if os.path.isdir(path): return path
908+
else:
909+
# Try again assuming sys.path[0] is a dir not a exe
910+
path = os.path.join(sys.path[0], 'matplotlibdata')
911+
if os.path.isdir(path): return path
912+
913+
raise RuntimeError('Could not find the matplotlib data files')
914+
915+
def _get_data_path_cached():
916+
if defaultParams['datapath'][0] is None:
917+
defaultParams['datapath'][0] = _get_data_path()
918+
return defaultParams['datapath'][0]
919+
920+
get_data_path = verbose.wrap('matplotlib data path %s', _get_data_path_cached, always=False)
921+
922+
def get_py2exe_datafiles():
923+
datapath = get_data_path()
924+
head, tail = os.path.split(datapath)
925+
d = {}
926+
for root, dirs, files in os.walk(datapath):
927+
# Need to explicitly remove cocoa_agg files or py2exe complains
928+
# NOTE I dont know why, but do as previous version
929+
if 'Matplotlib.nib' in files:
930+
files.remove('Matplotlib.nib')
931+
files = [os.path.join(root, filename) for filename in files]
932+
root = root.replace(tail, 'matplotlibdata')
933+
root = root[root.index('matplotlibdata'):]
934+
d[root] = files
935+
return d.items()
936+
937+
930938
def matplotlib_fname():
931939
"""
932940
Return the path to the rc file
@@ -1063,6 +1071,9 @@ def rc_params(fail_on_error=False):
10631071
if cval is not None:
10641072
ret[key] = cval
10651073

1074+
if ret['datapath'] is None:
1075+
ret['datapath'] = get_data_path()
1076+
10661077
verbose.report('loaded rc file %s'%fname)
10671078

10681079
return ret

0 commit comments

Comments
 (0)