142142"""
143143from __future__ import generators
144144
145- __version__ = '0.82 '
145+ __version__ = '0.83a '
146146__revision__ = '$Revision$'
147147__date__ = '$Date$'
148148
159159
160160 - environment variable MATPLOTLIBRC
161161
162- - HOME/.matplotlibrc if HOME is defined
162+ - HOME/.matplotlib/ matplotlibrc if HOME is defined
163163
164- - PATH/. matplotlibrc where PATH is the return value of
164+ - PATH/matplotlibrc where PATH is the return value of
165165 get_data_path()
166166"""
167167
168- import sys , os
168+ import sys , os , tempfile
169169
170170
171171major , minor1 , minor2 , s , tmp = sys .version_info
@@ -186,6 +186,21 @@ def enumerate(seq):
186186 yield i , seq [i ]
187187
188188
189+
190+ def _is_writable_dir (p ):
191+ """
192+ p is a string pointing to a putative writable dir -- return True p
193+ is such a string, else False
194+ """
195+ try : p + '' # test is string like
196+ except TypeError : return False
197+ try :
198+ t = tempfile .TemporaryFile (dir = p )
199+ t .write ('1' )
200+ t .close ()
201+ except OSError : return False
202+ else : return True
203+
189204class Verbose :
190205 """
191206 A class to handle reporting. Set the fileo attribute to any file
@@ -259,23 +274,65 @@ def ge(self, level):
259274
260275verbose = Verbose ('silent' )
261276
262- def get_home ():
277+
278+ def _get_home ():
279+ """Return the closest possible equivalent to a 'home' directory.
280+
281+ We first try $HOME. Absent that, on NT it's USERPROFILE if
282+ defined, else, $HOMEDRIVE\$HOMEPATH, else C:\
283+
263284 """
264- return the users HOME dir across platforms or None.
265-
266- On win32, if either HOME is not set or HOME is set but doesn't
267- exist, the value of USERPROFILE will be used instead.
285+
286+
287+ try : return os .environ ['HOME' ]
288+ except KeyError : pass
289+
290+ if os .name == 'nt' :
291+ # For some strange reason, win9x returns 'nt' for os.name.
292+
293+ try : p = os .environ ['USERPROFILE' ]
294+ except KeyError : pass
295+ else :
296+ if os .path .exists (p ): return p
297+
298+ try : p = os .path .join (os .environ ['HOMEDRIVE' ],os .environ ['HOMEPATH' ])
299+ except KeyError : pass
300+ else :
301+ if os .path .exists (p ): return p
302+
303+ try :
304+ import _winreg as wreg
305+ key = wreg .OpenKey (wreg .HKEY_CURRENT_USER ,
306+ 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders' )
307+ p = wreg .QueryValueEx (key ,'Personal' )[0 ]
308+ key .Close ()
309+ if os .path .exists (p ): return p
310+ except : pass
311+ return 'C:\\ '
312+
313+ raise RuntimeError ('please define environment variable $HOME' )
314+
315+
316+
317+
318+ get_home = verbose .wrap ('$HOME=%s' , _get_home , always = False )
319+
320+ def _get_configdir ():
268321 """
322+ Return the string representing the configuration dir. If s is the
323+ special string _default_, use HOME/.matplotlib. s must be writable
324+ """
325+ h = get_home ()
326+ if not _is_writable_dir (h ):
327+ raise RuntimeError ("'%s' is not a writable dir; you must set environment variable HOME to be a writable dir " % h )
269328
270- if os .environ .has_key ('HOME' ):
271- path = os .environ ['HOME' ]
272- if os .path .exists (path ): return path
329+ p = os .path .join (get_home (), '.matplotlib' )
273330
274- if sys .platform == 'win32' and os .environ .has_key ('USERPROFILE' ):
275- path = os .environ ['USERPROFILE' ]
276- if os .path .exists (path ): return path
331+ if not _is_writable_dir (p ):
332+ os .mkdir (p )
277333
278- return None
334+ return p
335+ get_configdir = verbose .wrap ('$HOME=%s' , _get_configdir , always = False )
279336
280337
281338def _get_data_path ():
@@ -320,6 +377,7 @@ def _get_data_path():
320377get_data_path = verbose .wrap ('matplotlib data path %s' , _get_data_path , always = False )
321378
322379
380+
323381def validate_path_exists (s ):
324382 'If s is a path, return s, else False'
325383 if os .path .exists (s ): return s
@@ -594,7 +652,7 @@ def __call__(self, s):
594652
595653 'grid.color' : ['k' , validate_color ], # grid color
596654 'grid.linestyle' : [':' , str ], # dotted
597- 'grid.linewidth' : [' 0.5' , validate_float ], # in points
655+ 'grid.linewidth' : [0.5 , validate_float ], # in points
598656
599657
600658 # figure props
@@ -628,17 +686,9 @@ def __call__(self, s):
628686 }
629687
630688
631- def matplotlib_fname ():
689+ def _old_matplotlib_fname ():
632690 """
633- Return the path to the rc file
634-
635- Search order:
636-
637- * current working dir
638- * environ var MATPLOTLIBRC
639- * HOME/.matplotlibrc
640- * MATPLOTLIBDATA/.matplotlibrc
641-
691+ Return the path to the rc file using the old search method
642692 """
643693
644694 fname = os .path .join ( os .getcwd (), '.matplotlibrc' )
@@ -663,6 +713,60 @@ def matplotlib_fname():
663713 warnings .warn ('Could not find .matplotlibrc; using defaults' )
664714 return fname
665715
716+ def matplotlib_fname ():
717+ """
718+ Return the path to the rc file
719+
720+ Search order:
721+
722+ * current working dir
723+ * environ var MATPLOTLIBRC
724+ * HOME/.matplotlib/matplotlibrc
725+ * MATPLOTLIBDATA/matplotlibrc
726+
727+
728+ """
729+
730+ oldname = os .path .join ( os .getcwd (), '.matplotlibrc' )
731+ if os .path .exists (oldname ):
732+ print >> sys .stderr , """\
733+ WARNING: Old rc filename ".matplotlibrc" found in working dir
734+ and and renamed to new default rc file name "matplotlibrc"
735+ (no leading"dot"). """
736+ os .rename ('.matplotlibrc' , 'matplotlibrc' )
737+
738+ home = get_home ()
739+ oldname = os .path .join ( home , '.matplotlibrc' )
740+ if os .path .exists (oldname ):
741+ configdir = get_configdir ()
742+ newname = os .path .join (configdir , 'matplotlibrc' )
743+ print >> sys .stderr , """\
744+ WARNING: Old rc filename "%s" found and renamed to
745+ new default rc file name "%s".""" % (oldname , newname )
746+
747+ os .rename (oldname , newname )
748+
749+
750+ fname = os .path .join ( os .getcwd (), 'matplotlibrc' )
751+ if os .path .exists (fname ): return fname
752+
753+ if os .environ .has_key ('MATPLOTLIBRC' ):
754+ path = os .environ ['MATPLOTLIBRC' ]
755+ if os .path .exists (path ):
756+ fname = os .path .join (path , 'matplotlibrc' )
757+ if os .path .exists (fname ):
758+ return fname
759+
760+ fname = os .path .join (get_configdir (), 'matplotlibrc' )
761+ if os .path .exists (fname ): return fname
762+
763+
764+ path = get_data_path () # guaranteed to exist or raise
765+ fname = os .path .join (path , 'matplotlibrc' )
766+ if not os .path .exists (fname ):
767+ warnings .warn ('Could not find matplotlibrc; using defaults' )
768+ return fname
769+
666770
667771
668772def rc_params ():
@@ -699,7 +803,7 @@ def rc_params():
699803 key = key .strip ()
700804 if key in deprecated_map .keys ():
701805 alt = deprecated_map [key ]
702- warnings .warn ('%s is deprecated in . matplotlibrc - use %s instead.' % (key , alt ))
806+ warnings .warn ('%s is deprecated in matplotlibrc - use %s instead.' % (key , alt ))
703807 key = alt
704808
705809 if not defaultParams .has_key (key ):
@@ -809,6 +913,12 @@ def rcdefaults():
809913 rcParams .update (rcParamsDefault )
810914
811915
916+
917+
918+
919+
920+
921+
812922
813923# Now allow command line to override
814924
0 commit comments