Permalink
Browse files

After an evening of hacking, here is python-networkmanager

  • Loading branch information...
0 parents commit c04994ba91225245cc7b21400c1daf70933c2655 @seveas committed Nov 4, 2011
Showing with 1,072 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +15 −0 COPYING
  3. +19 −0 HACKING
  4. +1 −0 MANIFEST.in
  5. +304 −0 NetworkManager.py
  6. +23 −0 README
  7. +130 −0 docs/Makefile
  8. +216 −0 docs/conf.py
  9. +128 −0 docs/index.rst
  10. +46 −0 examples/connection_detail.py
  11. +48 −0 examples/info.py
  12. +32 −0 examples/listener.py
  13. +59 −0 examples/openvpn_over_ssh.py
  14. +25 −0 makeconstants.py
  15. +20 −0 setup.py
@@ -0,0 +1,6 @@
+dist
+docs/_build
+.*.sw?
+*.pyc
+*.pyo
+MANIFEST
15 COPYING
@@ -0,0 +1,15 @@
+python-networkmanager - Easy communication with NetworkManager
+Copyright (C) 2011 Dennis Kaarsemaker <dennis@kaarsemaker.net>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
19 HACKING
@@ -0,0 +1,19 @@
+How to roll a release:
+
+Source preparation:
+ - Bump version number in setup.py
+ - Bump version number in debian/changelog
+ - Tag the release
+ - Push to github
+
+Packaging:
+ - ./setup.py sdist
+ - dpkg-buildpackage -rfakeroot
+ - Upload tarballs and packages to kaarsemaker.net
+
+pypi:
+ - dh clean
+ - cd docs/_build/html
+ - rm -f docs.zip && zip -r docs.zip *
+ - Upload docs.zip to pypi
+ - ./setup.py register
@@ -0,0 +1 @@
+include docs/index.rst
@@ -0,0 +1,304 @@
+import dbus
+import sys
+
+class NMDbusInterface(object):
+ bus = dbus.SystemBus()
+ dbus_service = 'org.freedesktop.NetworkManager'
+ object_path = None
+
+ def __init__(self, object_path=None):
+ if isinstance(object_path, NMDbusInterface):
+ object_path = object_path.object_path
+ self.object_path = self.object_path or object_path
+ self.proxy = self.bus.get_object(self.dbus_service, self.object_path)
+ self.interface = dbus.Interface(self.proxy, self.interface_name)
+
+ try:
+ properties = self.proxy.GetAll(self.interface_name,
+ dbus_interface='org.freedesktop.DBus.Properties')
+ for p in properties:
+ if not hasattr(self.__class__, p):
+ setattr(self.__class__, p, self._make_property(p))
+ except dbus.exceptions.DBusException, e:
+ if e.get_dbus_name() != 'org.freedesktop.DBus.Error.UnknownMethod':
+ raise
+
+ def _make_property(self, name):
+ def get(self):
+ return self.unwrap(self.proxy.Get(self.interface_name, name,
+ dbus_interface='org.freedesktop.DBus.Properties'))
+ def set(self, value):
+ return self.proxy.Set(self.interface_name, name, self.wrap(value),
+ dbus_interface='org.freedesktop.DBus.Properties')
+ return property(get, set)
+
+ def unwrap(self, val):
+ if isinstance(val, (dbus.Array, list, tuple)):
+ return [self.unwrap(x) for x in val]
+ if isinstance(val, (dbus.Dictionary, dict)):
+ return dict([(self.unwrap(x), self.unwrap(y)) for x,y in val.iteritems()])
+ if isinstance(val, dbus.ObjectPath):
+ if val.startswith('/org/freedesktop/NetworkManager/'):
+ classname = val.split('/')[4]
+ classname = {
+ 'Settings': 'Connection',
+ 'Devices': 'Device',
+ }.get(classname, classname)
+ return globals()[classname](val)
+ if isinstance(val, dbus.ByteArray):
+ return "".join([str(x) for x in val])
+ if isinstance(val, (dbus.Signature, dbus.String)):
+ return str(val)
+ if isinstance(val, dbus.UTF8String):
+ return unicode(val)
+ if isinstance(val, dbus.Boolean):
+ return bool(val)
+ if isinstance(val, (dbus.Int16, dbus.UInt16, dbus.Int32, dbus.UInt32, dbus.Int64, dbus.UInt64)):
+ return int(val)
+ return val
+
+ def wrap(self, val):
+ if isinstance(val, NMDbusInterface):
+ return val.object_path
+ if hasattr(val, '__iter__') and not isinstance(val, basestring):
+ if hasattr(val, 'items'):
+ return dict([(x, self.wrap(y)) for x, y in val.iteritems()])
+ else:
+ return [self.wrap(x) for x in val]
+ return val
+
+ def __getattr__(self, name):
+ if name == 'object_path':
+ print name
+ try:
+ return super(NMDbusInterface, self).__getattribute__(name)
+ except AttributeError:
+ def proxy_call(*args, **kwargs):
+ func = getattr(self.interface, name)
+ args = self.wrap(args)
+ kwargs = self.wrap(kwargs)
+ ret = func(*args, **kwargs)
+ return self.unwrap(ret)
+ return proxy_call
+
+ def connect_to_signal(self, signal, handler, *args, **kwargs):
+ def helper(*args, **kwargs):
+ args = [self.unwrap(x) for x in args]
+ handler(*args, **kwargs)
+ args = self.wrap(args)
+ kwargs = self.wrap(kwargs)
+ return self.proxy.connect_to_signal(signal, helper, *args, **kwargs)
+
+class NetworkManager(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager'
+ object_path = '/org/freedesktop/NetworkManager'
+NetworkManager = NetworkManager()
+
+class Settings(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Settings'
+ object_path = '/org/freedesktop/NetworkManager/Settings'
+Settings = Settings()
+
+class Connection(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Settings.Connection'
+
+class ActiveConnection(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Connection.Active'
+
+class Device(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device'
+
+ def SpecificDevice(self):
+ return {
+ NM_DEVICE_TYPE_ETHERNET: Wired,
+ NM_DEVICE_TYPE_WIFI: Wireless,
+ NM_DEVICE_TYPE_BT: Bluetooth,
+ NM_DEVICE_TYPE_OLPC_MESH: OlpcMesh,
+ NM_DEVICE_TYPE_WIMAX: Wimax,
+ NM_DEVICE_TYPE_MODEM: Modem,
+ }[self.DeviceType](self.object_path)
+
+class AccessPoint(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.AccessPoint'
+
+class Wired(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device.Wired'
+
+class Wireless(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device.Wireless'
+
+class Modem(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device.Modem'
+
+class Bluetooth(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device.Bluetooth'
+
+class Wimax(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device.Wimax'
+
+class OlpcMesh(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.Device.OlpcMesh'
+
+class IP4Config(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.IP4Config'
+
+class IP6Config(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.IP6Config'
+
+class VPNConnection(NMDbusInterface):
+ interface_name = 'org.freedesktop.NetworkManager.VPN.Connection'
+
+def const(prefix, val):
+ prefix = 'NM_' + prefix.upper() + '_'
+ for key, vval in globals().iteritems():
+ if 'REASON' in key and 'REASON' not in prefix:
+ continue
+ if key.startswith(prefix) and val == vval:
+ return key.replace(prefix,'').lower()
+ raise ValueError("No constant found for %s* with value %d", (prefix, val))
+
+# Constants below are generated with makeconstants.py. Do not edit manually.
+NM_STATE_UNKNOWN = 0
+NM_STATE_ASLEEP = 10
+NM_STATE_DISCONNECTED = 20
+NM_STATE_DISCONNECTING = 30
+NM_STATE_CONNECTING = 40
+NM_STATE_CONNECTED_LOCAL = 50
+NM_STATE_CONNECTED_SITE = 60
+NM_STATE_CONNECTED_GLOBAL = 70
+NM_DEVICE_TYPE_UNKNOWN = 0
+NM_DEVICE_TYPE_ETHERNET = 1
+NM_DEVICE_TYPE_WIFI = 2
+NM_DEVICE_TYPE_UNUSED1 = 3
+NM_DEVICE_TYPE_UNUSED2 = 4
+NM_DEVICE_TYPE_BT = 5
+NM_DEVICE_TYPE_OLPC_MESH = 6
+NM_DEVICE_TYPE_WIMAX = 7
+NM_DEVICE_TYPE_MODEM = 8
+NM_DEVICE_CAP_NONE = 0
+NM_DEVICE_CAP_NM_SUPPORTED = 1
+NM_DEVICE_CAP_CARRIER_DETECT = 2
+NM_WIFI_DEVICE_CAP_NONE = 0
+NM_WIFI_DEVICE_CAP_CIPHER_WEP40 = 1
+NM_WIFI_DEVICE_CAP_CIPHER_WEP104 = 2
+NM_WIFI_DEVICE_CAP_CIPHER_TKIP = 4
+NM_WIFI_DEVICE_CAP_CIPHER_CCMP = 8
+NM_WIFI_DEVICE_CAP_WPA = 16
+NM_WIFI_DEVICE_CAP_RSN = 32
+NM_802_11_AP_FLAGS_NONE = 0
+NM_802_11_AP_FLAGS_PRIVACY = 1
+NM_802_11_AP_SEC_NONE = 0
+NM_802_11_AP_SEC_PAIR_WEP40 = 1
+NM_802_11_AP_SEC_PAIR_WEP104 = 2
+NM_802_11_AP_SEC_PAIR_TKIP = 4
+NM_802_11_AP_SEC_PAIR_CCMP = 8
+NM_802_11_AP_SEC_GROUP_WEP40 = 16
+NM_802_11_AP_SEC_GROUP_WEP104 = 32
+NM_802_11_AP_SEC_GROUP_TKIP = 64
+NM_802_11_AP_SEC_GROUP_CCMP = 128
+NM_802_11_AP_SEC_KEY_MGMT_PSK = 256
+NM_802_11_AP_SEC_KEY_MGMT_802_1X = 512
+NM_802_11_MODE_UNKNOWN = 0
+NM_802_11_MODE_ADHOC = 1
+NM_802_11_MODE_INFRA = 2
+NM_BT_CAPABILITY_NONE = 0
+NM_BT_CAPABILITY_DUN = 1
+NM_BT_CAPABILITY_NAP = 2
+NM_DEVICE_MODEM_CAPABILITY_NONE = 0
+NM_DEVICE_MODEM_CAPABILITY_POTS = 1
+NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO = 2
+NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS = 4
+NM_DEVICE_MODEM_CAPABILITY_LTE = 8
+NM_DEVICE_STATE_UNKNOWN = 0
+NM_DEVICE_STATE_UNMANAGED = 10
+NM_DEVICE_STATE_UNAVAILABLE = 20
+NM_DEVICE_STATE_DISCONNECTED = 30
+NM_DEVICE_STATE_PREPARE = 40
+NM_DEVICE_STATE_CONFIG = 50
+NM_DEVICE_STATE_NEED_AUTH = 60
+NM_DEVICE_STATE_IP_CONFIG = 70
+NM_DEVICE_STATE_IP_CHECK = 80
+NM_DEVICE_STATE_SECONDARIES = 90
+NM_DEVICE_STATE_ACTIVATED = 100
+NM_DEVICE_STATE_DEACTIVATING = 110
+NM_DEVICE_STATE_FAILED = 120
+NM_DEVICE_STATE_REASON_NONE = 0
+NM_DEVICE_STATE_REASON_UNKNOWN = 1
+NM_DEVICE_STATE_REASON_NOW_MANAGED = 2
+NM_DEVICE_STATE_REASON_NOW_UNMANAGED = 3
+NM_DEVICE_STATE_REASON_CONFIG_FAILED = 4
+NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE = 5
+NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED = 6
+NM_DEVICE_STATE_REASON_NO_SECRETS = 7
+NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT = 8
+NM_DEVICE_STATE_REASON_SUPPLICANT_CONFIG_FAILED = 9
+NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED = 10
+NM_DEVICE_STATE_REASON_SUPPLICANT_TIMEOUT = 11
+NM_DEVICE_STATE_REASON_PPP_START_FAILED = 12
+NM_DEVICE_STATE_REASON_PPP_DISCONNECT = 13
+NM_DEVICE_STATE_REASON_PPP_FAILED = 14
+NM_DEVICE_STATE_REASON_DHCP_START_FAILED = 15
+NM_DEVICE_STATE_REASON_DHCP_ERROR = 16
+NM_DEVICE_STATE_REASON_DHCP_FAILED = 17
+NM_DEVICE_STATE_REASON_SHARED_START_FAILED = 18
+NM_DEVICE_STATE_REASON_SHARED_FAILED = 19
+NM_DEVICE_STATE_REASON_AUTOIP_START_FAILED = 20
+NM_DEVICE_STATE_REASON_AUTOIP_ERROR = 21
+NM_DEVICE_STATE_REASON_AUTOIP_FAILED = 22
+NM_DEVICE_STATE_REASON_MODEM_BUSY = 23
+NM_DEVICE_STATE_REASON_MODEM_NO_DIAL_TONE = 24
+NM_DEVICE_STATE_REASON_MODEM_NO_CARRIER = 25
+NM_DEVICE_STATE_REASON_MODEM_DIAL_TIMEOUT = 26
+NM_DEVICE_STATE_REASON_MODEM_DIAL_FAILED = 27
+NM_DEVICE_STATE_REASON_MODEM_INIT_FAILED = 28
+NM_DEVICE_STATE_REASON_GSM_APN_FAILED = 29
+NM_DEVICE_STATE_REASON_GSM_REGISTRATION_NOT_SEARCHING = 30
+NM_DEVICE_STATE_REASON_GSM_REGISTRATION_DENIED = 31
+NM_DEVICE_STATE_REASON_GSM_REGISTRATION_TIMEOUT = 32
+NM_DEVICE_STATE_REASON_GSM_REGISTRATION_FAILED = 33
+NM_DEVICE_STATE_REASON_GSM_PIN_CHECK_FAILED = 34
+NM_DEVICE_STATE_REASON_FIRMWARE_MISSING = 35
+NM_DEVICE_STATE_REASON_REMOVED = 36
+NM_DEVICE_STATE_REASON_SLEEPING = 37
+NM_DEVICE_STATE_REASON_CONNECTION_REMOVED = 38
+NM_DEVICE_STATE_REASON_USER_REQUESTED = 39
+NM_DEVICE_STATE_REASON_CARRIER = 40
+NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED = 41
+NM_DEVICE_STATE_REASON_SUPPLICANT_AVAILABLE = 42
+NM_DEVICE_STATE_REASON_MODEM_NOT_FOUND = 43
+NM_DEVICE_STATE_REASON_BT_FAILED = 44
+NM_DEVICE_STATE_REASON_LAST = 65535
+NM_ACTIVE_CONNECTION_STATE_UNKNOWN = 0
+NM_ACTIVE_CONNECTION_STATE_ACTIVATING = 1
+NM_ACTIVE_CONNECTION_STATE_ACTIVATED = 2
+NM_ACTIVE_CONNECTION_STATE_DEACTIVATING = 3
+NM_VPN_SERVICE_STATE_UNKNOWN = 0
+NM_VPN_SERVICE_STATE_INIT = 1
+NM_VPN_SERVICE_STATE_SHUTDOWN = 2
+NM_VPN_SERVICE_STATE_STARTING = 3
+NM_VPN_SERVICE_STATE_STARTED = 4
+NM_VPN_SERVICE_STATE_STOPPING = 5
+NM_VPN_SERVICE_STATE_STOPPED = 6
+NM_VPN_CONNECTION_STATE_UNKNOWN = 0
+NM_VPN_CONNECTION_STATE_PREPARE = 1
+NM_VPN_CONNECTION_STATE_NEED_AUTH = 2
+NM_VPN_CONNECTION_STATE_CONNECT = 3
+NM_VPN_CONNECTION_STATE_IP_CONFIG_GET = 4
+NM_VPN_CONNECTION_STATE_ACTIVATED = 5
+NM_VPN_CONNECTION_STATE_FAILED = 6
+NM_VPN_CONNECTION_STATE_DISCONNECTED = 7
+NM_VPN_CONNECTION_STATE_REASON_UNKNOWN = 0
+NM_VPN_CONNECTION_STATE_REASON_NONE = 1
+NM_VPN_CONNECTION_STATE_REASON_USER_DISCONNECTED = 2
+NM_VPN_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED = 3
+NM_VPN_CONNECTION_STATE_REASON_SERVICE_STOPPED = 4
+NM_VPN_CONNECTION_STATE_REASON_IP_CONFIG_INVALID = 5
+NM_VPN_CONNECTION_STATE_REASON_CONNECT_TIMEOUT = 6
+NM_VPN_CONNECTION_STATE_REASON_SERVICE_START_TIMEOUT = 7
+NM_VPN_CONNECTION_STATE_REASON_SERVICE_START_FAILED = 8
+NM_VPN_CONNECTION_STATE_REASON_NO_SECRETS = 9
+NM_VPN_CONNECTION_STATE_REASON_LOGIN_FAILED = 10
+NM_VPN_CONNECTION_STATE_REASON_CONNECTION_REMOVED = 11
+NM_VPN_PLUGIN_FAILURE_LOGIN_FAILED = 0
+NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED = 1
+NM_VPN_PLUGIN_FAILURE_BAD_IP_CONFIG = 2
23 README
@@ -0,0 +1,23 @@
+python-networkmanager Easy communication with NetworkManager
+============================================================
+
+python-networkmanager wraps NetworkManagers D-Bus interface so you can be less
+verbose when talking to NetworkManager from python. All interfaces have been
+wrapped in classes, properties are exposed as python properties and function
+calls are forwarded to the correct interface.
+
+See docs/index.rst for the documentation. An HTML version can be found on
+http://packages.python.org/python-networkmanager/
+
+Quick install instructions
+==========================
+
+Stable version:
+
+$ sudo easy_install python-prctl
+
+Latest code:
+
+$ git clone http://github.com/seveas/python-networkmanager
+$ cd python-networkmanager
+$ sudo python setup.py install
Oops, something went wrong.

0 comments on commit c04994b

Please sign in to comment.