|
10 | 10 | import numpy.ma as ma |
11 | 11 | from weakref import ref |
12 | 12 |
|
| 13 | +import matplotlib |
| 14 | + |
13 | 15 | major, minor1, minor2, s, tmp = sys.version_info |
14 | 16 |
|
15 | 17 |
|
@@ -338,6 +340,55 @@ def to_filehandle(fname, flag='rU', return_opened=False): |
338 | 340 | def is_scalar_or_string(val): |
339 | 341 | return is_string_like(val) or not iterable(val) |
340 | 342 |
|
| 343 | + |
| 344 | + |
| 345 | +def get_mpl_data(fname, asfileobj=True): |
| 346 | + """ |
| 347 | + Check the cachedirectory ~/.matplotlib/mpl_data for an mpl_data |
| 348 | + file. If it does not exist, fetch it with urllib from the mpl svn repo |
| 349 | +
|
| 350 | + http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/mpl_data/ |
| 351 | +
|
| 352 | + and store it in the cachedir. |
| 353 | +
|
| 354 | + If asfileobj is True, a file object will be returned. Else the |
| 355 | + path to the file as a string will be returned |
| 356 | +
|
| 357 | + To add a datafile to this directory, you need to check out |
| 358 | + mpl_data from matplotlib svn:: |
| 359 | +
|
| 360 | + svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/mpl_data |
| 361 | +
|
| 362 | + and svn add the data file you want to support. This is primarily |
| 363 | + intended for use in mpl examples that need custom data |
| 364 | + """ |
| 365 | + |
| 366 | + # TODO: how to handle stale data in the cache that has been |
| 367 | + # updated from svn -- is there a clean http way to get the current |
| 368 | + # revision number that will not leave us at the mercy of html |
| 369 | + # changes at sf? |
| 370 | + |
| 371 | + |
| 372 | + configdir = matplotlib.get_configdir() |
| 373 | + cachedir = os.path.join(configdir, 'mpl_data') |
| 374 | + if not os.path.exists(cachedir): |
| 375 | + os.mkdir(cachedir) |
| 376 | + |
| 377 | + cachefile = os.path.join(cachedir, fname) |
| 378 | + |
| 379 | + if not os.path.exists(cachefile): |
| 380 | + import urllib |
| 381 | + url = 'http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/mpl_data/%s'%urllib.quote(fname) |
| 382 | + matplotlib.verbose.report('Attempting to download %s to %s'%(url, cachefile)) |
| 383 | + urllib.urlretrieve(url, filename=cachefile) |
| 384 | + else: |
| 385 | + matplotlib.verbose.report('Aleady have mpl_data %s'%fname) |
| 386 | + |
| 387 | + if asfileobj: |
| 388 | + return to_filehandle(cachefile) |
| 389 | + else: |
| 390 | + return cachefile |
| 391 | + |
341 | 392 | def flatten(seq, scalarp=is_scalar_or_string): |
342 | 393 | """ |
343 | 394 | this generator flattens nested containers such as |
|
0 commit comments