@contextlib.contextmanager<EOL>def smart_open_read(path=None, mode='<STR_LIT:rb>', encoding=None, try_gzip=False):
assert mode in ('<STR_LIT:r>', '<STR_LIT:rb>')<EOL>assert path is None or isinstance(path, (str, _oldstr))<EOL>assert isinstance(mode, (str, _oldstr))<EOL>assert encoding is None or isinstance(encoding, (str, _oldstr))<EOL>assert isinstance(try_gzip, bool)<EOL>fh = None<EOL>binfh = None<EOL>gzfh = None<EOL>if path is None:<EOL><INDENT>fh = io.open(sys.stdin.fileno(), mode=mode, encoding=encoding)<EOL><DEDENT>else:<EOL><INDENT>if try_gzip:<EOL><INDENT>gzfh = try_open_gzip(path)<EOL><DEDENT>if gzfh is not None:<EOL><INDENT>logger.debug('<STR_LIT>''<STR_LIT>')<EOL>binfh = io.BufferedReader(gzfh)<EOL>if '<STR_LIT:b>' not in mode:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>fh = io.TextIOWrapper(binfh, encoding=encoding)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fh = io.open(path, mode=mode, encoding=encoding)<EOL><DEDENT><DEDENT>yield_fh = fh<EOL>if fh is None:<EOL><INDENT>yield_fh = binfh<EOL><DEDENT>try:<EOL><INDENT>yield yield_fh<EOL><DEDENT>finally:<EOL><INDENT>if fh is not None:<EOL><INDENT>if fh.fileno() != sys.stdin.fileno():<EOL><INDENT>fh.close()<EOL><DEDENT><DEDENT>if binfh is not None:<EOL><INDENT>binfh.close()<EOL><DEDENT>if gzfh is not None:<EOL><INDENT>gzfh.close()<EOL><DEDENT><DEDENT>
Open a file for reading or return ``stdin``.

    Adapted from StackOverflow user "Wolph"
    (http://stackoverflow.com/a/17603000).