142
142
"""
143
143
from __future__ import generators
144
144
145
- __version__ = '0.82 '
145
+ __version__ = '0.83a '
146
146
__revision__ = '$Revision$'
147
147
__date__ = '$Date$'
148
148
159
159
160
160
- environment variable MATPLOTLIBRC
161
161
162
- - HOME/.matplotlibrc if HOME is defined
162
+ - HOME/.matplotlib/ matplotlibrc if HOME is defined
163
163
164
- - PATH/. matplotlibrc where PATH is the return value of
164
+ - PATH/matplotlibrc where PATH is the return value of
165
165
get_data_path()
166
166
"""
167
167
168
- import sys , os
168
+ import sys , os , tempfile
169
169
170
170
171
171
major , minor1 , minor2 , s , tmp = sys .version_info
@@ -186,6 +186,21 @@ def enumerate(seq):
186
186
yield i , seq [i ]
187
187
188
188
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
+
189
204
class Verbose :
190
205
"""
191
206
A class to handle reporting. Set the fileo attribute to any file
@@ -259,23 +274,65 @@ def ge(self, level):
259
274
260
275
verbose = Verbose ('silent' )
261
276
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
+
263
284
"""
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 ():
268
321
"""
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 )
269
328
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' )
273
330
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 )
277
333
278
- return None
334
+ return p
335
+ get_configdir = verbose .wrap ('$HOME=%s' , _get_configdir , always = False )
279
336
280
337
281
338
def _get_data_path ():
@@ -320,6 +377,7 @@ def _get_data_path():
320
377
get_data_path = verbose .wrap ('matplotlib data path %s' , _get_data_path , always = False )
321
378
322
379
380
+
323
381
def validate_path_exists (s ):
324
382
'If s is a path, return s, else False'
325
383
if os .path .exists (s ): return s
@@ -594,7 +652,7 @@ def __call__(self, s):
594
652
595
653
'grid.color' : ['k' , validate_color ], # grid color
596
654
'grid.linestyle' : [':' , str ], # dotted
597
- 'grid.linewidth' : [' 0.5' , validate_float ], # in points
655
+ 'grid.linewidth' : [0.5 , validate_float ], # in points
598
656
599
657
600
658
# figure props
@@ -628,17 +686,9 @@ def __call__(self, s):
628
686
}
629
687
630
688
631
- def matplotlib_fname ():
689
+ def _old_matplotlib_fname ():
632
690
"""
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
642
692
"""
643
693
644
694
fname = os .path .join ( os .getcwd (), '.matplotlibrc' )
@@ -663,6 +713,60 @@ def matplotlib_fname():
663
713
warnings .warn ('Could not find .matplotlibrc; using defaults' )
664
714
return fname
665
715
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
+
666
770
667
771
668
772
def rc_params ():
@@ -699,7 +803,7 @@ def rc_params():
699
803
key = key .strip ()
700
804
if key in deprecated_map .keys ():
701
805
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 ))
703
807
key = alt
704
808
705
809
if not defaultParams .has_key (key ):
@@ -809,6 +913,12 @@ def rcdefaults():
809
913
rcParams .update (rcParamsDefault )
810
914
811
915
916
+
917
+
918
+
919
+
920
+
921
+
812
922
813
923
# Now allow command line to override
814
924
0 commit comments