您现在的位置是:主页 > news > 口碑最好的购物网站平台/网络营销企业培训
口碑最好的购物网站平台/网络营销企业培训
admin2025/5/3 8:02:41【news】
简介口碑最好的购物网站平台,网络营销企业培训,郑州建设网站公司,自己做网站用花钱么更新:如注释中所述,最近的发行版不支持Hal,现在的标准是udev,下面是一个使用glib loop和udev的小示例,出于历史原因,我保留了Hal版本。这基本上就是example in the pyudev documentation,它适应…
更新:如注释中所述,最近的发行版不支持Hal,现在的标准是udev,下面是一个使用glib loop和udev的小示例,出于历史原因,我保留了Hal版本。
这基本上就是example in the pyudev documentation,它适应于使用旧版本,并且使用glib循环,注意过滤器应该根据您的特定需要进行定制:import glib
from pyudev import Context, Monitor
try:
from pyudev.glib import MonitorObserver
def device_event(observer, device):
print 'event {0} on device {1}'.format(device.action, device)
except:
from pyudev.glib import GUDevMonitorObserver as MonitorObserver
def device_event(observer, action, device):
print 'event {0} on device {1}'.format(action, device)
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
observer = MonitorObserver(monitor)
observer.connect('device-event', device_event)
monitor.start()
glib.MainLoop().run()
带有Hal和d总线的旧版本:
您可以使用D-Bus绑定并监听DeviceAdded和DeviceRemoved信号。
您必须检查添加的设备的功能,以便仅选择存储设备。
下面是一个小例子,您可以删除注释并尝试它。import dbus
import gobject
class DeviceAddedListener:
def __init__(self):
您需要使用系统总线连接到Hal管理器。self.bus = dbus.SystemBus()
self.hal_manager_obj = self.bus.get_object(
"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager")
self.hal_manager = dbus.Interface(self.hal_manager_obj,
"org.freedesktop.Hal.Manager")
您需要将侦听器连接到您感兴趣的信号,在本例中为DeviceAdded。self.hal_manager.connect_to_signal("DeviceAdded", self._filter)
我使用的是基于功能的过滤器。它将接受任何volume,并使用if调用do_something,您可以阅读Hal文档以找到更适合您需要的查询,或有关Hal设备属性的更多信息。def _filter(self, udi):
device_obj = self.bus.get_object ("org.freedesktop.Hal", udi)
device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device")
if device.QueryCapability("volume"):
return self.do_something(device)
显示有关卷的某些信息的示例函数:def do_something(self, volume):
device_file = volume.GetProperty("block.device")
label = volume.GetProperty("volume.label")
fstype = volume.GetProperty("volume.fstype")
mounted = volume.GetProperty("volume.is_mounted")
mount_point = volume.GetProperty("volume.mount_point")
try:
size = volume.GetProperty("volume.size")
except:
size = 0
print "New storage device detectec:"
print " device_file: %s" % device_file
print " label: %s" % label
print " fstype: %s" % fstype
if mounted:
print " mount_point: %s" % mount_point
else:
print " not mounted"
print " size: %s (%.2fGB)" % (size, float(size) / 1024**3)
if __name__ == '__main__':
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()
DeviceAddedListener()
loop.run()