FilesOpen.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import platform
  2. import os
  3. import subprocess
  4. import xl2dict
  5. import baangt.base.GlobalConstants as GC
  6. from logging import getLogger
  7. logger = getLogger("pyC")
  8. def open(filenameAndPath: str):
  9. """Will open the given file with the Operating system default program.
  10. param
  11. filenameAndPath : Complete absolute path to file.
  12. return : True if sucessfully open. False if file doesn't exists or operating system doesn't have default program.
  13. """
  14. if not isinstance(filenameAndPath, str):
  15. filenameAndPath = str(filenameAndPath)
  16. filenameAndPath = os.path.abspath(filenameAndPath)
  17. logger.debug(f"Trying to open file with it's application: {filenameAndPath}")
  18. if not os.path.exists(filenameAndPath):
  19. logger.warning(f"Filename doesn't exist and can't be opened: {filenameAndPath}")
  20. return False
  21. elif platform.system().lower() == GC.PLATFORM_WINDOWS:
  22. try:
  23. filenameAndPath = f'"{filenameAndPath}"'
  24. os.startfile(filenameAndPath)
  25. return True
  26. except Exception as errorcode:
  27. if errorcode.errno == 22:
  28. os.popen(r"Rundll32.exe SHELL32.DLL, OpenAs_RunDLL "+filenameAndPath)
  29. return True
  30. else:
  31. return False
  32. elif platform.system().lower() == GC.PLATFORM_LINUX:
  33. logger.debug(f"In Linux trying to call xdg-open with filename: {str(filenameAndPath)}")
  34. status = subprocess.call(["xdg-open", str(filenameAndPath)])
  35. if status == 0:
  36. return True
  37. else:
  38. return False
  39. elif platform.system().lower() == GC.PLATFORM_MAC:
  40. filenameAndPath = f'"{filenameAndPath}"'
  41. status = os.system("open " + str(filenameAndPath))
  42. if status == 0:
  43. return True
  44. else:
  45. return False
  46. class FilesOpen:
  47. """
  48. Class is called from UI and will open the corresponding file-type in the os-specific application.
  49. """
  50. def __init__(self):
  51. pass
  52. @staticmethod
  53. def openTestRunDefinition(filenameAndPath):
  54. try:
  55. xl_object = xl2dict.XlToDict()
  56. xl_dict = xl_object.fetch_data_by_column_by_sheet_name(filenameAndPath, sheet_name="TestCaseSequence")
  57. for data in xl_dict:
  58. file_path = data["TestDataFileName"]
  59. if not os.path.exists(file_path):
  60. file_path = os.path.join(os.path.dirname(filenameAndPath), file_path)
  61. status = open(file_path)
  62. except:
  63. pass
  64. return open(filenameAndPath=filenameAndPath)
  65. @staticmethod
  66. def openResultFile(filenameAndPath):
  67. return open(filenameAndPath=filenameAndPath)
  68. @staticmethod
  69. def openLogFile(filenameAndPath):
  70. return open(filenameAndPath=filenameAndPath)