from typing import Optional
from pathlib import Path


def _try_touch_file(target) -> Optional[Path]:
    """Test to see if we have permissions to create a file at ``target``.
    
    If the target already exists, it will not be touched.  If it does not
    exist, this function attempts to create it and delete it (i.e. testing
    permissions).  NOTE: all parent directories required to write the file will
    be created, but NOT deleted.  
    
    If successful, the path is returned, if not, return None.
 
    Parameters
    ----------
    target : str
        Filepath to test
    
    Returns
    -------
    target : str or None
        Returns the target if it is writeable, returns None if it is not.
    """
    target = Path(target)
    if not target.exists():
        try:
            # create parent directories
            target.parent.mkdir(parents=True, exist_ok=True)
            target.touch()  # create the file itself
        except Exception:
            return None
        target.unlink()  # delete it
    return target