impromptu.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import plac
  2. import subprocess
  3. import re
  4. from pathlib import Path
  5. from datetime import datetime
  6. import config
  7. cfg = config.Config()
  8. imr_dir = cfg.system_dir + '\\IQD'
  9. catalog_dir = cfg.system_dir + '\\Catalogs'
  10. export_translation = {
  11. 'csv': 'x_ascii.flt',
  12. 'xls': 'x_excel.flt',
  13. 'sql': 'x_sql.flt',
  14. 'txt': 'x_text.flt',
  15. 'iqd': 'x_iqd.flt',
  16. 'ims': 'ims'
  17. }
  18. min_filesize = 2000 # 1 kB
  19. @plac.pos('imr_file', '', type=str)
  20. @plac.opt('export_format', '', type=str, choices=export_translation.keys())
  21. @plac.opt('export_dir', '', type=str)
  22. @plac.opt('catalog', '', type=str)
  23. def export(imr_file, export_format='csv', export_dir=None, catalog=None):
  24. imr_full_filename = imr_dir + '\\' + imr_file
  25. export_filename = Path(imr_full_filename).name[:-3] + export_format
  26. if export_dir is None:
  27. export_dir = str(Path(imr_full_filename).parent)
  28. export_full_filename = f'{export_dir}\\{export_filename}'
  29. if catalog is None:
  30. catalog = catalog_name(imr_file)
  31. if catalog[-4:] != '.cat':
  32. catalog = catalog + '.cat'
  33. catalog_full_filename = f'{catalog_dir}\\{catalog}'
  34. start = datetime.now().timestamp()
  35. cmd = f'"{cfg.cognos_dir}\\runmac32.exe" "{cfg.tools_dir}\\VBS\\impromptu.mac" "{imr_full_filename}","{catalog_full_filename}","{export_full_filename}","{export_translation[export_format]}"'
  36. print(f"Exportiere '{imr_file}' als '{export_format}' nach '{export_dir}'...", end='')
  37. p = subprocess.Popen(cmd)
  38. p.wait()
  39. if Path(export_full_filename).exists():
  40. export_stat = Path(export_full_filename).stat()
  41. if export_stat.st_mtime > start and export_stat.st_size > min_filesize:
  42. print('erfolgreich.')
  43. filesize_kb = round(export_stat.st_size / 1024)
  44. stop = datetime.now().timestamp()
  45. delta = round((stop - start))
  46. print(str(filesize_kb) + ' KB in ' + str(delta // 60) + ':' + str(delta % 60).zfill(2) + ' min geschrieben.')
  47. return
  48. print('fehlgeschlagen.')
  49. def catalog_name(imr_file):
  50. p = re.compile(r'catalogs\\([^\.]+)\.cat', re.IGNORECASE)
  51. with open(imr_dir + '\\' + imr_file, 'r', errors='ignore') as f:
  52. for line in f.readlines():
  53. m = p.search(line)
  54. if m:
  55. return p.match(m.group()).groups()[0].lower()
  56. if __name__ == '__main__':
  57. # plac.call(export)
  58. export('Belege\\current_date_Prognose.imr', 'ims')