Try to detect if an object is a file type on python python 2 & 3.
authorDiane Trout <diane@ghic.org>
Fri, 20 Mar 2015 23:22:06 +0000 (16:22 -0700)
committerDiane Trout <diane@ghic.org>
Fri, 20 Mar 2015 23:22:06 +0000 (16:22 -0700)
The base type for files changed between the two releases.

htsworkflow/util/opener.py

index 6d5e2c00246e2a7d2d53d131890d39e18d497d51..c599975da5ae2ee4457f83d0eecef665e551f9e6 100644 (file)
@@ -4,9 +4,16 @@ Helpful utilities for turning random names/objects into streams.
 import os
 import gzip
 import bz2
-import types
+import six
 from six.moves import urllib
 
+if six.PY2:
+    import types
+    FILE_CLASS = types.FileType
+else:
+    import io
+    FILE_CLASS = io.IOBase
+
 def isfilelike(file_ref, mode):
     """Does file_ref have the core file operations?
     """
@@ -41,7 +48,7 @@ def autoopen(file_ref, mode='r'):
     Attempt to intelligently turn file_ref into a readable stream
     """
     # catch being passed a file
-    if type(file_ref) is types.FileType:
+    if isinstance(file_ref, FILE_CLASS):
         return file_ref
     # does it look like a file?
     elif isfilelike(file_ref, mode):