UNB/ CS/ David Bremner/ blog/ files/ user-nm-dispatch.py
#!/usr/bin/env python
#
# Copyright (C) 2007 by David Bremner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.


import os.path
import sys
import traceback
import gobject
import dbus
import dbus.mainloop.glib
import pwd


def nm_signal_handler(*args,**kwargs):

    # basename is arguably slightly dodgy, since it is not  
    # a filesystem path.

    if kwargs['member'] == 'DeviceNowActive':
        interface=os.path.basename(args[0])
        ifup(interface,args[1:])
    elif kwargs['member'] == 'DeviceDeactivated':
        interface=os.path.basename(args[0])

        ifdown(interface,args[1:])


if __name__ == '__main__':

    USERNAME = pwd.getpwuid(os.getuid())[0]
    USERHOME = pwd.getpwuid(os.getuid())[5]

    nmdispatchrc= USERHOME+"/.nmdispatchrc"
    execfile(nmdispatchrc)

# set up event loop and dbus signal handler
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    bus = dbus.SystemBus()

    bus.add_signal_receiver(
        nm_signal_handler, 
        interface_keyword='dbus_interface', 
        member_keyword='member',
        dbus_interface="org.freedesktop.NetworkManager")
                            
    gobject.MainLoop().run()