c11_jobs.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import json
  2. import csv
  3. import config
  4. def get_converted_jobs(cfg: config.Config):
  5. with open(cfg.cognos11.reports_file, "r") as frh:
  6. reports = json.load(frh)
  7. report_id_to_name = dict([(r["id"], r["name"]) for r in reports])
  8. with open(cfg.cognos11.jobs_file, "r") as frh:
  9. data = json.load(frh)
  10. result = []
  11. if len(data) == 0:
  12. return
  13. print("\nZusaetzlicher C11-interner Versand:")
  14. for job in data:
  15. print(job["name"])
  16. for step in job["details"]["steps"]:
  17. res = {}
  18. res["job_name"] = job["name"]
  19. if not step["options"]:
  20. continue
  21. step_options = dict([(opt["name"], opt["value"]) for opt in step["options"]])
  22. res["to_address"] = ",".join(step_options.get("toAddress", []))
  23. res["params"] = {}
  24. if step["parameters"]:
  25. res["params"] = dict(
  26. [(opt["name"], opt["value"][0]["display"]) for opt in step["parameters"] if len(opt["value"]) > 0]
  27. )
  28. res["report_name"] = report_id_to_name.get(step.get("report_id", "ungültig"), "Verknüpfung ungültig!")
  29. res["subject"] = step_options.get("subject", "")
  30. print(" " + res["report_name"])
  31. # print(" " + ",\n ".join(res["to_address"]))
  32. # print(" ", res["params"])
  33. result.append(res)
  34. with open(cfg.cognos11.config_dir + "/c11_jobs.csv", "w", encoding="latin-1", newline="") as fwh:
  35. csv_writer = csv.writer(fwh, delimiter=";")
  36. csv_writer.writerow(result[0].keys())
  37. for row in result:
  38. csv_writer.writerow(row.values())
  39. with open(
  40. cfg.cognos11.config_dir + "/converted.template.csv",
  41. "w",
  42. encoding="latin-1",
  43. newline="",
  44. ) as fwh:
  45. csv_writer = csv.writer(fwh, delimiter=";")
  46. csv_writer.writerow(["Report", "Empfaenger"])
  47. for row in result:
  48. suffix = "_Summe" if len(row["params"].values()) == 0 else list(row["params"].values())[0]
  49. filename = f'{row["job_name"]}\\{row["report_name"]}_{suffix}.pdf'
  50. csv_writer.writerow([filename, row["to_address"].lower()])