From: <jd...@us...> - 2009-08-05 15:07:48
|
Revision: 7370 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7370&view=rev Author: jdh2358 Date: 2009-08-05 15:07:41 +0000 (Wed, 05 Aug 2009) Log Message: ----------- add support for nested dirs in sample_data Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/cbook.py Modified: trunk/matplotlib/lib/matplotlib/cbook.py =================================================================== --- trunk/matplotlib/lib/matplotlib/cbook.py 2009-08-05 15:02:12 UTC (rev 7369) +++ trunk/matplotlib/lib/matplotlib/cbook.py 2009-08-05 15:07:41 UTC (rev 7370) @@ -356,8 +356,14 @@ self.remove_stale_files() def in_cache_dir(self, fn): - return os.path.join(self.cache_dir, fn) + # make sure the datadir exists + reldir, filename = os.path.split(fn) + datadir = os.path.join(self.cache_dir, reldir) + if not os.path.exists(datadir): + os.makedirs(datadir) + return os.path.join(datadir, filename) + def read_cache(self): """ Read the cache file from the cache directory. @@ -386,7 +392,11 @@ listed = set([fn for (_, (fn, _, _)) in self.cache.items()]) for path in os.listdir(self.cache_dir): if path not in listed and path != 'cache.pck': - os.remove(os.path.join(self.cache_dir, path)) + thisfile = os.path.join(self.cache_dir, path) + if not os.path.isdir(thisfile): + matplotlib.verbose.report('_CacheProcessor:remove_stale_files: removing %s'%thisfile, + level='debug') + os.remove(thisfile) def write_cache(self): """ @@ -402,12 +412,14 @@ Store a received file in the cache directory. """ # Pick a filename - rightmost = url.rstrip('/').split('/')[-1] - fn = rightmost - while os.path.exists(self.in_cache_dir(fn)): - fn = rightmost + '.' + str(random.randint(0,9999999)) + fn = url[len(self.baseurl):] + fullpath = self.in_cache_dir(fn) - # Write out the data + #while os.path.exists(self.in_cache_dir(fn)): + # fn = rightmost + '.' + str(random.randint(0,9999999)) + + + f = open(self.in_cache_dir(fn), 'wb') f.write(data) f.close() @@ -438,7 +450,9 @@ """ url = req.get_full_url() fn, _, _ = self.cache[url] - file = open(self.in_cache_dir(fn), 'rb') + cachefile = self.in_cache_dir(fn) + matplotlib.verbose.report('_CacheProcessor: reading data file from cache file "%s"'%cachefile) + file = open(cachefile, 'rb') handle = urllib2.addinfourl(file, hdrs, url) handle.code = 304 return handle @@ -480,13 +494,14 @@ intended for use in mpl examples that need custom data """ + baseurl ='http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/sample_data/' if not hasattr(get_sample_data, 'opener'): configdir = matplotlib.get_configdir() cachedir = os.path.join(configdir, 'sample_data') if not os.path.exists(cachedir): os.mkdir(cachedir) # Store the cache processor and url opener as attributes of this function - get_sample_data.processor = _CacheProcessor(cachedir) + get_sample_data.processor = _CacheProcessor(cachedir, baseurl) get_sample_data.opener = urllib2.build_opener(get_sample_data.processor) @@ -497,8 +512,7 @@ import urllib quote = urllib.quote - url = 'http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/sample_data/' + \ - quote(fname) + url = baseurl + quote(fname) response = get_sample_data.opener.open(url) if asfileobj: return response This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |