Search code examples
gtksftppygtk

Pygtk mount file through sftp without username and password dialog?


I am trying to mount a directory via sftp in pygtk. Since I already have the username and password stored in variables, I don't want to get a dialog box asking for the username and password. I am using the following code:

def error_printer(o,r)
    try:
        o.mount_enclosing_volume_finish(r)
    except gio.Error, e:
        print str(e.message),"error code:", e.code

myfile = gio.File("sftp://" + my_ip_address)
op = gtk.MountOperation()
op.set_username(my_username)
op.set_password(my_password)
my_file.mount_enclosing_volume(op, error_printer)

When I run this, a dialog box appears asking for the username and password even though I already set the username and password.

What do I do to make the dialog never appear and use the username and password I set instead?

(I am using pygtk 2.16)


Solution

  • Solved it!
    Had to do it like these guys:

    class DupMountOperation(gio.MountOperation):
    """A simple MountOperation that grabs the password from the environment
       or the user.
    """
    def __init__(self, backend):
        gio.MountOperation.__init__(self)
        self.backend = backend
        self.connect('ask-password', self.ask_password)
    
    def ask_password(self, *args, **kwargs):
        self.set_password(self.backend.get_password())
        self.reply(gio.MOUNT_OPERATION_HANDLED)