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.
#! /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',
#! /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


bheekling 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...

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
from networkmanager.networkmanager import NetworkManager

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

nm = NetworkManager()


loop = gobject.MainLoop()

XG-網頁設計 said...

Yes, that's the plan.