From 269339f29f73f3365681d833ca82f5b6eb3c1d5f Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Tue, 21 Jan 2014 08:48:22 -0500 Subject: cli: drop get_* helpers, just make parse_* helpers handle all cases --- virt-image | 6 ++--- virt-install | 39 ++++++++++++++------------- virtinst/cli.py | 82 +++++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 76 insertions(+), 51 deletions(-) diff --git a/virt-image b/virt-image index 5bbb8c1d..6bc997ee 100755 --- a/virt-image +++ b/virt-image @@ -108,11 +108,11 @@ def main(conn=None): if options.uuid: guest.uuid = options.uuid - cli.parse_vcpus(guest, options.vcpu or image.domain.vcpu or "") + cli.parse_vcpus(guest, options.vcpus or image.domain.vcpu or "") cli.get_cpuset(guest, options.cpuset) cli.parse_cpu(guest, options.cpu) - cli.get_networks(guest, options.network) - cli.get_graphics(guest, options.graphics) + cli.parse_network(guest, options.network) + cli.parse_graphics(guest, options.graphics) cli.set_os_variant(guest, options.distro_type, options.distro_variant) cli.parse_features(guest, getattr(options, "features", None)) diff --git a/virt-install b/virt-install index 7d5c1b12..132bea65 100755 --- a/virt-install +++ b/virt-install @@ -140,7 +140,8 @@ def get_disks(guest, disks, nodisks, need_storage): # --file-size to --disk size=8 which doesn't validate on # its own. dev = cli.parse_disk(guest, disk, - virtinst.VirtualDisk(guest.conn)) + virtinst.VirtualDisk(guest.conn), + validate=False) size = dev.cli_size path = dev.path sparse = dev.get_sparse() @@ -516,24 +517,24 @@ def build_guest_instance(conn, options): guest.description = options.description # Non-default devices - cli.get_controllers(guest, options.controller) - cli.get_redirdevs(guest, options.redirdev) - cli.get_memballoons(guest, options.memballoon) - cli.get_networks(guest, options.network) - cli.get_graphics(guest, options.graphics) - cli.get_videos(guest, options.video) - cli.get_watchdogs(guest, options.watchdog) - cli.get_filesystems(guest, options.filesystem) - cli.get_sounds(guest, options.soundhw) - cli.get_serials(guest, options.serial) - cli.get_parallels(guest, options.parallel) - cli.get_channels(guest, options.channel) - cli.get_consoles(guest, options.console) - cli.get_hostdevs(guest, options.host_device) - cli.get_smartcards(guest, options.smartcard) - cli.get_tpms(guest, options.tpm) - cli.get_rngs(guest, options.rng) - cli.get_panic(guest, options.panic) + cli.parse_controller(guest, options.controller) + cli.parse_redirdev(guest, options.redirdev) + cli.parse_memballoon(guest, options.memballoon) + cli.parse_network(guest, options.network) + cli.parse_graphics(guest, options.graphics) + cli.parse_video(guest, options.video) + cli.parse_watchdog(guest, options.watchdog) + cli.parse_filesystem(guest, options.filesystem) + cli.parse_sound(guest, options.soundhw) + cli.parse_serial(guest, options.serial) + cli.parse_parallel(guest, options.parallel) + cli.parse_channel(guest, options.channel) + cli.parse_console(guest, options.console) + cli.parse_hostdev(guest, options.host_device) + cli.parse_smartcard(guest, options.smartcard) + cli.parse_tpm(guest, options.tpm) + cli.parse_rng(guest, options.rng) + cli.parse_panic(guest, options.panic) guest.add_default_input_device() guest.add_default_console_device() diff --git a/virtinst/cli.py b/virtinst/cli.py index b37b634d..5f0be985 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -1124,22 +1124,48 @@ class VirtCLIParser(object): "from parse handler.") self._params.append(_VirtCLIArgument(*args, **kwargs)) - def parse_list(self, guest, opts): - for optstr in util.listify(opts): - devtype = self.devclass.virtual_device_type + def parse(self, guest, optlist, inst=None, validate=True): + # XXX: review + optlist = util.listify(optlist) + editting = bool(inst) + + if editting and optlist: + # If an object is passed in, we are updating it in place, and + # only use the last command line occurence + optlist = [optlist[-1]] + + ret = [] + for optstr in optlist: + optinst = inst + if self.devclass and not inst: + optinst = self.devclass(guest.conn) # pylint: disable=E1102 + try: - dev = self.devclass(guest.conn) # pylint: disable=E1102 - devs = self.parse(guest, optstr, dev) + devs = self._parse_single_optstr(guest, optstr, optinst) for dev in util.listify(devs): - dev.validate() + if not hasattr(dev, "virtual_device_type"): + continue + + if validate: + dev.validate() + if editting: + continue guest.add_device(dev) + + ret += util.listify(devs) except Exception, e: - logging.debug("Exception parsing devtype=%s optstr=%s", - devtype, optstr, exc_info=True) - fail(_("Error in %(devtype)s device parameters: %(err)s") % - {"devtype": devtype, "err": str(e)}) + logging.debug("Exception parsing inst=%s optstr=%s", + inst, optstr, exc_info=True) + fail(_("Error in %(options)s: %(err)s") % + {"options": optstr, "err": str(e)}) + + if not ret: + return None + if len(ret) == 1: + return ret[0] + return ret - def parse(self, guest, optstr, inst=None): + def _parse_single_optstr(self, guest, optstr, inst): if not optstr: return None if self.check_none and optstr == "none": @@ -1576,7 +1602,7 @@ class ParserNetwork(VirtCLIParser): return VirtCLIParser._parse(self, optsobj, inst) -get_networks = ParserNetwork().parse_list +parse_network = ParserNetwork().parse ###################### @@ -1623,7 +1649,7 @@ class ParserGraphics(VirtCLIParser): self.set_param("passwdValidTo", "passwordvalidto") -get_graphics = ParserGraphics().parse_list +parse_graphics = ParserGraphics().parse ######################## @@ -1652,7 +1678,7 @@ class ParserController(VirtCLIParser): return VirtCLIParser._parse(self, opts, inst) -get_controllers = ParserController().parse_list +parse_controller = ParserController().parse ####################### @@ -1669,7 +1695,7 @@ class ParserSmartcard(VirtCLIParser): self.set_param("type", "type") -get_smartcards = ParserSmartcard().parse_list +parse_smartcard = ParserSmartcard().parse ###################### @@ -1686,7 +1712,7 @@ class ParserRedir(VirtCLIParser): self.set_param("type", "type") self.set_param("parse_friendly_server", "server") -get_redirdevs = ParserRedir().parse_list +parse_redirdev = ParserRedir().parse ################# @@ -1709,7 +1735,7 @@ class ParserTPM(VirtCLIParser): return VirtCLIParser._parse(self, opts, inst) -get_tpms = ParserTPM().parse_list +parse_tpm = ParserTPM().parse ################# @@ -1781,7 +1807,7 @@ class ParserRNG(VirtCLIParser): return VirtCLIParser._parse(self, optsobj, inst) -get_rngs = ParserRNG().parse_list +parse_rng = ParserRNG().parse ###################### @@ -1797,7 +1823,7 @@ class ParserWatchdog(VirtCLIParser): self.set_param("action", "action") -get_watchdogs = ParserWatchdog().parse_list +parse_watchdog = ParserWatchdog().parse ######################## @@ -1812,7 +1838,7 @@ class ParserMemballoon(VirtCLIParser): self.set_param("model", "model") -get_memballoons = ParserMemballoon().parse_list +parse_memballoon = ParserMemballoon().parse ################### @@ -1833,7 +1859,7 @@ class ParserPanic(VirtCLIParser): self.set_param(None, "iobase", setter_cb=set_iobase_cb) -get_panic = ParserPanic().parse_list +parse_panic = ParserPanic().parse ###################################################### @@ -1878,22 +1904,22 @@ class _ParserChar(VirtCLIParser): class ParserSerial(_ParserChar): devclass = virtinst.VirtualSerialDevice -get_serials = ParserSerial().parse_list +parse_serial = ParserSerial().parse class ParserParallel(_ParserChar): devclass = virtinst.VirtualParallelDevice -get_parallels = ParserParallel().parse_list +parse_parallel = ParserParallel().parse class ParserChannel(_ParserChar): devclass = virtinst.VirtualChannelDevice -get_channels = ParserChannel().parse_list +parse_channel = ParserChannel().parse class ParserConsole(_ParserChar): devclass = virtinst.VirtualConsoleDevice -get_consoles = ParserConsole().parse_list +parse_console = ParserConsole().parse ######################## @@ -1911,7 +1937,6 @@ class ParserFilesystem(VirtCLIParser): self.set_param("target", "target") -get_filesystems = ParserFilesystem().parse_list parse_filesystem = ParserFilesystem().parse @@ -1927,7 +1952,7 @@ class ParserVideo(VirtCLIParser): self.set_param("model", "model", ignore_default=True) -get_videos = ParserVideo().parse_list +parse_video = ParserVideo().parse ##################### @@ -1942,7 +1967,7 @@ class ParserSound(VirtCLIParser): self.set_param("model", "model", ignore_default=True) -get_sounds = ParserSound().parse_list +parse_sound = ParserSound().parse ##################### @@ -1964,5 +1989,4 @@ class ParserHostdev(VirtCLIParser): self.set_param("driver_name", "driver_name") -get_hostdevs = ParserHostdev().parse_list parse_hostdev = ParserHostdev().parse -- cgit v1.2.1