2 Helpful utilities for turning random names/objects into streams.
10 def isfilelike(file_ref, mode):
11 """Does file_ref have the core file operations?
13 # if mode is w/a check to make sure we writeable ops
14 # but always check to see if we can read
15 read_operations = ['read', 'readline', 'readlines']
16 write_operations = [ 'write', 'writelines' ]
17 #random_operations = [ 'seek', 'tell' ]
18 if mode[0] in ('w', 'a'):
19 for o in write_operations:
20 if not hasattr(file_ref, o):
22 for o in read_operations:
23 if not hasattr(file_ref, o):
28 def isurllike(file_ref, mode):
30 does file_ref look like a url?
31 (AKA does it start with protocol:// ?)
34 parsed = urllib2.urlparse.urlparse(file_ref)
35 schema, netloc, path, params, query, fragment = parsed
37 return len(schema) > 0
39 def autoopen(file_ref, mode='r'):
41 Attempt to intelligently turn file_ref into a readable stream
43 # catch being passed a file
44 if type(file_ref) is types.FileType:
46 # does it look like a file?
47 elif isfilelike(file_ref, mode):
49 elif isurllike(file_ref, mode):
50 return urllib2.urlopen(file_ref)
51 elif os.path.splitext(file_ref)[1] == ".gz":
52 return gzip.open(file_ref, mode)
53 elif os.path.splitext(file_ref)[1] == '.bz2':
54 return bz2.BZ2File(file_ref, mode)
56 return open(file_ref,mode)