Skip to content

Commit a1a2ab4

Browse files
author
lantis63
committed
Moved openSource function into utils file so it can be used elsewhere if needed
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%4085
1 parent 4404dba commit a1a2ab4

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

inputstream.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import 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

295
class HTMLInputStream(object):
306
"""For reading data from an input stream

utils/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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))

0 commit comments

Comments
 (0)