mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-02-02 09:47:16 +03:00
virtinst: Drop a bunch of uses of xmlbuilder make_xpath_cb
I'd like to drop that functionality from xmlbuilder, and just open code it at the callers... this is most of the way there
This commit is contained in:
parent
ebcb7c064c
commit
2d54949719
@ -158,6 +158,11 @@ class _VirtualCharDevice(VirtualDevice):
|
||||
return self.type in users[propname]
|
||||
return hasattr(self, propname)
|
||||
|
||||
def set_defaults(self, guest):
|
||||
if not self.source_host and self.supports_property("source_host"):
|
||||
self.source_host = "127.0.0.1"
|
||||
|
||||
|
||||
def _set_host_helper(self, hostparam, portparam, val):
|
||||
def parse_host(val):
|
||||
host, ignore, port = (val or "").partition(":")
|
||||
@ -180,7 +185,8 @@ class _VirtualCharDevice(VirtualDevice):
|
||||
_XML_PROP_ORDER = ["type", "_has_mode_bind", "_has_mode_connect",
|
||||
"bind_host", "bind_port",
|
||||
"source_mode", "_source_path", "source_channel",
|
||||
"source_host", "source_port",
|
||||
"_source_connect_host", "_source_bind_host",
|
||||
"_source_connect_port", "_source_bind_port",
|
||||
"target_type", "target_name"]
|
||||
|
||||
type = XMLProperty(
|
||||
@ -203,6 +209,20 @@ class _VirtualCharDevice(VirtualDevice):
|
||||
source_channel = XMLProperty(xpath="./source/@channel",
|
||||
doc=_("Source channel name."))
|
||||
|
||||
|
||||
########################
|
||||
# source mode handling #
|
||||
########################
|
||||
|
||||
def _get_mode_for_xml_prop(self):
|
||||
mode = self.source_mode
|
||||
if not mode:
|
||||
# If we are parsing XML, source_mode may be empty
|
||||
mode = self._get_default_source_mode()
|
||||
if not mode:
|
||||
mode = self.MODE_CONNECT
|
||||
return mode
|
||||
|
||||
def _get_default_source_mode(self):
|
||||
if self.type == self.TYPE_UDP:
|
||||
return self.MODE_CONNECT
|
||||
@ -218,33 +238,58 @@ class _VirtualCharDevice(VirtualDevice):
|
||||
make_xpath_cb=_make_sourcemode_xpath,
|
||||
default_cb=_get_default_source_mode)
|
||||
|
||||
def _get_default_sourcehost(self):
|
||||
if not self.supports_property("source_host"):
|
||||
return None
|
||||
return "127.0.0.1"
|
||||
def _set_source_validate(self, val):
|
||||
if val is None or self.type != self.TYPE_UDP:
|
||||
return val
|
||||
if not self._has_mode_connect:
|
||||
self._has_mode_connect = self.MODE_CONNECT
|
||||
return val
|
||||
def _make_sourcehost_xpath(self):
|
||||
mode = self.source_mode
|
||||
if self.type == self.TYPE_UDP:
|
||||
mode = "connect"
|
||||
return "./source[@mode='%s']/@host" % mode
|
||||
source_host = XMLProperty(name="char sourcehost",
|
||||
doc=_("Address to connect/listen to."),
|
||||
make_xpath_cb=_make_sourcehost_xpath,
|
||||
default_cb=_get_default_sourcehost,
|
||||
set_converter=_set_source_validate)
|
||||
|
||||
def _make_sourceport_xpath(self):
|
||||
return "./source[@mode='%s']/@service" % self.source_mode
|
||||
source_port = XMLProperty(name="char sourceport",
|
||||
doc=_("Port on target host to connect/listen to."),
|
||||
make_xpath_cb=_make_sourceport_xpath,
|
||||
set_converter=_set_source_validate, is_int=True)
|
||||
########################
|
||||
# source host handling #
|
||||
########################
|
||||
|
||||
_source_connect_host = XMLProperty("./source[@mode='connect']/@host")
|
||||
_source_bind_host = XMLProperty("./source[@mode='bind']/@host")
|
||||
|
||||
def _set_source_host(self, val):
|
||||
if (val and
|
||||
self.type == self.TYPE_UDP and
|
||||
not self._has_mode_connect):
|
||||
self._has_mode_connect = self.MODE_CONNECT
|
||||
|
||||
if self._get_mode_for_xml_prop() == self.MODE_CONNECT:
|
||||
self._source_connect_host = val
|
||||
else:
|
||||
self._source_bind_host = val
|
||||
def _get_source_host(self):
|
||||
if self._get_mode_for_xml_prop() == self.MODE_CONNECT:
|
||||
return self._source_connect_host
|
||||
else:
|
||||
return self._source_bind_host
|
||||
source_host = property(_get_source_host, _set_source_host,
|
||||
doc=_("Address to connect/listen to."))
|
||||
|
||||
|
||||
########################
|
||||
# source port handling #
|
||||
########################
|
||||
|
||||
_source_connect_port = XMLProperty("./source[@mode='connect']/@service",
|
||||
is_int=True)
|
||||
_source_bind_port = XMLProperty("./source[@mode='bind']/@service",
|
||||
is_int=True)
|
||||
def _set_source_port(self, val):
|
||||
if self._get_mode_for_xml_prop() == self.MODE_CONNECT:
|
||||
self._source_connect_port = val
|
||||
else:
|
||||
self._source_bind_port = val
|
||||
def _get_source_port(self):
|
||||
if self._get_mode_for_xml_prop() == self.MODE_CONNECT:
|
||||
return self._source_connect_port
|
||||
else:
|
||||
return self._source_bind_port
|
||||
source_port = property(_get_source_port, _set_source_port,
|
||||
doc=_("Port on target host to connect/listen to."))
|
||||
|
||||
|
||||
#######################
|
||||
# Remaining XML props #
|
||||
#######################
|
||||
|
||||
_has_mode_connect = XMLProperty("./source[@mode='connect']/@mode")
|
||||
_has_mode_bind = XMLProperty("./source[@mode='bind']/@mode")
|
||||
|
@ -51,26 +51,6 @@ class VirtualFilesystem(VirtualDevice):
|
||||
DRIVER_DEFAULT = "default"
|
||||
DRIVERS = [DRIVER_PATH, DRIVER_HANDLE, DRIVER_LOOP, DRIVER_NBD, DRIVER_DEFAULT]
|
||||
|
||||
@staticmethod
|
||||
def type_to_source_prop(fs_type):
|
||||
"""
|
||||
Convert a value of VirtualFilesystem.type to it's associated XML
|
||||
source @prop name
|
||||
"""
|
||||
if (fs_type == VirtualFilesystem.TYPE_MOUNT or
|
||||
fs_type == VirtualFilesystem.TYPE_DEFAULT or
|
||||
fs_type is None):
|
||||
return "dir"
|
||||
elif fs_type == VirtualFilesystem.TYPE_TEMPLATE:
|
||||
return "name"
|
||||
elif fs_type == VirtualFilesystem.TYPE_FILE:
|
||||
return "file"
|
||||
elif fs_type == VirtualFilesystem.TYPE_BLOCK:
|
||||
return "dev"
|
||||
elif fs_type == VirtualFilesystem.TYPE_RAM:
|
||||
return "usage"
|
||||
return "dir"
|
||||
|
||||
|
||||
type = XMLProperty("./@type",
|
||||
default_cb=lambda s: None,
|
||||
@ -90,11 +70,6 @@ class VirtualFilesystem(VirtualDevice):
|
||||
|
||||
units = XMLProperty("./source/@units")
|
||||
|
||||
def _make_source_xpath(self):
|
||||
return "./source/@" + self.type_to_source_prop(self.type)
|
||||
source = XMLProperty(name="filesystem source",
|
||||
make_xpath_cb=_make_source_xpath)
|
||||
|
||||
def _validate_set_target(self, val):
|
||||
# In case of qemu for default fs type (mount) target is not
|
||||
# actually a directory, it is merely a arbitrary string tag
|
||||
@ -111,5 +86,28 @@ class VirtualFilesystem(VirtualDevice):
|
||||
target = XMLProperty("./target/@dir",
|
||||
set_converter=_validate_set_target)
|
||||
|
||||
_source_dir = XMLProperty("./source/@dir")
|
||||
_source_name = XMLProperty("./source/@name")
|
||||
_source_file = XMLProperty("./source/@file")
|
||||
_source_dev = XMLProperty("./source/@dev")
|
||||
_source_usage = XMLProperty("./source/@usage")
|
||||
def _type_to_source_prop(self):
|
||||
if self.type == VirtualFilesystem.TYPE_TEMPLATE:
|
||||
return "_source_name"
|
||||
elif self.type == VirtualFilesystem.TYPE_FILE:
|
||||
return "_source_file"
|
||||
elif self.type == VirtualFilesystem.TYPE_BLOCK:
|
||||
return "_source_dev"
|
||||
elif self.type == VirtualFilesystem.TYPE_RAM:
|
||||
return "_source_usage"
|
||||
else:
|
||||
return "_source_dir"
|
||||
|
||||
def _get_source(self):
|
||||
return getattr(self, self._type_to_source_prop())
|
||||
def _set_source(self, val):
|
||||
return setattr(self, self._type_to_source_prop(), val)
|
||||
source = property(_get_source, _set_source)
|
||||
|
||||
|
||||
VirtualFilesystem.register_type()
|
||||
|
@ -329,13 +329,20 @@ class StoragePool(_StorageObject):
|
||||
self._random_uuid = util.generate_uuid(self.conn)
|
||||
return self._random_uuid
|
||||
|
||||
def _make_source_xpath(self):
|
||||
def _type_to_source_prop(self):
|
||||
if (self.type == self.TYPE_NETFS or
|
||||
self.type == self.TYPE_GLUSTER):
|
||||
return "./source/dir/@path"
|
||||
if self.type == self.TYPE_SCSI:
|
||||
return "./source/adapter/@name"
|
||||
return "./source/device/@path"
|
||||
return "_source_dir"
|
||||
elif self.type == self.TYPE_SCSI:
|
||||
return "_source_adapter"
|
||||
else:
|
||||
return "_source_device"
|
||||
|
||||
def _get_source(self):
|
||||
return getattr(self, self._type_to_source_prop())
|
||||
def _set_source(self, val):
|
||||
return setattr(self, self._type_to_source_prop(), val)
|
||||
source_path = property(_get_source, _set_source)
|
||||
|
||||
def _default_source_name(self):
|
||||
if not self.supports_property("source_name"):
|
||||
@ -371,9 +378,15 @@ class StoragePool(_StorageObject):
|
||||
_XML_PROP_ORDER = ["name", "type", "uuid",
|
||||
"capacity", "allocation", "available",
|
||||
"format", "hosts",
|
||||
"source_path", "source_name", "target_path",
|
||||
"_source_dir", "_source_adapter", "_source_device",
|
||||
"source_name", "target_path",
|
||||
"permissions"]
|
||||
|
||||
|
||||
_source_dir = XMLProperty("./source/dir/@path")
|
||||
_source_adapter = XMLProperty("./source/adapter/@name")
|
||||
_source_device = XMLProperty("./source/device/@path")
|
||||
|
||||
type = XMLProperty("./@type",
|
||||
doc=_("Storage device type the pool will represent."))
|
||||
uuid = XMLProperty("./uuid",
|
||||
@ -388,8 +401,6 @@ class StoragePool(_StorageObject):
|
||||
default_cb=_default_format_cb)
|
||||
iqn = XMLProperty("./source/initiator/iqn/@name",
|
||||
doc=_("iSCSI initiator qualified name"))
|
||||
source_path = XMLProperty(name="source path",
|
||||
make_xpath_cb=_make_source_xpath)
|
||||
source_name = XMLProperty("./source/name",
|
||||
default_cb=_default_source_name,
|
||||
doc=_("Name of the Volume Group"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user