PathManagement.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 where webdriver is located. You can also set path.
  24. - getOrSetImportPath: Will return path where input files are located. You can also set path.
  25. - getOrSetExportPath: Will return path where files will be save. You can also set path.
  26. - getOrSetDBExportPath: Will return path where DB query outputs should be saved. You can also set path.
  27. - getOrSetReportPath: Will return path where reports should be saved. You can also set path.
  28. - getOrSetRootPath: Will return root directory path. You can also set path.
  29. - getOrSetIni: Will return path of directory where ini files are managed.
  30. """
  31. def __init__(self):
  32. self.LogFilePath = ""
  33. self.LogFilePath = self.getLogfilePath()
  34. self.ScreenshotPath = ""
  35. self.DownloadPath = ""
  36. self.AttachmentDownloadPath = ""
  37. self.DriverPath = ""
  38. self.RootPath = ""
  39. self.ExportPath = ""
  40. self.ImportPath = ""
  41. self.DBExportPath = ""
  42. self.ReportPath = ""
  43. self.IniPath = ""
  44. def getLogfilePath(self):
  45. """
  46. Will return path where Log files will be saved.
  47. This Path will be taken from old_Paths.json
  48. :return: Logfile path
  49. """
  50. if self.LogFilePath:
  51. return self.LogFilePath
  52. self.LogFilePath = self.__combineBasePathWithObjectPath("Logs")
  53. self.__makeAndCheckDir(self.LogFilePath)
  54. return self.LogFilePath
  55. def getOrSetScreenshotsPath(self, path=None):
  56. """
  57. Will return path where Screenshots taken by the browser will be saved.
  58. Default path will be Screenshots folder in current working directory.
  59. :param path: Path to be set for browser screenshots.
  60. :return: Screenshot path
  61. """
  62. if self.ScreenshotPath != "":
  63. return self.ScreenshotPath
  64. if path:
  65. self.ScreenshotPath = Path(path)
  66. else:
  67. self.ScreenshotPath = self.__combineBasePathWithObjectPath(GC.PATH_SCREENSHOTS)
  68. self.__makeAndCheckDir(self.ScreenshotPath)
  69. return self.ScreenshotPath
  70. def getOrSetDownloadsPath(self, path=None):
  71. """
  72. Will return path where downloaded file will be saved.
  73. Default path will be 1TestResults folder in current working directory.
  74. :param path: Path to be set for browser downloads if download path didn't exists.
  75. :return: Download path
  76. """
  77. if self.DownloadPath != "":
  78. return self.DownloadPath
  79. if path:
  80. self.DownloadPath = path
  81. else:
  82. self.DownloadPath = self.__combineBasePathWithObjectPath("1TestResults")
  83. self.__makeAndCheckDir(self.DownloadPath)
  84. return self.DownloadPath
  85. def getOrSetAttachmentDownloadPath(self, path=None):
  86. """
  87. Will return path where downloaded file will be saved.
  88. Default path will be TestDownloads folder in current working directory.
  89. :param path: Path to be set for browser Attachment Downloads if AttachmentDownloadPath path didn't exists.
  90. :return: Attachment Download path
  91. """
  92. if self.AttachmentDownloadPath != "":
  93. return self.AttachmentDownloadPath
  94. if path:
  95. self.AttachmentDownloadPath = path
  96. else:
  97. self.AttachmentDownloadPath = os.path.join(self.getOrSetDownloadsPath(), "TestDownloads")
  98. self.__makeAndCheckDir(self.AttachmentDownloadPath)
  99. return self.AttachmentDownloadPath
  100. def getOrSetDriverPath(self, path=None):
  101. """
  102. Will return path where webdrivers are located.
  103. Default path will be browserDriver folder in current working directory.
  104. :param path: Path to be set for location where webdrivers are located or to be downloaded.
  105. :return: Webdriver path
  106. """
  107. if self.DriverPath != "":
  108. return self.DriverPath
  109. if path:
  110. self.DriverPath = path
  111. else:
  112. self.DriverPath = self.__combineBasePathWithObjectPath("browserDrivers")
  113. self.__makeAndCheckDir(self.DriverPath)
  114. return self.DriverPath
  115. def getOrSetRootPath(self, path=None):
  116. """
  117. Will return path for root directory.
  118. Default path will be current working directory.
  119. :param path: Path to be set as root directory.
  120. :return: Root path
  121. """
  122. if self.RootPath != "":
  123. return self.RootPath
  124. if path:
  125. self.RootPath = path
  126. else:
  127. self.RootPath = os.getcwd()
  128. return self.RootPath
  129. def getOrSetExportPath(self, path=None):
  130. """
  131. Will return path where output files should be save.
  132. :param path: Path to be set for Export Path.
  133. :return: Export path
  134. """
  135. if self.ExportPath != "":
  136. return self.ExportPath
  137. if path:
  138. self.ExportPath = path
  139. else:
  140. self.ExportPath = self.__combineBasePathWithObjectPath(GC.PATH_EXPORT)
  141. self.__makeAndCheckDir(self.ExportPath)
  142. return self.ExportPath
  143. def getOrSetImportPath(self, path=None):
  144. """
  145. Will return path where program will search for input files.
  146. :param path: Path to be set for location where input files are located.
  147. :return: Import path
  148. """
  149. if self.ImportPath != "":
  150. return self.ImportPath
  151. if path:
  152. self.ImportPath = path
  153. else:
  154. self.ImportPath = self.__combineBasePathWithObjectPath(GC.PATH_IMPORT)
  155. self.__makeAndCheckDir(self.ImportPath)
  156. return self.ImportPath
  157. def getOrSetDBExportPath(self, path=None):
  158. """
  159. Will return path where DB queriy outputs should be saved.
  160. :param path: Path to be set for DB exports.
  161. :return: DB Export path
  162. """
  163. if self.DBExportPath != "":
  164. return self.DBExportPath
  165. if path:
  166. self.DBExportPath = path
  167. else:
  168. self.DBExportPath = self.__combineBasePathWithObjectPath(GC.PATH_DB_EXPORT)
  169. self.__makeAndCheckDir(self.DBExportPath)
  170. return self.DBExportPath
  171. def getOrSetReportPath(self, path=None):
  172. """
  173. Will return path where reports should be saved.
  174. :param path: Path to be set for reports.
  175. :return: Report path
  176. """
  177. if self.ReportPath != "":
  178. return self.ReportPath
  179. if path:
  180. self.ReportPath = path
  181. else:
  182. self.ReportPath = self.__combineBasePathWithObjectPath(GC.PATH_REPORT)
  183. self.__makeAndCheckDir(self.ReportPath)
  184. return self.ReportPath
  185. def __combineBasePathWithObjectPath(self, objectPath: str):
  186. """
  187. :param objectPath:
  188. :return:
  189. """
  190. newPath = Path(self.derivePathForOSAndInstallationOption())
  191. newPath = newPath.joinpath(objectPath)
  192. return newPath
  193. def derivePathForOSAndInstallationOption(self):
  194. """
  195. Will provide different **base paths** depending on each OS and install-version (e.g. Windows Repository, Windows
  196. ZIP-file with Executable, Windows Installer vs. MacOS-Installer vs. MacOS Repository.
  197. Each Method/File-Type might have additional rules, like e.g. .joinpath("directory_for_this_filetype")
  198. :return: base Path
  199. """
  200. if platform.system() == "Windows":
  201. path = os.path.join(os.path.expanduser("~"), "baangt")
  202. if os.path.exists(path):
  203. return Path(path)
  204. return Path(os.getcwd())
  205. def __makeAndCheckDir(self, newPath):
  206. """
  207. :param newPath: Path to be made or check existence.
  208. :return: None
  209. """
  210. Path(newPath).mkdir(exist_ok=True, parents=True)
  211. if not Path(newPath).is_dir():
  212. baangtExceptions.baangtTestStepException(f"Tried to create folder {newPath} and failed.")
  213. logger.debug(f"Tried to create folder {newPath} and failed.")
  214. return None
  215. def getOrSetIni(self, path=None):
  216. """
  217. Will return path where program will search for ini files.
  218. :param path: Path to be set for location where ini files are located.
  219. :return: Ini path
  220. """
  221. if self.IniPath != "":
  222. return self.IniPath
  223. if path:
  224. self.IniPath = path
  225. else:
  226. self.IniPath = self.__combineBasePathWithObjectPath('ini')
  227. self.__makeAndCheckDir(self.IniPath)
  228. return self.IniPath