core-admin-client/qubesadmin/features.py

90 lines
3.2 KiB
Python
Raw Permalink Normal View History

2017-04-15 13:53:40 +02:00
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
'''VM features interface'''
class Features(object):
'''Manager of the features.
Features can have three distinct values: no value (not present in mapping,
which is closest thing to :py:obj:`None`), empty string (which is
interpreted as :py:obj:`False`) and non-empty string, which is
:py:obj:`True`. Anything assigned to the mapping is coerced to strings,
however if you assign instances of :py:class:`bool`, they are converted as
described above. Be aware that assigning the number `0` (which is considered
false in Python) will result in string `'0'`, which is considered true.
'''
# pylint: disable=too-few-public-methods
def __init__(self, vm):
super().__init__()
2017-04-15 13:53:40 +02:00
self.vm = vm
def __delitem__(self, key):
self.vm.qubesd_call(self.vm.name, 'admin.vm.feature.Remove', key)
2017-04-15 13:53:40 +02:00
def __setitem__(self, key, value):
if isinstance(value, bool):
# False value needs to be serialized as empty string
self.vm.qubesd_call(self.vm.name, 'admin.vm.feature.Set', key,
b'1' if value else b'')
else:
self.vm.qubesd_call(self.vm.name, 'admin.vm.feature.Set', key,
str(value).encode())
2017-04-15 13:53:40 +02:00
def __getitem__(self, item):
return self.vm.qubesd_call(
self.vm.name, 'admin.vm.feature.Get', item).decode('utf-8')
2017-04-15 13:53:40 +02:00
def __iter__(self):
qubesd_response = self.vm.qubesd_call(self.vm.name,
'admin.vm.feature.List')
2017-04-15 13:53:40 +02:00
return iter(qubesd_response.decode('utf-8').splitlines())
keys = __iter__
def items(self):
'''Return iterable of pairs (feature, value)'''
for key in self:
yield key, self[key]
2017-04-15 13:53:40 +02:00
_NO_DEFAULT = object()
2017-06-14 04:04:10 +02:00
def get(self, item, default=_NO_DEFAULT):
'''Get a feature, return default value if missing.'''
try:
return self[item]
except KeyError:
if default is self._NO_DEFAULT:
raise
return default
2017-04-15 13:53:40 +02:00
def check_with_template(self, feature, default=_NO_DEFAULT):
''' Check if the vm's template has the specified feature. '''
try:
qubesd_response = self.vm.qubesd_call(
self.vm.name, 'admin.vm.feature.CheckWithTemplate', feature)
2017-04-15 13:53:40 +02:00
return qubesd_response.decode('utf-8')
except KeyError:
if default is self._NO_DEFAULT:
raise
return default