I'd like to write a Python script for Amarok in Linux to automatically copy the stackoverflow podcast to my player. When I plug in the player, it would mount the drive, copy any pending podcasts, and eject the player. How can I listen for the "plugged in" event? I have looked through hald but couldn't find a good example.

我想为Amarok在Linux中编写一个Python脚本,将stackoverflow播客自动复制到我的播放器中。当我插入播放器时,它会挂载驱动器,复制任何挂起的播客,并弹出播放器。我怎样才能听到“插入”事件呢?我查了哈德的资料,但找不到好的例子。

4 个解决方案

#1


51

Update: As said in comments, Hal is not supported in recent distributions, the standard now is udev, Here is a small example that makes use of glib loop and udev, I keep the Hal version for historical reasons.

更新:正如评论中所说,Hal在最近的发行版中不受支持,现在的标准是udev,这里有一个使用glib循环和udev的小例子,出于历史原因,我保留了Hal版本。

This is basically the example in the pyudev documentation, adapted to work with older versions, and with the glib loop, notice that the filter should be customized for your specific needing:

这基本上是pyudev文档中的示例,适用于旧版本,使用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()

Old version with Hal and d-bus:

旧版本的Hal和d-bus:

You can use D-Bus bindings and listen to DeviceAdded and DeviceRemoved signals. You will have to check the capabilities of the Added device in order to select the storage devices only.

您可以使用D-Bus绑定并侦听deviceadd和DeviceRemoved信号。您必须检查添加设备的功能,以便只选择存储设备。

Here is a small example, you can remove the comments and try it.

这里有一个小例子,您可以删除注释并尝试它。

import dbus
import gobject

class DeviceAddedListener:
    def __init__(self):

You need to connect to Hal Manager using the System Bus.

您需要使用系统总线连接到Hal Manager。

        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")

And you need to connect a listener to the signals you are interested on, in this case DeviceAdded.

你需要连接一个监听器到你感兴趣的信号,在这个例子中是deviceadd。

        self.hal_manager.connect_to_signal("DeviceAdded", self._filter)

I'm using a filter based on capabilities. It will accept any volume and will call do_something with if, you can read Hal documentation to find the more suitable queries for your needs, or more information about the properties of the Hal devices.

我在使用基于功能的过滤器。它将接受任何卷,并将调用do_something与if,您可以阅读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)

Example function that shows some information about the volume:

显示有关卷的一些信息的示例函数:

     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()

更多相关文章

  1. 创建链表的小例子
  2. LNMP(Linux+Nginx+Mysql+PHP)环境下安装yaf框架并编写一个MVC例子
  3. 简单统计报表例子(存储过程)
  4. sqlserver 差异备份与还原示例
  5. sqlmap遇到url重写的示例
  6. unity连接数据库MySQL简单例子
  7. webgote的例子(6)SQL注入(盲注)
  8. 大神求解,hibernate4.2.1的一个HelloWorld例子配置搞了两天
  9. 【java开发系列】—— struts2简单入门示例

随机推荐

  1. Android进程内存上限
  2. Android开发之ConstraintLayout布局
  3. android常用知识(累计)
  4. Android系统自带主题的使用及自定义主题
  5. Android圆角图片
  6. Android系统在超级终端下必会的命令大全(a
  7. 使用系统资源的引用总结以及收到短信后给
  8. Android 之EditText自动弹出/不弹出输入
  9. Android系统的开机画面显示过程分析(4)
  10. android相对布局的案例