# 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)


from dataflows import Flow, set_type, validate, delete_fields
from datapackage_pipelines.wrapper import ingest
from datapackage_pipelines.utilities.flow_utils import spew_flow


def flow(parameters):
    resources = parameters.get('resources')
    regex = parameters.get('regex', True)
    if 'types' in parameters:
        return Flow(
            *[
                set_type(name, resources=resources, regex=regex, **options)
                if options is not None else
                delete_fields([name], resources=resources)
                for name, options in parameters['types'].items()
            ]
        )
    else:
        return Flow(
            validate()
        )


if __name__ == '__main__':
    with ingest() as ctx:
        print(flow(ctx.parameters).chain)

# 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/set_types.py
from dataflows import Flow, set_type, validate, delete_fields
from datapackage_pipelines.wrapper import ingest
from datapackage_pipelines.utilities.flow_utils import spew_flow



def flow(parameters):
    resources = parameters.get('resources')
    regex = parameters.get('regex', True)
    if 'types' in parameters:
        return Flow(
            *[
                set_type(name, resources=resources, regex=regex, **options)
                if options is not None else
                delete_fields([name], resources=resources)
                for name, options in parameters['types'].items()
            ]
        )
    else:
        return Flow(
            validate()
        )


if __name__ == '__main__':
    with ingest() as ctx:
        print(flow(ctx.parameters).chain)

        spew_flow(flow(ctx.parameters), ctx)