Add property_get_default method

It is useful to get property default value, without changing it to
default. For example to list it in GUI tools

Fixes QubesOS/qubes-issues#3197
This commit is contained in:
Marek Marczykowski-Górecki 2018-01-12 23:33:32 +01:00
parent 3fcae995df
commit ce0d230866
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 56 additions and 1 deletions

View File

@ -154,6 +154,25 @@ class PropertyHolder(object):
assert isinstance(is_default, bool)
return is_default
def property_get_default(self, item):
'''
Get default property value, regardless of the current value
:param str item: name of property
:return: default value
'''
if item.startswith('_'):
raise AttributeError(item)
property_str = self.qubesd_call(
self._method_dest,
self._method_prefix + 'GetDefault',
item,
None)
if not property_str:
raise AttributeError(item + ' has no default')
(prop_type, value) = property_str.split(b' ', 1)
return self._parse_type_value(prop_type, value)
def clone_properties(self, src, proplist=None):
'''Clone properties from other object.
@ -172,7 +191,6 @@ class PropertyHolder(object):
continue
def __getattr__(self, item):
# pylint: disable=too-many-return-statements
if item.startswith('_'):
raise AttributeError(item)
try:
@ -184,6 +202,19 @@ class PropertyHolder(object):
except qubesadmin.exc.QubesDaemonNoResponseError:
raise qubesadmin.exc.QubesPropertyAccessError(item)
(_default, prop_type, value) = property_str.split(b' ', 2)
return self._parse_type_value(prop_type, value)
def _parse_type_value(self, prop_type, value):
'''
Parse `type=... ...` qubesd response format. Return a value of
appropriate type.
:param bytes prop_type: 'type=...' part of the response (including
`type=` prefix)
:param bytes value: 'value' part of the response
:return: parsed value
'''
# pylint: disable=too-many-return-statements
prop_type = prop_type.decode('ascii')
if not prop_type.startswith('type='):
raise qubesadmin.exc.QubesDaemonCommunicationError(

View File

@ -164,6 +164,30 @@ class TC_00_Properties(qubesadmin.tests.vm.VMTestCase):
del self.vm.prop1
self.assertAllCalled()
def test_040_get_default(self):
self.app.expected_calls[
('test-vm', 'admin.vm.property.GetDefault', 'prop1', None)] = \
b'0\x00type=str some value'
default_value = self.vm.property_get_default('prop1')
self.assertEqual(default_value, 'some value')
self.assertAllCalled()
def test_041_get_default_int(self):
self.app.expected_calls[
('test-vm', 'admin.vm.property.GetDefault', 'prop1', None)] = \
b'0\x00type=int 42'
default_value = self.vm.property_get_default('prop1')
self.assertEqual(default_value, 42)
self.assertAllCalled()
def test_042_get_default_none(self):
self.app.expected_calls[
('test-vm', 'admin.vm.property.GetDefault', 'prop1', None)] = \
b'0\x00'
with self.assertRaises(AttributeError):
self.vm.property_get_default('prop1')
self.assertAllCalled()
class TC_01_SpecialCases(qubesadmin.tests.vm.VMTestCase):
def test_000_get_name(self):