Jul 21, 2009

HackWeek: NetworkManager Python library

It is Hack Week again, a time when we venture into unexplored territories. So why am I returning to cnetworkmanager?

It is because I am doing a complete rewrite, based on an easy-to-use library that hides some rough edges in dbus-python. If you ever thought NM needed some programmatic prodding but were put off by the tedious details, the library might be just the thing for you. For me, it is also an opportunity to learn more Python.

Compare how you get the interface names.
Before:
#! /usr/bin/python
import dbus
bus = dbus.SystemBus()
NM = 'org.freedesktop.NetworkManager'
nm = bus.get_object(NM, '/org/freedesktop/NetworkManager')
for dev_opath in nm.GetDevices(dbus_interface = NM):
dev = bus.get_object(NM, dev_opath)
print dev.Get(NM + '.Device', 'Interface',
dbus_interface='org.freedesktop.DBus.Properties')
After:
#! /usr/bin/python
from networkmanager.networkmanager import NetworkManager
nm = NetworkManager()
for d in nm.GetDevices():
print d["Interface"]
It is in the librarize branch of the git repo. Beware, the code is still messy. Getting it:
git clone git://repo.or.cz/cnetworkmanager.git cnetworkmanager.librarize
cd cnetworkmanager.librarize
git checkout -b librarize origin/librarize
./cnetworkmanager -h

5 comments:

Nirbheek said...

Why not make the python interface to networkmanager a separate project? Or perhaps a part of upstream?

Martin Vidner said...

Yes, that's the plan.

Martin Vidner said...

v0.20 is now merged in the master branch. It is still messy, but does nearly everything that v0.8.4 did.

Anonymous said...

Awesome!
I've spent a long time trying to connect to a NetworkManager event with python without success. Tried with your bindings, and it works!

#! /usr/bin/python
import gobject
import dbus, dbus.service
from dbus.mainloop.glib import DBusGMainLoop
dbus_loop=DBusGMainLoop()
dbus.set_default_main_loop(dbus_loop)
from networkmanager.networkmanager import NetworkManager

def nState(state):
   print 'New State'
   print state

nm = NetworkManager()

nm.connect_to_signal('StateChanged',nState)

loop = gobject.MainLoop()
loop.run()

XG-網頁設計 said...

Yes, that's the plan.