from pathlib import Path
import importlib


def rtb_path_to_datafile(*filename, local=True):
    """
    Get absolute path to datafile

    :param filename: pathname of datafile
    :type filename: str
    :param local: search for file locally first, default True
    :type local: bool
    :raises FileNotFoundError: File does not exist
    :return: Absolute path
    :rtype: Path

    The positional arguments are joined, like ``os.path.join``.

    If ``local`` is True then ``~`` is expanded and if the file exists, the
    path is made absolute, and symlinks resolved.

    Otherwise, the file is sought within the ``rtbdata`` package and if found,
    return that absolute path.

    Example::

        loadmat('data/map1.mat')   # read rtbdata/data/map1.mat
        loadmat('foo.dat')         # read ./foo.dat
        loadmat('~/foo.dat')       # read $HOME/foo.dat
    """

    filename = Path(*filename)

    if local:
        # check if file is in user's local filesystem

        p = filename.expanduser()
        p = p.resolve()
        if p.exists():
            return p

    # otherwise, look for it in rtbdata

    rtbdata = importlib.import_module("rtbdata")
    root = Path(rtbdata.__path__[0])
    
    path = root / filename
    if path.exists():
        return path.resolve()
    else:
        raise ValueError(f"file {filename} not found locally or in rtbdata")