# Path: datapackage_pipelines/wrapper/wrapper.py
# def ingest(debug=False):
#     params, datapackage, resource_iterator = _ingest(debug=debug)
#     return ProcessorContext(params, datapackage, resource_iterator)
# 
# Path: datapackage_pipelines/utilities/flow_utils.py
# def spew_flow(flow, ctx: ProcessorContext):
#     flow = Flow(
#         update_package(**ctx.datapackage),
#         load((ctx.datapackage, ctx.resource_iterator)),
#         flow,
#     )
#     datastream = flow.datastream()
#     ctx.datapackage = datastream.dp.descriptor
#     ctx.resource_iterator = datastream.res_iter
#     ctx.stats = MergeableStats(datastream.stats, ctx.stats)


import sys
from importlib import import_module
from datapackage_pipelines.wrapper import ingest
from datapackage_pipelines.utilities.flow_utils import spew_flow


with ingest() as ctx:
    parameters, datapackage, resources = ctx
    stats = {}

    sys.path.append(parameters.pop('__path'))
    flow_module = import_module(parameters.pop('__flow'))
    flow = flow_module.flow(parameters, datapackage, resources, ctx.stats)


# Path: datapackage_pipelines/wrapper/wrapper.py
# def ingest(debug=False):
#     params, datapackage, resource_iterator = _ingest(debug=debug)
#     return ProcessorContext(params, datapackage, resource_iterator)
# 
# Path: datapackage_pipelines/utilities/flow_utils.py
# def spew_flow(flow, ctx: ProcessorContext):
#     flow = Flow(
#         update_package(**ctx.datapackage),
#         load((ctx.datapackage, ctx.resource_iterator)),
#         flow,
#     )
#     datastream = flow.datastream()
#     ctx.datapackage = datastream.dp.descriptor
#     ctx.resource_iterator = datastream.res_iter
#     ctx.stats = MergeableStats(datastream.stats, ctx.stats)


# Path: datapackage_pipelines/lib/flow.py
import sys
from importlib import import_module
from datapackage_pipelines.wrapper import ingest
from datapackage_pipelines.utilities.flow_utils import spew_flow



with ingest() as ctx:
    parameters, datapackage, resources = ctx
    stats = {}

    sys.path.append(parameters.pop('__path'))
    flow_module = import_module(parameters.pop('__flow'))
    flow = flow_module.flow(parameters, datapackage, resources, ctx.stats)


    spew_flow(flow, ctx)