tags.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 General Public License as published by
  10. # the Free Software Foundation; either version 2 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. '''VM tags interface'''
  21. class Tags(object):
  22. '''Manager of the tags.
  23. Tags are simple: tag either can be present on qube or not. Tag is a
  24. simple string consisting of ASCII alphanumeric characters, plus `_` and
  25. `-`.
  26. '''
  27. # pylint: disable=too-few-public-methods
  28. def __init__(self, vm):
  29. super().__init__()
  30. self.vm = vm
  31. def remove(self, elem):
  32. '''Remove a tag'''
  33. self.vm.qubesd_call(self.vm.name, 'admin.vm.tag.Remove', elem)
  34. def add(self, elem):
  35. '''Add a tag'''
  36. self.vm.qubesd_call(self.vm.name, 'admin.vm.tag.Set', elem)
  37. def update(self, *others):
  38. '''Add tags from iterable(s)'''
  39. for other in others:
  40. for elem in other:
  41. self.add(elem)
  42. def discard(self, elem):
  43. '''Remove a tag if present'''
  44. try:
  45. self.remove(elem)
  46. except KeyError:
  47. pass
  48. def __iter__(self):
  49. qubesd_response = self.vm.qubesd_call(self.vm.name,
  50. 'admin.vm.tag.List')
  51. return iter(qubesd_response.decode('utf-8').splitlines())
  52. def __contains__(self, elem):
  53. '''Does the VM have a tag'''
  54. response = self.vm.qubesd_call(self.vm.name, 'admin.vm.tag.Get', elem)
  55. return response == b'1'