human
Complete this python program:
def bundle_to_joblist(bundle, allow_debug=True):
assistant
#Here is the Python program you requested.

    """Convert the bundle hierarchy into a "job list".
    Each job represents one output url; often there will be only
    a single job, if nested bundles insist on not being merged
    into their parents, there may be multiple ones; direct output
    to memory is a special case of a job.
    After multiple different tries, this "job list" approach
    is the best one I found yet; the most important aspect is that
    when we merge and filter, we always know the final output file,
    and all the source files ultimately involved, even when they
    are handled in a different pass. We need this information for
    example to determine when we need to regenerate at all, or
    when applying source filters.
    Example return:
    \output.js\: (
    ((\js\), (\file1\, \file2\, \file3\, \file4\,)),
    ((\js\, \sass\), (\foo\, \bar\,)),
    ((), (\no\, \filters\, \applied\,)),
    \link-to-source.js\: \js/link-to-source.js\,
    TODO: Consider that the tree:
    csspack (files)
    sass (files
    csspack (files)
    could be more processed with less filter-applications as
    "csspack(files + sass(files) + files))"
    Currently we are in fact doing:
    "csspack(files) + csspack(sass(files)) + csspack(files)"
    """
    jobs = SortedDict()
    def handle(bundle, work_list=[], parent_filters=[], output=False, debug=None):
        (do_merge, do_filters) = resolve_action(bundle, debug, allow_debug=allow_debug)
        sum_filters = parent_filters
        if (do_filters and bundle.filters):
            sum_filters = merge_filters(bundle.filters, parent_filters)
        create_needed = False
        if (not do_merge):
            output = False
        elif (not output):
            output = bundle.output
            create_needed = True
        else:
            pass
        _files = []
        for c in bundle.contents:
            if (not isinstance(c, Bundle)):
                if (not do_merge):
                    jobs[c] = c
                else:
                    _files.append(c)
            else:
                this_debug = (bundle.debug if (bundle.debug != None) else debug)
                if do_merge:
                    if _files:
                        work_list.append((sum_filters, _files))
                    _files = []
                    handle(c, work_list, sum_filters, output, this_debug)
                else:
                    handle(c, [], sum_filters, None, this_debug)
        if _files:
            work_list.append((sum_filters, _files))
        if create_needed:
            if output:
                if (output in jobs):
                    raise BundleError(('Bundle    %s    targets    the    same    output    file    as    another    bundle:    %s' % (bundle, output)))
                jobs[output] = work_list[:]
                del work_list[:]
            elif ((not output) and work_list):
                raise BundleError(('No    output    target    found    for    %s' % bundle))
    handle(bundle)
    return jobs