from urllib.request import urlopen
import gzip


def tomso_open(filename, *args, **kwargs):
    """Wrapper function to open files ending with `.gz` with built-in
    `gzip` module or paths starting with `http` using
    `urllib.request.urlopen`, otherwise use normal open.  `.gz` and
    normal modes take the same arguments as `open` and `gzip.open` and
    return a file object."""
    if filename.startswith('http'):
        return urlopen(filename)
    elif filename.lower().endswith('.gz'):
        return gzip.open(filename, *args, **kwargs)
    else:
        return open(filename, *args, **kwargs)