File tree Expand file tree Collapse file tree 2 files changed +27
-25
lines changed
Expand file tree Collapse file tree 2 files changed +27
-25
lines changed Original file line number Diff line number Diff line change 11import codecs
22
3- def openSource (source ):
4- """ Opens source first trying to open a local file, if that fails
5- try to open as a URL and finally treating source as a string.
6-
7- Returns a file-like object.
8- """
9- # Already a file-like object?
10- if hasattr (source , 'tell' ):
11- return source
12-
13- # Try opening source normally
14- try :
15- return open (source )
16- except : pass
17-
18- # Try opening source as a URL and storing the bytes returned so
19- # they can be turned into a file-like object below
20- try :
21- import urllib
22- source = urllib .urlopen (source ).read (- 1 )
23- except : pass
24-
25- # Treat source as a string and make it into a file-like object
26- import cStringIO as StringIO
27- return StringIO .StringIO (str (source ))
3+ from utils .utils import openSource
284
295class HTMLInputStream (object ):
306 """For reading data from an input stream
Original file line number Diff line number Diff line change @@ -33,3 +33,29 @@ def __getitem__(self, key):
3333 return self .defaultValue
3434 else :
3535 raise
36+
37+ def openSource (source ):
38+ """ Opens source first trying to open a local file, if that fails
39+ try to open as a URL and finally treating source as a string.
40+
41+ Returns a file-like object.
42+ """
43+ # Already a file-like object?
44+ if hasattr (source , 'tell' ):
45+ return source
46+
47+ # Try opening source normally
48+ try :
49+ return open (source )
50+ except : pass
51+
52+ # Try opening source as a URL and storing the bytes returned so
53+ # they can be turned into a file-like object below
54+ try :
55+ import urllib
56+ source = urllib .urlopen (source ).read (- 1 )
57+ except : pass
58+
59+ # Treat source as a string and make it into a file-like object
60+ import cStringIO as StringIO
61+ return StringIO .StringIO (str (source ))
You can’t perform that action at this time.
0 commit comments