label.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''VM Labels'''
  21. import qubesadmin.exc
  22. class Label(object):
  23. '''Label definition for virtual machines
  24. Label specifies colour of the padlock displayed next to VM's name.
  25. :param str color: colour specification as in HTML (``#abcdef``)
  26. :param str name: label's name like "red" or "green"
  27. '''
  28. def __init__(self, app, name):
  29. self.app = app
  30. self._name = name
  31. self._color = None
  32. self._index = None
  33. @property
  34. def color(self):
  35. '''color specification as in HTML (``#abcdef``)'''
  36. if self._color is None:
  37. try:
  38. qubesd_response = self.app.qubesd_call(
  39. 'dom0', 'admin.label.Get', self._name, None)
  40. except qubesadmin.exc.QubesDaemonNoResponseError:
  41. raise AttributeError
  42. self._color = qubesd_response.decode()
  43. return self._color
  44. @property
  45. def name(self):
  46. '''label's name like "red" or "green"'''
  47. return self._name
  48. @property
  49. def icon(self):
  50. '''freedesktop icon name, suitable for use in
  51. :py:meth:`PyQt4.QtGui.QIcon.fromTheme`'''
  52. return 'appvm-' + self.name
  53. @property
  54. def index(self):
  55. '''color specification as in HTML (``#abcdef``)'''
  56. if self._index is None:
  57. try:
  58. qubesd_response = self.app.qubesd_call(
  59. 'dom0', 'admin.label.Index', self._name, None)
  60. except qubesadmin.exc.QubesDaemonNoResponseError:
  61. raise AttributeError
  62. self._index = int(qubesd_response.decode())
  63. return self._index
  64. def __str__(self):
  65. return self._name
  66. def __eq__(self, other):
  67. if isinstance(other, Label):
  68. return self.name == other.name
  69. return NotImplemented