c11_export.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import json
  2. import logging
  3. import os
  4. from .c11_api import c11_api
  5. from datetime import datetime
  6. class c11_export:
  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}/error_{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 export_folder(self, folder='', format='PDF') -> None:
  27. if folder == '':
  28. folder = 'Team Content' if format == 'XML' else 'Team Content/ReportOutput'
  29. elif not folder.startswith('Team Content'):
  30. folder = 'Team Content/ReportOutput/' + folder
  31. reports = self.api.get_reports_in_folder(folder, True)
  32. for r in reports:
  33. print(r['name'])
  34. if format == 'PDF':
  35. self.export_pdf(r['id'], folder)
  36. if format == 'XML':
  37. self.export_unstubbed(r['id'])
  38. def export_unstubbed(self, report_id):
  39. report = self.api.get_report(report_id)
  40. unstubbed_report = self.api.request_unstubbed(report_id)
  41. if unstubbed_report:
  42. filename = f"{self.spec_dir}/{report['path']}/{report['name']}.xml"
  43. os.makedirs(os.path.dirname(filename), exist_ok=True)
  44. with open(filename, "w") as f:
  45. f.write(unstubbed_report)
  46. def export_pdf(self, report_id, folder=None):
  47. report = self.api.get_report(report_id)
  48. if 'meta' not in report:
  49. logging.warning(report['name'] + ' is not accessible!')
  50. return False
  51. params = {}
  52. if len(report['meta']['required']) > 0:
  53. if set(report['meta']['required'].keys()) != {'p_Von', 'p_Bis'}:
  54. return False
  55. params['p_Von'] = {'2022-10-12': '12.10.2022'}
  56. params['p_Bis'] = {'2022-10-12': '12.10.2022'}
  57. params['p_Zeitraum'] = {'Einzelne Monate': 'Einzelne Monate'}
  58. for k, v in report['meta']['optional'].items():
  59. if k in ['p_Zeit', 'p_Auswahl_Monate', 'p_12_Monate']:
  60. for k1, v1 in reversed(v.items()):
  61. if v1 != 'Invalid Dates':
  62. params[k] = {k1: v1}
  63. continue # use last element only
  64. if len(report['params']) == 0:
  65. filename = report['filename']
  66. self.request_and_save_file(report['id'], params, filename)
  67. return True
  68. if len(report['params']) == 1:
  69. filename = report['filename'].format('1')
  70. self.request_and_save_file(report['id'], params, filename)
  71. key1 = report['params'][0]
  72. for k1, v1 in report['meta']['optional'][key1].items():
  73. filename = report['filename'].format(v1)
  74. params[key1] = {k1: v1}
  75. self.request_and_save_file(report['id'], params, filename)
  76. return True
  77. if len(report['params']) == 2:
  78. key1, key2 = report['params']
  79. for k1, v1 in report['meta']['optional'][key1].items():
  80. for k2, v2 in report['meta']['optional'][key2].items():
  81. filename = report['filename'].format(v1, v2)
  82. params[key1] = {k1: v1}
  83. params[key2] = {k2: v2}
  84. self.request_and_save_file(report['id'], params, filename)
  85. return True
  86. def request_and_save_file(self, report_id, params, filename):
  87. logging.debug(filename)
  88. logging.debug(params)
  89. status_code, content = self.api.request_file(report_id, params)
  90. if status_code == 200:
  91. os.makedirs(os.path.dirname(filename), exist_ok=True)
  92. with open(filename, "wb") as f:
  93. f.write(content)
  94. else:
  95. logging.warning(content)
  96. def export_errors(self):
  97. reports = self.api.get_reports_in_folder('Team Content', recursive=True, specs=True)
  98. errors = [r for r in reports if 'error' in r]
  99. filename = 'C:/GlobalCube/Tasks/logs/c11_report_errors.json'
  100. json.dump(errors, open(filename, 'w'), indent=2)
  101. if __name__ == '__main__':
  102. api = c11_api()
  103. api.login()
  104. pdf = c11_export(None, api)
  105. # pdf.export_folder('Team Content/Verkauf/1. Gesamtverkauf', 'PDF')
  106. # pdf.export_folder('Team Content/Aftersales/1. Service')
  107. pdf.export_errors()