import os
import copy

from datapackage_pipelines.wrapper import process
from os_package_registry import PackageRegistry

ES_ADDRESS = os.environ.get('ELASTICSEARCH_ADDRESS')


def modify_datapackage(dp, parameters, *_):
    dataset_id = parameters['dataset-id']
    loaded = parameters.get('loaded')
    datapackage_url = parameters.get('datapackage-url')
    if ES_ADDRESS:
        registry = PackageRegistry(ES_ADDRESS)
        params = {}
        if 'babbageModel' in dp:
            model = dp['babbageModel']
            datapackage = copy.deepcopy(dp)
            del datapackage['babbageModel']
            params.update(dict(
                model=model,
                datapackage=datapackage
            ))
        if datapackage_url:
            params['datapackage_url'] = datapackage_url
        if loaded is not None:
            params['loaded'] = loaded
            params['loading_status'] = 'done' if loaded else 'loading-data'
        registry.update_model(
            dataset_id,
            **params
        )
    return dp


if __name__ == '__main__':
    process(modify_datapackage=modify_datapackage)

@@ -416,16 +416,56 @@
 DDRESS)%0A
+        datapackage = copy.deepcopy(dp)%0A
         
@@ -506,17 +506,26 @@
 el' in d
-p
+atapackage
 :%0A      
@@ -539,17 +539,26 @@
 odel = d
-p
+atapackage
 %5B'babbag
@@ -570,52 +570,8 @@
 l'%5D%0A
-            datapackage = copy.deepcopy(dp)%0A
     
@@ -632,105 +632,25 @@
 rams
-.update(dict(%0A                model=model,%0A                datapackage=datapackage%0A            ))
+%5B'model'%5D = model
 %0A   
@@ -730,16 +730,64 @@
 age_url%0A
+            params%5B'datapackage'%5D = datapackage%0A
         

Change where we're saving the datapackage