import json
import os
import shutil

cd = os.path.dirname(os.path.realpath(__file__))
template_filepath = F"{cd}/../input_files/sdg_template.json"
dag_template = F"{cd}/dag_template.py"
output_path = F"{cd}/../airflow/dags/"

f = open(template_filepath)
config = json.load(f)
f.close()

for dataflow in config["dataflows"]:
    file_path=output_path+dataflow["name"].replace('-','_').replace(' ','_')+".py"
    if os.path.exists(file_path):
        os.remove(file_path)
    shutil.copyfile(dag_template, file_path)
    dag_generator=[F"\ndataflow={dataflow}"]
    dag_generator+=[F"\n\n@dag(dag_id=dataflow[\"name\"],\
    \n     schedule_interval=None,\
    \n     start_date=datetime(2022, 2, 26),\
    \n     catchup=False,\
    \n     tags=['sdg'])\
    \ndef taskflow():\
    \n     iterate_sources(dataflow)\
    \n\na=taskflow()"]
    with open(file_path, "a") as file_object:
        file_object.writelines(dag_generator)
import json
import os
import shutil

cd = os.path.dirname(os.path.realpath(__file__))
template_filepath = F"{cd}/../input_files/sdg_template.json"
dag_template = F"{cd}/dag_template.py"
output_path = F"{cd}/../airflow/dags/"

f = open(template_filepath)
config = json.load(f)
f.close()

for dataflow in config["dataflows"]:
    file_path=output_path+dataflow["name"].replace('-','_').replace(' ','_')+".py"
    if os.path.exists(file_path):
        os.remove(file_path)
    shutil.copyfile(dag_template, file_path)
    dag_generator=[F"\ndataflow={dataflow}"]
    dag_generator+=[F"\n\n@dag(dag_id=dataflow[\"name\"],\
    \n     schedule_interval=None,\
    \n     start_date=datetime(2022, 2, 26),\
    \n     catchup=False,\
    \n     tags=['sdg'])\
    \ndef taskflow():\
    \n     iterate_sources(dataflow)\
    \n\na=taskflow()"]
    with open(file_path, "a") as file_object:
        file_object.writelines(dag_generator)