Discover and return the disk file path of the Python module named in
    `arg` by importing the module and returning its ``__file__`` attribute.

    If `find_source` is `True`, the named module is a ``pyc`` or ``pyo`` file,
    and a corresponding ``.py`` file exists on disk, the path to the ``.py``
    file is returned instead.

    If `show_directory` is `True`, the path to the directory containing the
    discovered module file is returned. Similarly, if `hide_init` is `True` and
    the named module is the ``__init__`` module of a package, the function
    returns the path to the package directory containing the ``__init__.py``
    filename.

    If `real_path` is `True` and the discovered module was loaded via symlink,
    the real path (as determined by `os.path.realpath()`) is returned.

    If the named module cannot be imported or its path on disk determined, this
    function raises a `pywhich.ModuleNotFound` exception.

def identify_filepath(arg, real_path=None, show_directory=None,
    find_source=None, hide_init=None):
    """Discover and return the disk file path of the Python module named in
    `arg` by importing the module and returning its ``__file__`` attribute.

    If `find_source` is `True`, the named module is a ``pyc`` or ``pyo`` file,
    and a corresponding ``.py`` file exists on disk, the path to the ``.py``
    file is returned instead.

    If `show_directory` is `True`, the path to the directory containing the
    discovered module file is returned. Similarly, if `hide_init` is `True` and
    the named module is the ``__init__`` module of a package, the function
    returns the path to the package directory containing the ``__init__.py``
    filename.

    If `real_path` is `True` and the discovered module was loaded via symlink,
    the real path (as determined by `os.path.realpath()`) is returned.

    If the named module cannot be imported or its path on disk determined, this
    function raises a `pywhich.ModuleNotFound` exception.

    """
    mod = identify_module(arg)  # raises ModuleNotFound
    try:
        filename = mod.__file__
    except AttributeError:
        raise ModuleNotFound("module has no '__file__' attribute; is it a "
            "built-in or C module?")

    if find_source and (filename.endswith('.pyc') or filename.endswith('.pyo')):
        log.debug("Filename ends in pyc or pyo, so looking for the .py file")
        sourcefile = filename[:-1]
        if os.access(sourcefile, os.F_OK):
            filename = sourcefile
        else:
            log.debug("Did not find .py file for path %r, using as-is",
                filename)

    if real_path:
        filename = os.path.realpath(filename)

    if show_directory or (hide_init and
        os.path.basename(filename).startswith('__init__.')):
        log.debug("Showing directories or hiding __init__s, so returning "
            "directory of %r", filename)
        filename = os.path.dirname(filename)

    return filename