c11_create.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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().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 = ['/'.join(folder_split[:i]) for i in range(1, len(folder_split) + 1)]
  45. print(folder_parents)
  46. folders = self.api.get_folders()
  47. last_id = None
  48. for parent in folder_parents:
  49. parent_id = [f['id'] for f in folders if f['name'] == parent]
  50. if len(parent_id) > 0:
  51. last_id = parent_id.pop()
  52. else:
  53. last_id = self.api.create_folder(last_id, os.path.basename(parent))
  54. return last_id
  55. def create_report(self, folder_id, folder_name, fullpath):
  56. report_name = os.path.basename(fullpath)[:-4]
  57. reports = [r for r in self.api.get_reports_in_folder(folder_name) if r['name'] == report_name]
  58. if len(reports) == 0:
  59. self.api.create_report(folder_id, fullpath)
  60. else:
  61. self.api.update_report(reports[0], fullpath)
  62. if __name__ == '__main__':
  63. create_api = c11_create()
  64. # pdf.export_folder('Team Content/Verkauf/1. Gesamtverkauf', 'PDF')
  65. # pdf.export_folder('Team Content/Aftersales/1. Service')
  66. create_api.create_path('Team Content/ReportOutput/Forderungen')