virt-manager/virtManager/mediadev.py

145 lines
4.0 KiB
Python
Raw Normal View History

#
# Copyright (C) 2009 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.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.
#
# pylint: disable=E0611
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
from gi.repository import GObject
# pylint: enable=E0611
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
import logging
2013-09-23 02:13:24 +04:00
from virtinst import NodeDevice
from virtManager.baseclass import vmmGObject
MEDIA_FLOPPY = "floppy"
MEDIA_CDROM = "cdrom"
class vmmMediaDevice(vmmGObject):
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
__gsignals__ = {
"media-added": (GObject.SignalFlags.RUN_FIRST, None, []),
"media-removed": (GObject.SignalFlags.RUN_FIRST, None, []),
}
@staticmethod
2011-04-10 05:56:05 +04:00
def mediadev_from_nodedev(dev):
nodedev = dev.get_xmlobj()
2011-04-10 03:32:30 +04:00
if nodedev.device_type != "storage":
return None
if nodedev.drive_type not in [MEDIA_CDROM, MEDIA_FLOPPY]:
return None
drvtype = nodedev.drive_type
path = nodedev.block
key = nodedev.name
has_media = nodedev.media_available
media_label = nodedev.media_label
media_key = None
obj = vmmMediaDevice(path, key, has_media, media_label, media_key,
2011-04-10 03:32:30 +04:00
dev, drvtype)
obj.do_poll = True
return obj
def __init__(self, path, key, has_media, media_label, media_key,
nodedev_obj=None, media_type=MEDIA_CDROM):
vmmGObject.__init__(self)
self.path = path
self.key = key
self._has_media = has_media
self.media_label = media_label
2009-12-01 00:22:20 +03:00
self.media_key = media_key
self.media_type = media_type
self.nodedev_obj = nodedev_obj
self.do_poll = False
self.last_tick = 0
def _cleanup(self):
pass
def get_path(self):
return self.path
def get_key(self):
return self.key
def get_media_type(self):
return self.media_type
def has_media(self):
return self._has_media
def get_media_label(self):
return self.media_label
2009-12-01 00:22:20 +03:00
def get_media_key(self):
return self.media_key
def set_media(self, has_media, media_label, media_key):
self._has_media = has_media
self.media_label = media_label
2009-12-01 00:22:20 +03:00
self.media_key = media_key
def clear_media(self):
self.set_media(None, None, None)
def pretty_label(self):
media_label = self.get_media_label()
has_media = self.has_media()
if not has_media:
media_label = _("No media detected")
elif not media_label:
media_label = _("Media Unknown")
return "%s (%s)" % (media_label, self.get_path())
#########################################
# Nodedev API polling for media updates #
#########################################
def tick(self):
if not self.nodedev_obj:
return
if not self.nodedev_obj.conn.is_active():
return
try:
2011-04-10 03:32:30 +04:00
self.nodedev_obj.refresh_xml()
xml = self.nodedev_obj.get_xml()
except:
# Assume the device was removed
return
try:
2013-09-23 02:13:24 +04:00
vobj = NodeDevice.parse(self.nodedev_obj.conn.get_backend(), xml)
has_media = vobj.media_available or False
except:
logging.exception("Node device CDROM polling failed")
return
if has_media == self.has_media():
return
self.set_media(has_media, None, None)
self.idle_emit(has_media and "media-added" or "media-removed")