from typing import Union
from pathlib import Path
from typing import Tuple
from typing import Optional


def _find_user_code(entry_point: Union[str, Path]) -> Tuple[Path, Optional[str], Optional[str]]:
    """Generate a (path, package, method) triple from the given entry point.

    Args:
        entry_point (Union[str, Path]): the entry point to execute

    Raises:
        ValueError: If the entry point could not be parsed

    Returns:
        Tuple[Path, Optional[str], Optional[str]]: the (path, package, method) triple
    """
    if isinstance(entry_point, Path):
        if not entry_point.is_file():
            raise ValueError("If entry pint is a Path object it must point to a file!")
        path = entry_point.parent
        package = entry_point.name
        if package.endswith(".py"):
            package = package[:-3]
        return path, package, None
    
    *path, rest = entry_point.replace("\\", "/").split("/")
    if not rest:
        raise ValueError("Cannot parse entry point '{}'".format(entry_point))
    if rest.count(':') > 1:
        raise ValueError("Cannot parse entry point '{}'".format(entry_point))
    
    package, *method_list = rest.split(":", maxsplit=1)

    method = method_list[0] if method_list else None
 
    if package.endswith('.py'):
        package = package[:-3]
    return Path('/'.join(path)), package, method