From 98a4d86d7f8e7386e70c5c7d61623e6fbc8e34c4 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 15 Jul 2013 12:57:37 -0400 Subject: [PATCH] VirtualRedir: Convert to new style XML props --- tests/clitest.py | 4 - tests/xmlconfig-xml/boot-many-devices.xml | 4 + tests/xmlconfig.py | 9 +++ tests/xmlparse-xml/change-redirdev-in.xml | 2 +- tests/xmlparse.py | 5 +- virtManager/addhardware.py | 3 +- virtinst/VirtualRedirDevice.py | 96 +++++------------------ virtinst/cli.py | 24 +++--- 8 files changed, 52 insertions(+), 95 deletions(-) diff --git a/tests/clitest.py b/tests/clitest.py index 89928dc2..166213b2 100644 --- a/tests/clitest.py +++ b/tests/clitest.py @@ -640,10 +640,6 @@ vinst.add_valid("redirdev", "--redirdev usb,type=tcp,server=127.0.0.1:4002") # vinst.add_invalid("redirdev", "--redirdev") # Missing argument vinst.add_invalid("redirdev", "--redirdev pci") # Unsupported bus vinst.add_invalid("redirdev", "--redirdev usb,type=spicevmc,server=foo:12") # Invalid argument -vinst.add_invalid("redirdev", "--redirdev usb,type=tcp,server=") # Missing argument -vinst.add_invalid("redirdev", "--redirdev usb,type=tcp,server=localhost:p4000") # Invalid address -vinst.add_invalid("redirdev", "--redirdev usb,type=tcp,server=localhost:") # Missing address -vinst.add_invalid("redirdev", "--redirdev usb,type=tcp,server=:399") # Missing host vinst.add_category("hostdev", "--noautoconsole --nographics --nodisks --pxe") diff --git a/tests/xmlconfig-xml/boot-many-devices.xml b/tests/xmlconfig-xml/boot-many-devices.xml index fa05675a..dffddbf2 100644 --- a/tests/xmlconfig-xml/boot-many-devices.xml +++ b/tests/xmlconfig-xml/boot-many-devices.xml @@ -94,6 +94,10 @@ + + + + diff --git a/tests/xmlconfig.py b/tests/xmlconfig.py index 58ea49ea..88dbcbf5 100644 --- a/tests/xmlconfig.py +++ b/tests/xmlconfig.py @@ -852,6 +852,15 @@ class TestXMLConfig(unittest.TestCase): g.seclabel.label = "foolabel" g.seclabel.imagelabel = "imagelabel" + redir1 = virtinst.VirtualRedirDevice(g.conn) + redir1.type = "spicevmc" + + redir2 = virtinst.VirtualRedirDevice(g.conn) + redir2.type = "tcp" + redir2.parse_friendly_server("foobar.com:1234") + g.add_device(redir1) + g.add_device(redir2) + self._compare(g, "boot-many-devices", False) def testCpuset(self): diff --git a/tests/xmlparse-xml/change-redirdev-in.xml b/tests/xmlparse-xml/change-redirdev-in.xml index 4b8a12a6..9c0bb861 100644 --- a/tests/xmlparse-xml/change-redirdev-in.xml +++ b/tests/xmlparse-xml/change-redirdev-in.xml @@ -32,6 +32,6 @@ - + diff --git a/tests/xmlparse.py b/tests/xmlparse.py index 809dc3cd..f7a010b1 100644 --- a/tests/xmlparse.py +++ b/tests/xmlparse.py @@ -691,11 +691,12 @@ class XMLParseTest(unittest.TestCase): dev2 = guest.get_devices("redirdev")[1] check = self._make_checker(dev1) + check("bus", "usb", "baz", "usb") check("host", "foo", "bar") - check("service", "12", "42") + check("service", 12, 42) check = self._make_checker(dev2) - check("type", "spicevmc") + check("type", "tcp", "spicevmc") self._alter_compare(guest.get_xml_config(), outfile) diff --git a/virtManager/addhardware.py b/virtManager/addhardware.py index a0512e06..59d2078b 100644 --- a/virtManager/addhardware.py +++ b/virtManager/addhardware.py @@ -1601,7 +1601,8 @@ class vmmAddHardware(vmmGObjectUI): service = self.get_config_usbredir_service() try: - self._dev = VirtualRedirDevice(conn, bus="usb", stype=stype) + self._dev = VirtualRedirDevice(conn) + self._dev.type = stype if host: self._dev.host = host if service: diff --git a/virtinst/VirtualRedirDevice.py b/virtinst/VirtualRedirDevice.py index c5850ec2..98830107 100644 --- a/virtinst/VirtualRedirDevice.py +++ b/virtinst/VirtualRedirDevice.py @@ -28,83 +28,27 @@ class VirtualRedirDevice(VirtualDevice): _virtual_device_type = VirtualDevice.VIRTUAL_DEV_REDIRDEV - BUS_DEFAULT = "usb" - _buses = ["usb"] + BUS_DEFAULT = "default" + BUSES = ["usb"] - TYPE_DEFAULT = "spicevmc" - _types = ["tcp", "spicevmc", None] - - def __init__(self, conn, bus=BUS_DEFAULT, stype=TYPE_DEFAULT, - parsexml=None, parsexmlnode=None): - VirtualDevice.__init__(self, conn, parsexml, parsexmlnode) - - self._type = None - self._bus = None - self._host = None - self._service = None - if self._is_parse(): - return - - self.bus = bus - self.type = stype - - def get_buses(self): - return self._buses[:] - buses = property(get_buses) - - def get_bus(self): - return self._bus - def set_bus(self, new_val): - if new_val not in self.buses: - raise ValueError(_("Unsupported bus '%s'" % new_val)) - self._bus = new_val - bus = XMLProperty(get_bus, set_bus, - xpath="./@bus") - - def get_types(self): - return self._types[:] - types = property(get_types) - - def get_type(self): - return self._type - def set_type(self, new_val): - if new_val not in self.types: - raise ValueError(_("Unsupported redirection type '%s'" % new_val)) - self._type = new_val - type = XMLProperty(get_type, set_type, - xpath="./@type") - - def get_host(self): - return self._host - def set_host(self, val): - if len(val) == 0: - raise ValueError(_("Invalid host value")) - self._host = val - host = XMLProperty(get_host, set_host, - xpath="./source/@host") - - def get_service(self): - return self._service - def set_service(self, val): - int(val) - self._service = val - service = XMLProperty(get_service, set_service, - xpath="./source/@service") + TYPE_DEFAULT = "default" + TYPES = ["tcp", "spicevmc", TYPE_DEFAULT] def parse_friendly_server(self, serverstr): - if serverstr.count(":") == 1: - self.host, self.service = serverstr.split(":") - else: - raise ValueError(_("Could not determine or unsupported format of '%s'") % serverstr) + if serverstr.count(":") != 1: + raise ValueError(_("Could not determine or unsupported " + "format of '%s'") % serverstr) + self.host, self.service = serverstr.split(":") - def _get_xml_config(self): - xml = (" \n" % - (self.host, self.service)) - xml += " " - return xml + + _XML_PROP_ORDER = ["bus", "type"] + + bus = XMLProperty(xpath="./@bus", + default_cb=lambda s: "usb", + default_name=BUS_DEFAULT) + type = XMLProperty(xpath="./@type", + default_cb=lambda s: "spicevmc", + default_name=TYPE_DEFAULT) + + host = XMLProperty(xpath="./source/@host") + service = XMLProperty(xpath="./source/@service", is_int=True) diff --git a/virtinst/cli.py b/virtinst/cli.py index 1ea90a93..35c1b163 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -1701,27 +1701,29 @@ def parse_redirdev(guest, optstring, dev=None): # Peel the mode off the front opts = parse_optstr(optstring, remove_first="bus") - bus = get_opt_param(opts, "bus") - stype = get_opt_param(opts, "type") server = get_opt_param(opts, "server") - if bus == "none": + if opts.get("bus") == "none": return None if not dev: - dev = virtinst.VirtualRedirDevice(bus=bus, - stype=stype, - conn=guest.conn) + dev = virtinst.VirtualRedirDevice(guest.conn) - if stype == "spicevmc" and server: - raise ValueError(_("The server option is invalid with spicevmc redirection")) - - if stype == "tcp" and not server: - raise ValueError(_("The server option is missing for TCP redirection")) + set_param = _build_set_param(dev, opts) + set_param("bus", "bus") + set_param("type", "type") if server: dev.parse_friendly_server(server) + if dev.type == "spicevmc" and server: + raise ValueError( + _("The server option is invalid with spicevmc redirection")) + + if dev.type == "tcp" and not server: + raise ValueError( + _("The server option is missing for TCP redirection")) + if opts: raise ValueError(_("Unknown options %s") % opts.keys())