2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2011-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
'''
|
|
|
|
Qubes OS
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
:copyright: © 2010-2015 Invisible Things Lab
|
2014-11-13 18:10:27 +01:00
|
|
|
'''
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
import builtins
|
2014-12-05 14:58:05 +01:00
|
|
|
import collections
|
|
|
|
import os
|
|
|
|
import os.path
|
2017-03-28 20:43:53 +02:00
|
|
|
import string
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
import lxml.etree
|
2015-06-23 22:27:20 +02:00
|
|
|
import qubes.config
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.events
|
|
|
|
import qubes.exc
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2016-06-02 22:02:06 +02:00
|
|
|
__author__ = 'Invisible Things Lab'
|
|
|
|
__license__ = 'GPLv2 or later'
|
|
|
|
__version__ = 'R3'
|
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
class Label(object):
|
2014-11-17 19:09:25 +01:00
|
|
|
'''Label definition for virtual machines
|
|
|
|
|
|
|
|
Label specifies colour of the padlock displayed next to VM's name.
|
|
|
|
When this is a :py:class:`qubes.vm.dispvm.DispVM`, padlock is overlayed
|
|
|
|
with recycling pictogram.
|
|
|
|
|
|
|
|
:param int index: numeric identificator of label
|
|
|
|
:param str color: colour specification as in HTML (``#abcdef``)
|
|
|
|
:param str name: label's name like "red" or "green"
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __init__(self, index, color, name):
|
2014-11-17 19:09:25 +01:00
|
|
|
#: numeric identificator of label
|
|
|
|
self.index = index
|
|
|
|
|
|
|
|
#: colour specification as in HTML (``#abcdef``)
|
|
|
|
self.color = color
|
|
|
|
|
|
|
|
#: label's name like "red" or "green"
|
|
|
|
self.name = name
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: freedesktop icon name, suitable for use in
|
|
|
|
#: :py:meth:`PyQt4.QtGui.QIcon.fromTheme`
|
2014-12-05 14:58:05 +01:00
|
|
|
self.icon = 'appvm-' + name
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: freedesktop icon name, suitable for use in
|
|
|
|
#: :py:meth:`PyQt4.QtGui.QIcon.fromTheme` on DispVMs
|
2014-12-05 14:58:05 +01:00
|
|
|
self.icon_dispvm = 'dispvm-' + name
|
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2014-11-21 16:51:59 +01:00
|
|
|
@classmethod
|
|
|
|
def fromxml(cls, xml):
|
|
|
|
'''Create label definition from XML node
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
:param lxml.etree._Element xml: XML node reference
|
|
|
|
:rtype: :py:class:`qubes.Label`
|
2014-11-21 16:51:59 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
index = int(xml.get('id').split('-', 1)[1])
|
|
|
|
color = xml.get('color')
|
|
|
|
name = xml.text
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
return cls(index, color, name)
|
|
|
|
|
|
|
|
|
|
|
|
def __xml__(self):
|
2015-01-07 14:22:12 +01:00
|
|
|
element = lxml.etree.Element(
|
2015-06-23 22:46:56 +02:00
|
|
|
'label', id='label-{}'.format(self.index), color=self.color)
|
2014-12-05 14:58:05 +01:00
|
|
|
element.text = self.name
|
|
|
|
return element
|
|
|
|
|
2016-02-11 02:52:06 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2014-11-21 16:51:59 +01:00
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
def __repr__(self):
|
2015-05-31 11:10:12 +02:00
|
|
|
return '{}({!r}, {!r}, {!r})'.format(
|
2014-11-17 19:09:25 +01:00
|
|
|
self.__class__.__name__,
|
|
|
|
self.index,
|
|
|
|
self.color,
|
2014-12-05 14:58:05 +01:00
|
|
|
self.name)
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2017-03-13 15:13:20 +01:00
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Label):
|
|
|
|
return self.name == other.name
|
|
|
|
return NotImplemented
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2017-04-25 22:50:12 +02:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
@builtins.property
|
2014-11-17 19:09:25 +01:00
|
|
|
def icon_path(self):
|
|
|
|
'''Icon path
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
.. deprecated:: 2.0
|
|
|
|
use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon`
|
|
|
|
'''
|
2015-01-20 14:09:47 +01:00
|
|
|
return os.path.join(qubes.config.system_path['qubes_icon_dir'],
|
|
|
|
self.icon) + ".png"
|
2014-11-17 19:09:25 +01:00
|
|
|
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
@builtins.property
|
2014-12-05 14:58:05 +01:00
|
|
|
def icon_path_dispvm(self):
|
|
|
|
'''Icon path
|
|
|
|
|
|
|
|
.. deprecated:: 2.0
|
|
|
|
use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon_dispvm`
|
|
|
|
'''
|
2015-01-20 14:09:47 +01:00
|
|
|
return os.path.join(qubes.config.system_path['qubes_icon_dir'],
|
|
|
|
self.icon_dispvm) + ".png"
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2017-04-10 03:39:11 +02:00
|
|
|
class property(object): # pylint: disable=redefined-builtin,invalid-name
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Qubes property.
|
|
|
|
|
|
|
|
This class holds one property that can be saved to and loaded from
|
|
|
|
:file:`qubes.xml`. It is used for both global and per-VM properties.
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
Property can be unset by ordinary ``del`` statement or assigning
|
|
|
|
:py:attr:`DEFAULT` special value to it. After deletion (or before first
|
|
|
|
assignment/load) attempting to read a property will get its default value
|
|
|
|
or, when no default, py:class:`exceptions.AttributeError`.
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
:param str name: name of the property
|
2015-01-19 17:06:30 +01:00
|
|
|
:param collections.Callable setter: if not :py:obj:`None`, this is used to \
|
|
|
|
initialise value; first parameter to the function is holder instance \
|
|
|
|
and the second is value; this is called before ``type``
|
|
|
|
:param collections.Callable saver: function to coerce value to something \
|
|
|
|
readable by setter
|
2014-12-05 14:58:05 +01:00
|
|
|
:param type type: if not :py:obj:`None`, value is coerced to this type
|
2015-01-19 17:06:30 +01:00
|
|
|
:param object default: default value; if callable, will be called with \
|
|
|
|
holder as first argument
|
|
|
|
:param int load_stage: stage when property should be loaded (see \
|
|
|
|
:py:class:`Qubes` for description of stages)
|
2014-12-05 14:58:05 +01:00
|
|
|
:param int order: order of evaluation (bigger order values are later)
|
2016-02-24 01:08:32 +01:00
|
|
|
:param bool clone: :py:meth:`PropertyHolder.clone_properties` will not \
|
|
|
|
include this property by default if :py:obj:`False`
|
2015-01-19 17:06:30 +01:00
|
|
|
:param str doc: docstring; this should be one paragraph of plain RST, no \
|
|
|
|
sphinx-specific features
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
Setters and savers have following signatures:
|
|
|
|
|
|
|
|
.. :py:function:: setter(self, prop, value)
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
:param self: instance of object that is holding property
|
|
|
|
:param prop: property object
|
|
|
|
:param value: value being assigned
|
|
|
|
|
|
|
|
.. :py:function:: saver(self, prop, value)
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
:param self: instance of object that is holding property
|
|
|
|
:param prop: property object
|
|
|
|
:param value: value being saved
|
|
|
|
:rtype: str
|
|
|
|
:raises property.DontSave: when property should not be saved at all
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
#: Assigning this value to property means setting it to its default value.
|
|
|
|
#: If property has no default value, this will unset it.
|
|
|
|
DEFAULT = object()
|
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
# internal use only
|
|
|
|
_NO_DEFAULT = object()
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
def __init__(self, name, setter=None, saver=None, type=None,
|
2015-09-25 21:36:35 +02:00
|
|
|
default=_NO_DEFAULT, write_once=False, load_stage=2, order=0,
|
2016-02-24 01:08:32 +01:00
|
|
|
save_via_ref=False, clone=True,
|
2017-04-10 03:39:11 +02:00
|
|
|
doc=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=redefined-builtin
|
2014-12-05 14:58:05 +01:00
|
|
|
self.__name__ = name
|
|
|
|
self._setter = setter
|
2015-01-07 14:22:12 +01:00
|
|
|
self._saver = saver if saver is not None else (
|
|
|
|
lambda self, prop, value: str(value))
|
2017-03-11 19:04:50 +01:00
|
|
|
self.type = type
|
2014-12-05 14:58:05 +01:00
|
|
|
self._default = default
|
2015-09-25 21:36:35 +02:00
|
|
|
self._write_once = write_once
|
2014-12-05 14:58:05 +01:00
|
|
|
self.order = order
|
|
|
|
self.load_stage = load_stage
|
|
|
|
self.save_via_ref = save_via_ref
|
2016-02-24 01:08:32 +01:00
|
|
|
self.clone = clone
|
2014-12-05 14:58:05 +01:00
|
|
|
self.__doc__ = doc
|
|
|
|
self._attr_name = '_qubesprop_' + name
|
|
|
|
|
|
|
|
def __get__(self, instance, owner):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
|
|
|
|
# XXX this violates duck typing, shall we keep it?
|
|
|
|
if not isinstance(instance, PropertyHolder):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError('qubes.property should be used on '
|
|
|
|
'qubes.PropertyHolder instances only')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
return getattr(instance, self._attr_name)
|
|
|
|
|
|
|
|
except AttributeError:
|
2015-01-08 19:35:59 +01:00
|
|
|
if self._default is self._NO_DEFAULT:
|
2015-01-07 14:22:12 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'property {!r} not set'.format(self.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
elif isinstance(self._default, collections.Callable):
|
|
|
|
return self._default(instance)
|
|
|
|
else:
|
|
|
|
return self._default
|
|
|
|
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2015-09-25 21:36:35 +02:00
|
|
|
self._enforce_write_once(instance)
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
if value is self.__class__.DEFAULT:
|
|
|
|
self.__delete__(instance)
|
|
|
|
return
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
try:
|
|
|
|
oldvalue = getattr(instance, self.__name__)
|
|
|
|
has_oldvalue = True
|
|
|
|
except AttributeError:
|
|
|
|
has_oldvalue = False
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
if self._setter is not None:
|
|
|
|
value = self._setter(instance, self, value)
|
2017-03-11 19:04:50 +01:00
|
|
|
if self.type not in (None, type(value)):
|
|
|
|
value = self.type(value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if has_oldvalue:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event_pre('property-pre-set:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, newvalue=value, oldvalue=oldvalue)
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event_pre('property-pre-set:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, newvalue=value)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
instance._property_init(self, value) # pylint: disable=protected-access
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
if has_oldvalue:
|
2017-02-21 14:09:06 +01:00
|
|
|
instance.fire_event('property-set:' + self.__name__,
|
|
|
|
name=self.__name__, newvalue=value, oldvalue=oldvalue)
|
2014-12-09 18:34:00 +01:00
|
|
|
else:
|
2017-02-21 14:09:06 +01:00
|
|
|
instance.fire_event('property-set:' + self.__name__,
|
|
|
|
name=self.__name__, newvalue=value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __delete__(self, instance):
|
2015-09-25 21:36:35 +02:00
|
|
|
self._enforce_write_once(instance)
|
|
|
|
|
2015-01-08 17:45:34 +01:00
|
|
|
try:
|
2016-09-08 04:13:02 +02:00
|
|
|
oldvalue = getattr(instance, self._attr_name)
|
2015-01-08 17:45:34 +01:00
|
|
|
has_oldvalue = True
|
|
|
|
except AttributeError:
|
|
|
|
has_oldvalue = False
|
|
|
|
|
|
|
|
if has_oldvalue:
|
2016-03-04 18:04:39 +01:00
|
|
|
instance.fire_event_pre('property-pre-del:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, oldvalue=oldvalue)
|
2016-04-06 18:09:57 +02:00
|
|
|
delattr(instance, self._attr_name)
|
2016-03-04 18:04:39 +01:00
|
|
|
instance.fire_event('property-del:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__, oldvalue=oldvalue)
|
2016-04-06 18:09:57 +02:00
|
|
|
|
2015-01-08 17:45:34 +01:00
|
|
|
else:
|
2016-04-06 18:09:57 +02:00
|
|
|
instance.fire_event_pre('property-pre-del:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__)
|
2016-03-04 18:04:39 +01:00
|
|
|
instance.fire_event('property-del:' + self.__name__,
|
2017-02-21 14:09:06 +01:00
|
|
|
name=self.__name__)
|
2015-01-08 17:45:34 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2015-07-03 17:06:24 +02:00
|
|
|
default = ' default={!r}'.format(self._default) \
|
|
|
|
if self._default is not self._NO_DEFAULT \
|
|
|
|
else ''
|
|
|
|
return '<{} object at {:#x} name={!r}{}>'.format(
|
2017-05-19 18:42:03 +02:00
|
|
|
self.__class__.__name__, id(self), self.__name__, default)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2017-05-19 18:42:03 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.__name__
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.__name__)
|
|
|
|
|
2017-02-19 00:04:23 +01:00
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, property):
|
2017-02-23 22:30:01 +01:00
|
|
|
return (self.load_stage, self.order, self.__name__) <\
|
|
|
|
(other.load_stage, other.order, other.__name__)
|
|
|
|
return NotImplemented
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2017-02-27 21:57:56 +01:00
|
|
|
if isinstance(other, str):
|
|
|
|
return self.__name__ == other
|
2016-02-10 16:49:46 +01:00
|
|
|
return isinstance(other, property) and self.__name__ == other.__name__
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
def _enforce_write_once(self, instance):
|
|
|
|
if self._write_once and not instance.property_is_default(self):
|
|
|
|
raise AttributeError(
|
|
|
|
'property {!r} is write-once and already set'.format(
|
|
|
|
self.__name__))
|
|
|
|
|
2017-03-28 20:43:53 +02:00
|
|
|
def sanitize(self, *, untrusted_newvalue):
|
|
|
|
'''Coarse sanitization of value to be set, before sending it to a
|
|
|
|
setter. Can raise QubesValueError if the value is invalid.
|
|
|
|
|
|
|
|
:param untrusted_newvalue: value to be validated
|
|
|
|
:return sanitized value
|
|
|
|
:raises qubes.exc.QubesValueError
|
|
|
|
'''
|
|
|
|
# do not treat type='str' as sufficient validation
|
|
|
|
if self.type is not None and self.type is not str:
|
|
|
|
# assume specific type will preform enough validation
|
|
|
|
if self.type is bool:
|
|
|
|
try:
|
|
|
|
untrusted_newvalue = untrusted_newvalue.decode('ascii')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
raise qubes.exc.QubesValueError
|
|
|
|
return self.bool(None, None, untrusted_newvalue)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return self.type(untrusted_newvalue)
|
|
|
|
except ValueError:
|
|
|
|
raise qubes.exc.QubesValueError
|
|
|
|
else:
|
|
|
|
# 'str' or not specified type
|
|
|
|
try:
|
|
|
|
untrusted_newvalue = untrusted_newvalue.decode('ascii',
|
|
|
|
errors='strict')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
raise qubes.exc.QubesValueError
|
|
|
|
allowed_set = string.printable
|
|
|
|
if not all(x in allowed_set for x in untrusted_newvalue):
|
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'Invalid characters in property value')
|
|
|
|
return untrusted_newvalue
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# exceptions
|
|
|
|
#
|
|
|
|
|
|
|
|
class DontSave(Exception):
|
2015-09-17 12:08:03 +02:00
|
|
|
'''This exception may be raised from saver to sign that property should
|
2014-12-29 12:46:16 +01:00
|
|
|
not be saved.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dontsave(self, prop, value):
|
|
|
|
'''Dummy saver that never saves anything.'''
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=bad-staticmethod-argument,unused-argument
|
2015-01-20 14:09:47 +01:00
|
|
|
raise property.DontSave()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
#
|
|
|
|
# some setters provided
|
|
|
|
#
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def forbidden(self, prop, value):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Property setter that forbids loading a property.
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
This is used to effectively disable property in classes which inherit
|
|
|
|
unwanted property. When someone attempts to load such a property, it
|
|
|
|
|
|
|
|
:throws AttributeError: always
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=bad-staticmethod-argument,unused-argument
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'setting {} property on {} instance is forbidden'.format(
|
|
|
|
prop.__name__, self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
@staticmethod
|
|
|
|
def bool(self, prop, value):
|
|
|
|
'''Property setter for boolean properties.
|
|
|
|
|
|
|
|
It accepts (case-insensitive) ``'0'``, ``'no'`` and ``false`` as
|
|
|
|
:py:obj:`False` and ``'1'``, ``'yes'`` and ``'true'`` as
|
|
|
|
:py:obj:`True`.
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=bad-staticmethod-argument,unused-argument
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
if isinstance(value, str):
|
2015-09-25 21:49:59 +02:00
|
|
|
lcvalue = value.lower()
|
|
|
|
if lcvalue in ('0', 'no', 'false', 'off'):
|
|
|
|
return False
|
|
|
|
if lcvalue in ('1', 'yes', 'true', 'on'):
|
|
|
|
return True
|
2017-03-15 18:23:09 +01:00
|
|
|
raise qubes.exc.QubesValueError(
|
2015-09-25 21:49:59 +02:00
|
|
|
'Invalid literal for boolean property: {!r}'.format(value))
|
|
|
|
|
|
|
|
return bool(value)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2017-04-10 04:02:13 +02:00
|
|
|
def stateless_property(func):
|
|
|
|
'''Decorator similar to :py:class:`builtins.property`, but for properties
|
|
|
|
exposed through management API (including qvm-prefs etc)'''
|
|
|
|
return property(func.__name__,
|
|
|
|
setter=property.forbidden,
|
|
|
|
saver=property.DontSave,
|
|
|
|
default=func,
|
|
|
|
doc=func.__doc__)
|
|
|
|
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
class PropertyHolder(qubes.events.Emitter):
|
|
|
|
'''Abstract class for holding :py:class:`qubes.property`
|
|
|
|
|
|
|
|
Events fired by instances of this class:
|
|
|
|
|
|
|
|
.. event:: property-load (subject, event)
|
|
|
|
|
|
|
|
Fired once after all properties are loaded from XML. Individual
|
|
|
|
``property-set`` events are not fired.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-set:<propname> \
|
|
|
|
(subject, event, name, newvalue[, oldvalue])
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
Fired when property changes state. Signature is variable,
|
|
|
|
*oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param newvalue: New value of the property
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-pre-set:<propname> \
|
|
|
|
(subject, event, name, newvalue[, oldvalue])
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
Fired before property changes state. Signature is variable,
|
|
|
|
*oldvalue* is present only if there was an old value.
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param newvalue: New value of the property
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-del:<propname> \
|
|
|
|
(subject, event, name[, oldvalue])
|
2015-01-08 17:45:34 +01:00
|
|
|
|
|
|
|
Fired when property gets deleted (is set to default). Signature is
|
|
|
|
variable, *oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-pre-del:<propname> \
|
|
|
|
(subject, event, name[, oldvalue])
|
2015-01-08 17:45:34 +01:00
|
|
|
|
|
|
|
Fired before property gets deleted (is set to default). Signature
|
|
|
|
is variable, *oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2016-10-25 17:11:38 +02:00
|
|
|
.. event:: clone-properties (subject, event, src, proplist)
|
|
|
|
|
|
|
|
:param src: object, from which we are cloning
|
|
|
|
:param proplist: list of properties
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
Members:
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
def __init__(self, xml, **kwargs):
|
2014-12-05 14:58:05 +01:00
|
|
|
self.xml = xml
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
propvalues = {}
|
2015-01-15 12:57:44 +01:00
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
all_names = set(prop.__name__ for prop in self.property_list())
|
2016-07-13 20:38:46 +02:00
|
|
|
for key in list(kwargs):
|
2015-01-21 15:24:29 +01:00
|
|
|
if not key in all_names:
|
|
|
|
continue
|
2015-09-25 21:36:35 +02:00
|
|
|
propvalues[key] = kwargs.pop(key)
|
|
|
|
|
|
|
|
super(PropertyHolder, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
for key, value in propvalues.items():
|
|
|
|
setattr(self, key, value)
|
2015-01-21 15:24:29 +01:00
|
|
|
|
2016-06-26 02:18:13 +02:00
|
|
|
if self.xml is not None:
|
|
|
|
# check if properties are appropriate
|
|
|
|
all_names = set(prop.__name__ for prop in self.property_list())
|
|
|
|
|
|
|
|
for node in self.xml.xpath('./properties/property'):
|
|
|
|
name = node.get('name')
|
|
|
|
if name not in all_names:
|
|
|
|
raise TypeError(
|
|
|
|
'property {!r} not applicable to {!r}'.format(
|
|
|
|
name, self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-09 15:09:56 +01:00
|
|
|
@classmethod
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_list(cls, load_stage=None):
|
2015-01-09 15:09:56 +01:00
|
|
|
'''List all properties attached to this VM's class
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
:param load_stage: Filter by load stage
|
|
|
|
:type load_stage: :py:func:`int` or :py:obj:`None`
|
|
|
|
'''
|
|
|
|
|
|
|
|
props = set()
|
2015-01-09 15:09:56 +01:00
|
|
|
for class_ in cls.__mro__:
|
2014-12-05 14:58:05 +01:00
|
|
|
props.update(prop for prop in class_.__dict__.values()
|
|
|
|
if isinstance(prop, property))
|
|
|
|
if load_stage is not None:
|
|
|
|
props = set(prop for prop in props
|
|
|
|
if prop.load_stage == load_stage)
|
2017-02-23 22:30:01 +01:00
|
|
|
return sorted(props)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
def _property_init(self, prop, value):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Initialise property to a given value, without side effects.
|
|
|
|
|
|
|
|
:param qubes.property prop: property object of particular interest
|
|
|
|
:param value: value
|
|
|
|
'''
|
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-21 12:50:00 +01:00
|
|
|
setattr(self, self.property_get_def(prop)._attr_name, value)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def property_is_default(self, prop):
|
|
|
|
'''Check whether property is in it's default value.
|
|
|
|
|
|
|
|
Properties when unset may return some default value, so
|
|
|
|
``hasattr(vm, prop.__name__)`` is wrong in some circumstances. This
|
|
|
|
method allows for checking if the value returned is in fact it's
|
|
|
|
default value.
|
|
|
|
|
|
|
|
:param qubes.property prop: property object of particular interest
|
|
|
|
:rtype: bool
|
2015-07-03 17:06:24 +02:00
|
|
|
''' # pylint: disable=protected-access
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2015-07-03 17:06:24 +02:00
|
|
|
# both property_get_def() and ._attr_name may throw AttributeError,
|
|
|
|
# which we don't want to catch
|
|
|
|
attrname = self.property_get_def(prop)._attr_name
|
|
|
|
return not hasattr(self, attrname)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
2015-01-09 15:09:56 +01:00
|
|
|
@classmethod
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_get_def(cls, prop):
|
2015-01-08 19:35:59 +01:00
|
|
|
'''Return property definition object.
|
|
|
|
|
|
|
|
If prop is already :py:class:`qubes.property` instance, return the same
|
|
|
|
object.
|
|
|
|
|
|
|
|
:param prop: property object or name
|
|
|
|
:type prop: qubes.property or str
|
|
|
|
:rtype: qubes.property
|
|
|
|
'''
|
|
|
|
|
|
|
|
if isinstance(prop, qubes.property):
|
|
|
|
return prop
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
for p in cls.property_list():
|
2015-01-08 19:35:59 +01:00
|
|
|
if p.__name__ == prop:
|
|
|
|
return p
|
|
|
|
|
|
|
|
raise AttributeError('No property {!r} found in {!r}'.format(
|
2015-01-09 15:09:56 +01:00
|
|
|
prop, cls))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load_properties(self, load_stage=None):
|
|
|
|
'''Load properties from immediate children of XML node.
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
``property-set`` events are not fired for each individual property.
|
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
:param int load_stage: Stage of loading.
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-12-23 14:17:23 +01:00
|
|
|
if self.xml is None:
|
|
|
|
return
|
2015-01-07 14:22:12 +01:00
|
|
|
all_names = set(
|
2015-01-21 12:50:00 +01:00
|
|
|
prop.__name__ for prop in self.property_list(load_stage))
|
2014-12-05 14:58:05 +01:00
|
|
|
for node in self.xml.xpath('./properties/property'):
|
|
|
|
name = node.get('name')
|
|
|
|
value = node.get('ref') or node.text
|
|
|
|
|
|
|
|
if not name in all_names:
|
2015-01-21 15:24:29 +01:00
|
|
|
continue
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
setattr(self, name, value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
def xml_properties(self, with_defaults=False):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Iterator that yields XML nodes representing set properties.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool with_defaults: If :py:obj:`True`, then it also includes \
|
|
|
|
properties which were not set explicite, but have default values \
|
|
|
|
filled.
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
properties = lxml.etree.Element('properties')
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
for prop in self.property_list():
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2014-12-05 14:58:05 +01:00
|
|
|
try:
|
2015-01-07 14:22:12 +01:00
|
|
|
value = getattr(
|
|
|
|
self, (prop.__name__ if with_defaults else prop._attr_name))
|
2015-01-20 14:41:19 +01:00
|
|
|
except AttributeError:
|
2014-12-05 14:58:05 +01:00
|
|
|
continue
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
|
|
|
value = prop._saver(self, prop, value)
|
|
|
|
except property.DontSave:
|
|
|
|
continue
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
element = lxml.etree.Element('property', name=prop.__name__)
|
|
|
|
if prop.save_via_ref:
|
|
|
|
element.set('ref', value)
|
|
|
|
else:
|
|
|
|
element.text = value
|
|
|
|
properties.append(element)
|
|
|
|
|
|
|
|
return properties
|
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
# this was clone_attrs
|
|
|
|
def clone_properties(self, src, proplist=None):
|
|
|
|
'''Clone properties from other object.
|
|
|
|
|
|
|
|
:param PropertyHolder src: source object
|
2015-01-19 17:06:30 +01:00
|
|
|
:param list proplist: list of properties \
|
2016-02-24 01:08:32 +01:00
|
|
|
(:py:obj:`None` or omit for all properties except those with \
|
|
|
|
:py:attr:`property.clone` set to :py:obj:`False`)
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if proplist is None:
|
2016-02-24 01:08:32 +01:00
|
|
|
proplist = [prop for prop in self.property_list()
|
|
|
|
if prop.clone]
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
2015-01-21 12:50:00 +01:00
|
|
|
proplist = [prop for prop in self.property_list()
|
2014-12-29 12:46:16 +01:00
|
|
|
if prop.__name__ in proplist or prop in proplist]
|
|
|
|
|
2016-02-10 16:49:08 +01:00
|
|
|
for prop in proplist:
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-21 12:50:00 +01:00
|
|
|
self._property_init(prop, getattr(src, prop._attr_name))
|
2014-12-29 12:46:16 +01:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
2017-02-21 14:09:06 +01:00
|
|
|
self.fire_event('clone-properties', src=src, proplist=proplist)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_require(self, prop, allow_none=False, hard=False):
|
2015-01-08 19:35:59 +01:00
|
|
|
'''Complain badly when property is not set.
|
|
|
|
|
|
|
|
:param prop: property name or object
|
|
|
|
:type prop: qubes.property or str
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool allow_none: if :py:obj:`True`, don't complain if \
|
|
|
|
:py:obj:`None` is found
|
|
|
|
:param bool hard: if :py:obj:`True`, raise :py:class:`AssertionError`; \
|
|
|
|
if :py:obj:`False`, log warning instead
|
2015-01-08 19:35:59 +01:00
|
|
|
'''
|
|
|
|
|
2015-06-24 15:45:19 +02:00
|
|
|
if isinstance(prop, qubes.property):
|
2015-01-08 19:35:59 +01:00
|
|
|
prop = prop.__name__
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = getattr(self, prop)
|
|
|
|
if value is None and not allow_none:
|
|
|
|
raise AttributeError()
|
|
|
|
except AttributeError:
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2015-01-08 19:35:59 +01:00
|
|
|
msg = 'Required property {!r} not set on {!r}'.format(prop, self)
|
|
|
|
if hard:
|
|
|
|
raise AssertionError(msg)
|
|
|
|
else:
|
2015-01-22 11:24:23 +01:00
|
|
|
# pylint: disable=no-member
|
|
|
|
self.log.fatal(msg)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2016-06-02 22:02:06 +02:00
|
|
|
# pylint: disable=wrong-import-position
|
2016-04-28 16:00:29 +02:00
|
|
|
from qubes.vm import VMProperty
|
|
|
|
from qubes.app import Qubes
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2016-04-28 16:00:29 +02:00
|
|
|
__all__ = [
|
|
|
|
'Label',
|
|
|
|
'PropertyHolder',
|
|
|
|
'Qubes',
|
|
|
|
'VMProperty',
|
|
|
|
'property',
|
|
|
|
]
|