features.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 features interface'''
  21. class Features(object):
  22. '''Manager of the features.
  23. Features can have three distinct values: no value (not present in mapping,
  24. which is closest thing to :py:obj:`None`), empty string (which is
  25. interpreted as :py:obj:`False`) and non-empty string, which is
  26. :py:obj:`True`. Anything assigned to the mapping is coerced to strings,
  27. however if you assign instances of :py:class:`bool`, they are converted as
  28. described above. Be aware that assigning the number `0` (which is considered
  29. false in Python) will result in string `'0'`, which is considered true.
  30. '''
  31. # pylint: disable=too-few-public-methods
  32. def __init__(self, vm):
  33. super().__init__()
  34. self.vm = vm
  35. def __delitem__(self, key):
  36. self.vm.qubesd_call(self.vm.name, 'admin.vm.feature.Remove', key)
  37. def __setitem__(self, key, value):
  38. if isinstance(value, bool):
  39. # False value needs to be serialized as empty string
  40. self.vm.qubesd_call(self.vm.name, 'admin.vm.feature.Set', key,
  41. b'1' if value else b'')
  42. else:
  43. self.vm.qubesd_call(self.vm.name, 'admin.vm.feature.Set', key,
  44. str(value).encode())
  45. def __getitem__(self, item):
  46. return self.vm.qubesd_call(
  47. self.vm.name, 'admin.vm.feature.Get', item).decode('utf-8')
  48. def __iter__(self):
  49. qubesd_response = self.vm.qubesd_call(self.vm.name,
  50. 'admin.vm.feature.List')
  51. return iter(qubesd_response.decode('utf-8').splitlines())
  52. keys = __iter__
  53. def items(self):
  54. '''Return iterable of pairs (feature, value)'''
  55. for key in self:
  56. yield key, self[key]
  57. _NO_DEFAULT = object()
  58. def get(self, item, default=_NO_DEFAULT):
  59. '''Get a feature, return default value if missing.'''
  60. try:
  61. return self[item]
  62. except KeyError:
  63. if default is self._NO_DEFAULT:
  64. raise
  65. return default
  66. def check_with_template(self, feature, default=_NO_DEFAULT):
  67. ''' Check if the vm's template has the specified feature. '''
  68. try:
  69. qubesd_response = self.vm.qubesd_call(
  70. self.vm.name, 'admin.vm.feature.CheckWithTemplate', feature)
  71. return qubesd_response.decode('utf-8')
  72. except KeyError:
  73. if default is self._NO_DEFAULT:
  74. raise
  75. return default