PathManagement.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import os
  2. import json
  3. import platform
  4. from pathlib import Path
  5. from baangt.base import GlobalConstants as GC
  6. from baangt.TestSteps import Exceptions as baangtExceptions
  7. import logging
  8. logger = logging.getLogger("pyC")
  9. class Singleton(type):
  10. _instances = {}
  11. def __call__(cls, *args, **kwargs):
  12. if cls not in cls._instances:
  13. cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
  14. return cls._instances[cls]
  15. class ManagedPaths(metaclass=Singleton):
  16. """
  17. The class to manage paths for different task.
  18. Main Methods:
  19. - getLogfilePath: Will return a path for logfile directory.
  20. - getOrSetScreenshotsPath: Will return a path for Screenshots directory. You can also set path.
  21. - getOrSetDownloadsPath: Will return a path for Download directory. You can also set path.
  22. - getOrSetAttachmentDownloadPath: Will return a path for Attachment Download directory. You can also set path.
  23. - getOrSetDriverPath: Will return path were webdriver is located. You can also set path.
  24. - getOrSetImportPath: Will return path were input files are located. You can also set path.
  25. - getOrSetExportPath: Will return path were files will be save. You can also set path.
  26. - getOrSetRootPath: Will return root directory path. You can also set path.
  27. - getOrSetIni: Will return path of directory where ini files are managed.
  28. """
  29. def __init__(self):
  30. self.LogFilePath = ""
  31. self.LogFilePath = self.getLogfilePath()
  32. self.ScreenshotPath = ""
  33. self.DownloadPath = ""
  34. self.AttachmentDownloadPath = ""
  35. self.DriverPath = ""
  36. self.RootPath = ""
  37. self.ExportPath = ""
  38. self.ImportPath = ""
  39. self.IniPath = ""
  40. def getLogfilePath(self):
  41. """
  42. Will return path where Log files will be saved.
  43. This Path will be taken from old_Paths.json
  44. :return: Logfile path
  45. """
  46. if self.LogFilePath:
  47. return self.LogFilePath
  48. self.LogFilePath = self.__combineBasePathWithObjectPath("Logs")
  49. self.__makeAndCheckDir(self.LogFilePath)
  50. return self.LogFilePath
  51. def getOrSetScreenshotsPath(self, path=None):
  52. """
  53. Will return path where Screenshots taken by the browser will be saved.
  54. Default path will be Screenshots folder in current working directory.
  55. :param path: Path to be set for browser screenshots.
  56. :return: Screenshot path
  57. """
  58. if self.ScreenshotPath != "":
  59. return self.ScreenshotPath
  60. if path:
  61. self.ScreenshotPath = Path(path)
  62. else:
  63. self.ScreenshotPath = self.__combineBasePathWithObjectPath(GC.PATH_SCREENSHOTS)
  64. self.__makeAndCheckDir(self.ScreenshotPath)
  65. return self.ScreenshotPath
  66. def getOrSetDownloadsPath(self, path=None):
  67. """
  68. Will return path where downloaded file will be saved.
  69. Default path will be 1TestResults folder in current working directory.
  70. :param path: Path to be set for browser downloads if download path didn't exists.
  71. :return: Download path
  72. """
  73. if self.DownloadPath != "":
  74. return self.DownloadPath
  75. if path:
  76. self.DownloadPath = path
  77. else:
  78. self.DownloadPath = self.__combineBasePathWithObjectPath("1TestResults")
  79. self.__makeAndCheckDir(self.DownloadPath)
  80. return self.DownloadPath
  81. def getOrSetAttachmentDownloadPath(self, path=None):
  82. """
  83. Will return path where downloaded file will be saved.
  84. Default path will be TestDownloads folder in current working directory.
  85. :param path: Path to be set for browser Attachment Downloads if AttachmentDownloadPath path didn't exists.
  86. :return: Attachment Download path
  87. """
  88. if self.AttachmentDownloadPath != "":
  89. return self.AttachmentDownloadPath
  90. if path:
  91. self.AttachmentDownloadPath = path
  92. else:
  93. self.AttachmentDownloadPath = os.path.join(self.getOrSetDownloadsPath(), "TestDownloads")
  94. self.__makeAndCheckDir(self.AttachmentDownloadPath)
  95. return self.AttachmentDownloadPath
  96. def getOrSetDriverPath(self, path=None):
  97. """
  98. Will return path where webdrivers are located.
  99. Default path will be browserDriver folder in current working directory.
  100. :param path: Path to be set for location where webdrivers are located or to be downloaded.
  101. :return: Webdriver path
  102. """
  103. if self.DriverPath != "":
  104. return self.DriverPath
  105. if path:
  106. self.DriverPath = path
  107. else:
  108. self.DriverPath = self.__combineBasePathWithObjectPath("browserDrivers")
  109. self.__makeAndCheckDir(self.DriverPath)
  110. return self.DriverPath
  111. def getOrSetRootPath(self, path=None):
  112. """
  113. Will return path for root directory.
  114. Default path will be current working directory.
  115. :param path: Path to be set as root directory.
  116. :return: Root path
  117. """
  118. if self.RootPath != "":
  119. return self.RootPath
  120. if path:
  121. self.RootPath = path
  122. else:
  123. self.RootPath = os.getcwd()
  124. return self.RootPath
  125. def getOrSetExportPath(self, path=None):
  126. """
  127. Will return path where output files should be save.
  128. :param path: Path to be set for Export Path.
  129. :return: Export path
  130. """
  131. if self.ExportPath != "":
  132. return self.ExportPath
  133. if path:
  134. self.ExportPath = path
  135. else:
  136. self.ExportPath = self.__combineBasePathWithObjectPath(GC.PATH_EXPORT)
  137. self.__makeAndCheckDir(self.ExportPath)
  138. return self.ExportPath
  139. def getOrSetImportPath(self, path=None):
  140. """
  141. Will return path where program will search for input files.
  142. :param path: Path to be set for location where input files are located.
  143. :return: Import path
  144. """
  145. if self.ImportPath != "":
  146. return self.ImportPath
  147. if path:
  148. self.ImportPath = path
  149. else:
  150. self.ImportPath = self.__combineBasePathWithObjectPath(GC.PATH_IMPORT)
  151. self.__makeAndCheckDir(self.ImportPath)
  152. return self.ImportPath
  153. def __combineBasePathWithObjectPath(self, objectPath: str):
  154. """
  155. :param objectPath:
  156. :return:
  157. """
  158. newPath = Path(self.derivePathForOSAndInstallationOption())
  159. newPath = newPath.joinpath(objectPath)
  160. return newPath
  161. def derivePathForOSAndInstallationOption(self):
  162. """
  163. Will provide different **base paths** depending on each OS and install-version (e.g. Windows Repository, Windows
  164. ZIP-file with Executable, Windows Installer vs. MacOS-Installer vs. MacOS Repository.
  165. Each Method/File-Type might have additional rules, like e.g. .joinpath("directory_for_this_filetype")
  166. :return: base Path
  167. """
  168. if platform.system().lower() == GC.PLATFORM_WINDOWS:
  169. path = os.path.join(os.path.expanduser("~"), "baangt")
  170. if os.path.exists(path):
  171. return Path(path)
  172. return Path(os.getcwd())
  173. def __makeAndCheckDir(self, newPath):
  174. """
  175. :param newPath: Path to be made or check existence.
  176. :return: None
  177. """
  178. Path(newPath).mkdir(exist_ok=True, parents=True)
  179. if not Path(newPath).is_dir():
  180. baangtExceptions.baangtTestStepException(f"Tried to create folder {newPath} and failed.")
  181. logger.debug(f"Tried to create folder {newPath} and failed.")
  182. return None
  183. def getOrSetIni(self, path=None):
  184. """
  185. Will return path where program will search for ini files.
  186. :param path: Path to be set for location where ini files are located.
  187. :return: Ini path
  188. """
  189. if self.IniPath != "":
  190. return self.IniPath
  191. if path:
  192. self.IniPath = path
  193. else:
  194. self.IniPath = self.__combineBasePathWithObjectPath('ini')
  195. self.__makeAndCheckDir(self.IniPath)
  196. return self.IniPath
  197. def findFileInAnyPath(self, filename):
  198. if Path(filename).exists():
  199. return filename
  200. if Path(self.getOrSetExportPath()).joinpath(filename).exists():
  201. return Path(self.getOrSetExportPath()).joinpath(filename)
  202. if Path(self.getOrSetImportPath()).joinpath(filename).exists():
  203. return Path(self.getOrSetImportPath()).joinpath(filename)
  204. if Path(self.getOrSetRootPath()).joinpath(filename).exists():
  205. return Path(self.getOrSetRootPath()).joinpath(filename)
  206. return None