import inspect
from pathlib import Path


def _get_task_metadata(node):
    """Get a dictionary of task metadata: 'code', 'filepath' and 'docstring'.
    For 'filepath', remove the path to the project from the full code location
    before sending to JSON.

    Example:
        'code_full_path':   'path-to-project/project_root/path-to-code/node.py'
        'Path.cwd().parent':'path-to-project/'
        'filepath':    'project_root/path-to-code/node.py''

    """
    task_metadata = {"code": inspect.getsource(node["obj"]._func)}

    code_full_path = Path(inspect.getfile(node["obj"]._func)).expanduser().resolve()
    filepath = code_full_path.relative_to(Path.cwd().parent)
    task_metadata["filepath"] = str(filepath)

    docstring = inspect.getdoc(node["obj"]._func)
    if docstring:
        task_metadata["docstring"] = docstring

    if "parameters" in node:
        task_metadata["parameters"] = node["parameters"]

    return task_metadata