c11_create.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # import json
  2. import logging
  3. import os
  4. from .c11_api import c11_api
  5. from datetime import datetime
  6. class c11_create:
  7. api: c11_api
  8. config = None
  9. log_dir = "C:/GlobalCube/Tasks/logs/c11"
  10. spec_dir = "C:/GlobalCube/System/OPTIMA/Report"
  11. def __init__(self, cfg, api=None):
  12. self.config = cfg
  13. self.api = api
  14. if api is None:
  15. self.api = c11_api(self.config).login()
  16. now = datetime.now().strftime("%Y%m%d_%H%M%S")
  17. prot_file = f"{self.log_dir}/create_{now}.log"
  18. os.makedirs(self.log_dir, exist_ok=True)
  19. logging.basicConfig(
  20. filename=prot_file,
  21. filemode="w",
  22. encoding="utf-8",
  23. level=logging.DEBUG,
  24. force=True,
  25. )
  26. def create_path(self, path, recursive=False):
  27. if path.startswith("Team Content"):
  28. fullpath = f"{self.spec_dir}/{path}"
  29. else:
  30. pos = path.find("Team Content")
  31. fullpath = path
  32. path = fullpath[pos:]
  33. fullpath = os.path.abspath(fullpath)
  34. path = path.replace("\\", "/")
  35. if not os.path.exists(fullpath):
  36. logging.error(fullpath + " does not exist!")
  37. return False
  38. folder = path if os.path.isdir(fullpath) else os.path.dirname(path)
  39. folder_id = self.create_folders(folder)
  40. if os.path.isfile(fullpath):
  41. self.create_report(folder_id, folder, fullpath)
  42. def create_folders(self, folder):
  43. folder_split = folder.split("/")
  44. folder_parents = [
  45. "/".join(folder_split[:i]) for i in range(1, len(folder_split) + 1)
  46. ]
  47. print(folder_parents)
  48. folders = self.api.get_folders()
  49. last_id = None
  50. for parent in folder_parents:
  51. parent_id = [f["id"] for f in folders if f["name"] == parent]
  52. if len(parent_id) > 0:
  53. last_id = parent_id.pop()
  54. else:
  55. last_id = self.api.create_folder(last_id, os.path.basename(parent))
  56. return last_id
  57. def create_report(self, folder_id, folder_name, fullpath):
  58. report_name = os.path.basename(fullpath)[:-4]
  59. reports = [
  60. r
  61. for r in self.api.get_reports_in_folder(folder_name)
  62. if r["name"] == report_name
  63. ]
  64. if len(reports) == 0:
  65. self.api.create_report(folder_id, fullpath)
  66. else:
  67. self.api.update_report(reports[0], fullpath)
  68. if __name__ == "__main__":
  69. create_api = c11_create()
  70. # pdf.export_folder('Team Content/Verkauf/1. Gesamtverkauf', 'PDF')
  71. # pdf.export_folder('Team Content/Aftersales/1. Service')
  72. create_api.create_path("Team Content/ReportOutput/Forderungen")