__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. from __future__ import print_function
  18. import itertools
  19. from PIL import Image, ImageDraw
  20. from six.moves import input
  21. import pystray
  22. from pystray import MenuItem as item
  23. COLORS = itertools.cycle((
  24. 'black',
  25. 'white',
  26. 'red',
  27. 'yellow',
  28. 'blue',
  29. 'red',
  30. 'green',
  31. 'white'))
  32. def say(*args, **kwargs):
  33. """Prints a message, ensuring space between messages.
  34. """
  35. print('\n')
  36. print(*args, **kwargs)
  37. def action(on_activate):
  38. """A convenience function to create a hidden default menu item.
  39. :param callable on_activate: The activation callback.
  40. """
  41. return item('Default', on_activate, default=True, visible=False)
  42. def separator():
  43. """A wrapper around :attr:`pystray.Menu.SEPARATOR`.
  44. """
  45. return pystray.Menu.SEPARATOR
  46. def icon(no_image=False, **kwargs):
  47. """Generates a systray icon with the specified colours.
  48. A systray icon created by this method will be automatically hidden
  49. when the current test finishes.
  50. :return: the tuple ``(icon, colors)``, where ``icon`` is a
  51. hidden systray icon, and ``colors`` is the ``colors`` return value
  52. of :meth:`image`.
  53. """
  54. img, colors = image()
  55. ico = pystray.Icon(
  56. 'test',
  57. icon=img if not no_image else None,
  58. **kwargs)
  59. return ico, colors
  60. def image(width=64, height=64):
  61. """Generates an icon image.
  62. :return: the tuple ``(image, colors)``, where ``image`` is a
  63. *PIL* image and ``colors`` is a tuple containing the colours as
  64. *PIL* colour names, suitable for printing; the stringification of
  65. the tuple is also suitable for printing
  66. """
  67. class Colors(tuple):
  68. def __str__(self):
  69. return ' and '.join(self)
  70. colors = Colors((next_color(), next_color()))
  71. img = Image.new('RGB', (width, height), colors[0])
  72. dc = ImageDraw.Draw(img)
  73. dc.rectangle((width // 2, 0, width, height // 2), fill=colors[1])
  74. dc.rectangle((0, height // 2, width // 2, height), fill=colors[1])
  75. return img, colors
  76. def next_color():
  77. """Returns the next colour to use.
  78. """
  79. return next(COLORS)
  80. def confirm(self, statement, *fmt):
  81. """Asks the user to confirm a statement.
  82. :param self: An instance of a test suite.
  83. :param str statement: The statement to confirm.
  84. :raises AssertionError: if the user does not confirm
  85. """
  86. valid_responses = ('yes', 'y', 'no', 'n')
  87. accept_responses = valid_responses[:2]
  88. message = ('\n' + statement % fmt) + ' '
  89. while True:
  90. response = input(message)
  91. if response.lower() in valid_responses:
  92. self.assertIn(
  93. response.lower(), accept_responses,
  94. 'User declined statement "%s"' % message)
  95. return
  96. else:
  97. print(
  98. 'Please respond %s' % ', '.join(
  99. '"%s"' % r for r in valid_responses))
  100. def true(*args):
  101. """Returns ``True``.
  102. """
  103. return True