import re


def get_output_path(manifest, regex):
    """
    lists status.nodes in an argo manifest, and grabs intermediary output files paths using the node tree represented by
    status.nodes[*].name. Keeps only nodes of type 'Pod' and phase 'succeeded'.

    Parameters
    ----------
    manifest : dict
    regex : str
        regular expression syntax str to filter nodes based on which templates were executed within a given node and before that given
        node in the tree.
    Returns
    ------
    dict:
        path : str, the path to the intermediary output file
        nodeId: the id of the manifest node that outputted this file
    """
    out_zarr_path = None
    nodeId = None
    i = 0

    for node in manifest['status']['nodes']:
        this_node = manifest['status']['nodes'][node]
        if this_node['type'] == 'Pod' and this_node['phase'] == 'Succeeded' and re.search(regex, this_node['name']):
            i = i + 1
            if i > 1:
                raise Exception('I could not identify a unique node in the manifest for regex : ' + regex + '\n' +
                                '. Id of the first match : ' + nodeId + '\n' + 'Id of second match : ' + this_node['id'])
            nodeId = this_node['id']
            if 'outputs' in this_node and 'parameters' in this_node['outputs']:
                for param in this_node['outputs']['parameters']:
                    if param['name'] == 'out-zarr':
                        out_zarr_path = param['value']

    if out_zarr_path is None and nodeId is None:
        raise Exception('I could not identify any node in the manifest')

    return ({'path': out_zarr_path, 'nodeId': nodeId})