28
28
29
29
30
30
configdir = get_configdir ()
31
- if configdir is None :
32
- raise RuntimeError ('Could not find a suitable configuration directory' )
33
- cachedir = os .path .join (configdir , 'finance.cache' )
31
+ # cachedir will be None if there is no writable directory.
32
+ if configdir is not None :
33
+ cachedir = os .path .join (configdir , 'finance.cache' )
34
+ else :
35
+ # Should only happen in a restricted environment (such as Google App
36
+ # Engine). Deal with this gracefully by not caching finance data.
37
+ cachedir = None
34
38
35
39
36
40
stock_dt = np .dtype ([('date' , object ),
@@ -180,20 +184,24 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False)
180
184
d2 [0 ], d2 [1 ], d2 [2 ], ticker , g )
181
185
182
186
183
- if cachename is None :
184
- cachename = os .path .join (cachedir , md5 (url ).hexdigest ())
185
- if os .path .exists (cachename ):
186
- fh = open (cachename )
187
- verbose .report ('Using cachefile %s for %s' % (cachename , ticker ))
187
+ # Cache the finance data if there is a writable cache directory.
188
+ if cachedir is not None :
189
+ if cachename is None :
190
+ cachename = os .path .join (cachedir , md5 (url ).hexdigest ())
191
+ if os .path .exists (cachename ):
192
+ fh = open (cachename )
193
+ verbose .report ('Using cachefile %s for %s' % (cachename , ticker ))
194
+ else :
195
+ mkdirs (os .path .abspath (os .path .dirname (cachename )))
196
+ with contextlib .closing (urlopen (url )) as urlfh :
197
+ with open (cachename , 'wb' ) as fh :
198
+ fh .write (urlfh .read ())
199
+ verbose .report ('Saved %s data to cache file %s' % (ticker , cachename ))
200
+ fh = open (cachename , 'r' )
201
+
202
+ return fh
188
203
else :
189
- mkdirs (os .path .abspath (os .path .dirname (cachename )))
190
- with contextlib .closing (urlopen (url )) as urlfh :
191
- with open (cachename , 'wb' ) as fh :
192
- fh .write (urlfh .read ())
193
- verbose .report ('Saved %s data to cache file %s' % (ticker , cachename ))
194
- fh = open (cachename , 'r' )
195
-
196
- return fh
204
+ return urlopen (url )
197
205
198
206
199
207
def quotes_historical_yahoo (ticker , date1 , date2 , asobject = False ,
0 commit comments