__init__.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # coding=utf-8
  2. # pystray
  3. # Copyright (C) 2016-2020 Moses Palmér
  4. #
  5. # This program is free software: you can redistribute it and/or modify it under
  6. # the terms of the GNU Lesser General Public License as published by the Free
  7. # Software Foundation, either version 3 of the License, or (at your option) any
  8. # later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import os
  18. import sys
  19. def backend():
  20. """Returns the backend module.
  21. """
  22. def appindicator():
  23. from . import _appindicator as backend; return backend
  24. def darwin():
  25. from . import _darwin as backend; return backend
  26. def gtk():
  27. from . import _gtk as backend; return backend
  28. def win32():
  29. from . import _win32 as backend; return backend
  30. def xorg():
  31. from . import _xorg as backend; return backend
  32. backends = {b.__name__: b for b in (
  33. appindicator, darwin, gtk, win32, xorg)}
  34. backend_name = os.environ.get('PYSTRAY_BACKEND', None)
  35. if backend_name:
  36. try:
  37. candidates = [backends[backend_name]]
  38. except KeyError as e:
  39. raise ImportError('unknown backend: {}'.format(e))
  40. elif sys.platform == 'darwin':
  41. candidates = [darwin]
  42. elif sys.platform == 'win32':
  43. candidates = [win32]
  44. else:
  45. candidates = [appindicator, gtk, xorg]
  46. errors = []
  47. for candidate in candidates:
  48. try:
  49. return candidate()
  50. except ImportError as e:
  51. errors.append(e)
  52. raise ImportError('this platform is not supported: {}'.format(
  53. '; '.join(str(e) for e in errors)))
  54. Icon = backend().Icon
  55. del backend
  56. from ._base import Menu, MenuItem