utils.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from subprocess import Popen
  2. from typing import Dict
  3. import shutil
  4. import sys
  5. BROWSERS = {
  6. "win32": {
  7. "Chrome": [
  8. "C:/Program Files/Google/Chrome/Application/chrome.exe",
  9. "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
  10. ],
  11. "Firefox": [
  12. "C:/Program Files/Mozilla Firefox/firefox.exe",
  13. "C:/Program Files (x86)/Mozilla Firefox/firefox.exe"
  14. ],
  15. "Edge": [
  16. "C:/Program Files/Microsoft/Edge/Application/msedge.exe",
  17. "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
  18. ]
  19. },
  20. "darwin": {
  21. "Chrome": [
  22. "/Applications/Google Chrome.app"
  23. ],
  24. "Firefox": [
  25. "/Applications/Firefox.app"
  26. ],
  27. "Safari": [
  28. "/Applications/Safari.app"
  29. ]
  30. },
  31. "linux": {
  32. "Chrome": [
  33. "chromium",
  34. "chrome"
  35. ],
  36. "Firefox": [
  37. "firefox"
  38. ],
  39. "Edge": [
  40. "microsoft-edge"
  41. ]
  42. }
  43. }
  44. def get_available_browsers() -> Dict[str, str]:
  45. browsers = {"Default": ""}
  46. for browser, paths in BROWSERS[sys.platform].items():
  47. for path in paths:
  48. if browser not in browsers and shutil.which(path):
  49. browsers[browser] = path
  50. return browsers
  51. def open_browser(path: str, address: str):
  52. if path == "":
  53. open_default_browser(address)
  54. return
  55. args = ["--no-default-browser-check", "--no-first-run", "--foreground", "--new-window", address]
  56. if sys.platform == "win32":
  57. Popen([path] + args)
  58. elif sys.platform == "darwin":
  59. Popen(["open", "-a", path, "--args"] + args)
  60. elif sys.platform == "linux":
  61. Popen([path] + args)
  62. def open_default_browser(address: str):
  63. if sys.platform == "win32":
  64. Popen(["rundll32", "url.dll,FileProtocolHandler", address])
  65. elif sys.platform == "darwin":
  66. Popen(["open", "-u", address])
  67. elif sys.platform == "linux":
  68. Popen(["xdg-open", address])