From: Diane Trout Date: Fri, 20 Mar 2015 23:22:06 +0000 (-0700) Subject: Try to detect if an object is a file type on python python 2 & 3. X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=htsworkflow.git;a=commitdiff_plain;h=5ab0e75f657ddd5beb5a0ca527874930981a47bc Try to detect if an object is a file type on python python 2 & 3. The base type for files changed between the two releases. --- diff --git a/htsworkflow/util/opener.py b/htsworkflow/util/opener.py index 6d5e2c0..c599975 100644 --- a/htsworkflow/util/opener.py +++ b/htsworkflow/util/opener.py @@ -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):