0

Trying to make a program to monitor usb drive connections using pyudev. Here is the code:

def __init__(self):
    self.window = gtk.Window()
    self.window.set_default_size(300, 300)

    self.vbox= gtk.VBox(False, 5)
    label = gtk.Label("Please plug the device")

    context = Context()
    monitor = Monitor.from_netlink(context)
    monitor.filter_by(subsystem='block',device_type='disk')
    observer = GUDevMonitorObserver(monitor)
    observer.connect("device-added",self.device_connected)
    monitor.start()

    self.vbox.pack_start(label)
    self.window.add(self.vbox)

    self.window.show_all()

def device_connected(self, device):
    self.window.remove(self.vbox)
    label = gtk.Label('{0!r} added'.format(device))
    self.vbox.pack_end(label)
    self.window.add(self.vbox)

The traceback :

vineet@vineet:~/Documents/Project$ python project.py
TypeError: device_connected() takes exactly 2 arguments (3 given)
TypeError: device_connected() takes exactly 2 arguments (3 given)

Please help me rectify this.

I am trying to use the piece of code given on the docs page. As you will notice , the device_connected method has the arguments - device_connected(observer,device) but the code doesn't work in that case either. It returns the throws the same error. But I was wondering how it would work in the first place. Shouldn't every method of a class have self as an argument?

Flame of udun
  • 2,136
  • 7
  • 35
  • 79

1 Answers1

0

The docs don't say those are methods of the class, but signals that are sent by that class. (In fact the name the docs are using is "device-added", which isn't even a valid function name in Python.)

The function should be a standalone one, which you register as a listener for that signal. You can see in the code snippet at the top of the page an example of how to connect your signal to the observer.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • oh yea! But then, how can I make changes to `self.window` if the function is not a part of the class? And if I were to make the function a part of the class how do I resolve the arguments issue? – Flame of udun Mar 16 '14 at 13:34
  • Also running the program after I make the function a stand-alone one along with pulling the all the monitor related code out of the class throws the error : `observer.connect("device-added",device_connected) NameError: name 'device_connected' is not defined` – Flame of udun Mar 16 '14 at 13:40
  • Ok the above seems to have been resolved.Placing the method declaration before `observer.connect` does the trick. – Flame of udun Mar 16 '14 at 13:44