portal.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import plac
  2. import subprocess
  3. import pandas as pd
  4. import time
  5. from datetime import datetime
  6. from pathlib import Path
  7. from shutil import copy2
  8. import config
  9. cfg = config.config()
  10. report_dir = cfg.portal_dir + '\\System\\Report'
  11. publish_dir = cfg.portal_dir + '\\Publish\\daten'
  12. @plac.pos('config_file', '', type=str)
  13. def portal(config_file='GAPS_neu'):
  14. print(f"== Portal '{config_file}.xml' ==")
  15. full_filename = f'{cfg.xml_dir}\\{config_file}.xml'
  16. if not Path(full_filename).exists():
  17. print(f"!! Datei '{full_filename}' existiert nicht !!")
  18. return
  19. prepare_report_temp(config_file)
  20. pub_dir = Path(f'{publish_dir}\\{config_file}')
  21. rep_dir = f'{report_dir}\\{config_file}'
  22. if not pub_dir.exists():
  23. pub_dir.mkdir()
  24. with open(f'{cfg.log_dir}\\{config_file}.mac.log', 'w') as logfile:
  25. export_files('jpg', rep_dir, pub_dir, logfile)
  26. export_files('xls', rep_dir, pub_dir, logfile)
  27. export_files('pdf', rep_dir, pub_dir, logfile)
  28. def export_files(export_format, rep_dir, pub_dir, logfile):
  29. rep_dir = f'{rep_dir}\\{export_format}'
  30. cmd = f'"{cfg.cognos_dir}\\runmac32.exe" "{cfg.tools_dir}\\publish-reports.mac" "{rep_dir}","{export_format}","{pub_dir}"'
  31. print(f"Exportiere '{rep_dir}' nach '{pub_dir}'...")
  32. start = datetime.now().timestamp()
  33. p = subprocess.Popen(cmd, stdout=logfile, stderr=logfile)
  34. reports = sorted(Path(rep_dir).glob('*.pp[rx]'))
  35. exports = [translate_filename(f.name, export_format, str(pub_dir)) for f in reports]
  36. exports_remaining = exports
  37. while p.poll() is None:
  38. exports_remaining = [f for f in exports_remaining if not f.exists() or f.stat().st_mtime < start]
  39. print('Fortschritt: ' + str(len(exports) - len(exports_remaining)) + '/' + str(len(exports)) + '...', end='\r')
  40. time.sleep(5)
  41. print('Abgeschlossen. ')
  42. if export_format == 'jpg':
  43. rename_files(pub_dir)
  44. cleanup_dir(pub_dir)
  45. def translate_filename(filename, export_format, pub_dir):
  46. if export_format == 'jpg':
  47. return Path(pub_dir + '\\' + filename[:-4] + 'graph1.jpg')
  48. return Path(pub_dir + '\\' + filename[:-4] + '_0.' + export_format)
  49. def rename_files(pub_dir):
  50. for f in pub_dir.glob('*.jpg'):
  51. if f.exists():
  52. new_name = str(f).replace('graph', '_')
  53. f.replace(Path(new_name))
  54. def cleanup_dir(pub_dir):
  55. for f in pub_dir.glob('*.htm'):
  56. f.unlink()
  57. def prepare_report_temp(config_file):
  58. df = pd.read_csv(cfg.xml_dir + '\\info\\reports.csv', sep=';')
  59. df = df[df['Datei'].str.contains(config_file + '\\.', case=False)]
  60. format_list = [('GIF', 'jpg'), ('PDF', 'pdf'), ('XLS', 'xls')]
  61. for (filter_col, export_format) in format_list:
  62. reports = set(df[df[filter_col] == 'J']['Report'].to_list())
  63. rep_dir = f'{report_dir}\\{config_file}\\{export_format}'
  64. unlink_and_recreate(Path(rep_dir))
  65. for rep in reports:
  66. ppx = report_dir + '\\' + rep + '.ppx'
  67. ppr = report_dir + '\\' + rep + '.ppr'
  68. if Path(ppx).exists():
  69. copy2(ppx, rep_dir)
  70. elif Path(ppr).exists():
  71. copy2(ppr, rep_dir)
  72. def unlink_and_recreate(dirname):
  73. if not dirname.exists():
  74. dirname.mkdir(parents=True)
  75. else:
  76. for f in dirname.glob('*.pp[xr]'):
  77. f.unlink()
  78. if __name__ == '__main__':
  79. plac.call(portal)