def smart_open(uri, mode="rb", **kw):

    # validate mode parameter
    if not isinstance(mode, six.string_types):
        raise TypeError('mode should be a string')

    if isinstance(uri, six.string_types):
        # this method just routes the request to classes handling the specific storage
        # schemes, depending on the URI protocol in `uri`
        parsed_uri = ParseUri(uri)

        if parsed_uri.scheme in ("file", ):
            # local files -- both read & write supported
            # compression, if any, is determined by the filename extension (.gz, .bz2)
            return file_smart_open(parsed_uri.uri_path, mode)
        elif parsed_uri.scheme in ("s3", "s3n"):
            # Get an S3 host. It is required for sigv4 operations.
            host = kw.pop('host', None)
            if not host:
                host = boto.config.get('s3', 'host', 's3.amazonaws.com')

            # For credential order of precedence see
            # http://boto.cloudhackers.com/en/latest/boto_config_tut.html#credentials
            s3_connection = boto.connect_s3(
                aws_access_key_id=parsed_uri.access_id,
                host=host,
                aws_secret_access_key=parsed_uri.access_secret,
                profile_name=kw.pop('profile_name', None))

            bucket = s3_connection.get_bucket(parsed_uri.bucket_id)
            if mode in ('r', 'rb'):
                key = bucket.get_key(parsed_uri.key_id)
                if key is None:
                    raise KeyError(parsed_uri.key_id)
                return S3OpenRead(key, **kw)
            elif mode in ('w', 'wb'):
                key = bucket.get_key(parsed_uri.key_id, validate=False)
                if key is None:
                    raise KeyError(parsed_uri.key_id)
                return S3OpenWrite(key, **kw)
            else:
                raise NotImplementedError("file mode %s not supported for %r scheme", mode, parsed_uri.scheme)

        elif parsed_uri.scheme in ("hdfs", ):
            if mode in ('r', 'rb'):
                return HdfsOpenRead(parsed_uri, **kw)
            else:
                raise NotImplementedError("file mode %s not supported for %r scheme", mode, parsed_uri.scheme)
        elif parsed_uri.scheme in ("webhdfs", ):
            if mode in ('r', 'rb'):
                return WebHdfsOpenRead(parsed_uri, **kw)
            elif mode in ('w', 'wb'):
                return WebHdfsOpenWrite(parsed_uri, **kw)
            else:
                raise NotImplementedError("file mode %s not supported for %r scheme", mode, parsed_uri.scheme)
        else:
            raise NotImplementedError("scheme %r is not supported", parsed_uri.scheme)
    elif isinstance(uri, boto.s3.key.Key):
        # handle case where we are given an S3 key directly
        if mode in ('r', 'rb'):
            return S3OpenRead(uri)
        elif mode in ('w', 'wb'):
            return S3OpenWrite(uri)
    elif hasattr(uri, 'read'):
        # simply pass-through if already a file-like
        return uri
    else:
        raise TypeError('don\'t know how to handle uri %s' % repr(uri))
Open the given S3 / HDFS / filesystem file pointed to by `uri` for reading or writing.
dataset/ETHPy150Open piskvorky/smart_open/smart_open/smart_open_lib.py/smart_open