mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-03-11 09:04:54 +00:00
networks: Refactor creating a network manager client
This allows better control on how to create and maintain the network manager client. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
693b109806
commit
b5ae03239c
@ -19,13 +19,15 @@ Helper functions for working with network manager.
|
||||
"""
|
||||
|
||||
import collections
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import logging
|
||||
import socket
|
||||
import struct
|
||||
import uuid
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth.utils import import_from_gi
|
||||
|
||||
glib = import_from_gi('GLib', '2.0')
|
||||
nm = import_from_gi('NM', '1.0')
|
||||
|
||||
@ -59,6 +61,11 @@ def ipv4_int_to_string(address_int):
|
||||
return socket.inet_ntoa(struct.pack('=I', address_int))
|
||||
|
||||
|
||||
def get_nm_client():
|
||||
"""Return a network manager client object."""
|
||||
return nm.Client.new(None)
|
||||
|
||||
|
||||
def _callback(source_object, result, user_data):
|
||||
"""Called when an operation is completed."""
|
||||
del source_object # Unused
|
||||
@ -76,7 +83,7 @@ def _commit_callback(connection, error, data=None):
|
||||
def get_interface_list(device_type):
|
||||
"""Get a list of network interface available on the system."""
|
||||
interfaces = {}
|
||||
for device in nm.Client.new(None).get_devices():
|
||||
for device in get_nm_client().get_devices():
|
||||
if device.get_device_type() == device_type:
|
||||
interfaces[device.get_iface()] = device.get_hw_address()
|
||||
|
||||
@ -100,7 +107,7 @@ def get_status_from_connection(connection):
|
||||
setting_wireless = connection.get_setting_wireless()
|
||||
status['wireless']['ssid'] = setting_wireless.get_ssid().get_data()
|
||||
|
||||
primary_connection = nm.Client.new(None).get_primary_connection()
|
||||
primary_connection = get_nm_client().get_primary_connection()
|
||||
status['primary'] = (
|
||||
primary_connection
|
||||
and primary_connection.get_uuid() == connection.get_uuid())
|
||||
@ -210,7 +217,7 @@ def _get_wifi_channel_from_frequency(frequency):
|
||||
def get_connection_list():
|
||||
"""Get a list of active and available connections."""
|
||||
active_uuids = []
|
||||
client = nm.Client.new(None)
|
||||
client = get_nm_client()
|
||||
for connection in client.get_active_connections():
|
||||
active_uuids.append(connection.get_uuid())
|
||||
|
||||
@ -246,7 +253,7 @@ def get_connection(connection_uuid):
|
||||
Raise ConnectionNotFound if a connection with that uuid is not
|
||||
found.
|
||||
"""
|
||||
client = nm.Client.new(None)
|
||||
client = get_nm_client()
|
||||
connection = client.get_connection_by_uuid(connection_uuid)
|
||||
if not connection:
|
||||
raise ConnectionNotFound(connection_uuid)
|
||||
@ -260,7 +267,7 @@ def get_active_connection(connection_uuid):
|
||||
Raise ConnectionNotFound if a connection with that uuid is not
|
||||
found.
|
||||
"""
|
||||
connections = nm.Client.new(None).get_active_connections()
|
||||
connections = get_nm_client().get_active_connections()
|
||||
connections = {
|
||||
connection.get_uuid(): connection
|
||||
for connection in connections
|
||||
@ -273,7 +280,7 @@ def get_active_connection(connection_uuid):
|
||||
|
||||
def get_device_by_interface_name(interface_name):
|
||||
"""Return a device by interface name."""
|
||||
return nm.Client.new(None).get_device_by_iface(interface_name)
|
||||
return get_nm_client().get_device_by_iface(interface_name)
|
||||
|
||||
|
||||
def _update_common_settings(connection, connection_uuid, common):
|
||||
@ -453,7 +460,7 @@ def add_connection(settings):
|
||||
"""
|
||||
connection_uuid = str(uuid.uuid4())
|
||||
connection = _update_settings(None, connection_uuid, settings)
|
||||
client = nm.Client.new(None)
|
||||
client = get_nm_client()
|
||||
client.add_connection_async(connection, True, None, _callback, None)
|
||||
return connection_uuid
|
||||
|
||||
@ -468,7 +475,7 @@ def activate_connection(connection_uuid):
|
||||
"""Find and activate a network connection."""
|
||||
connection = get_connection(connection_uuid)
|
||||
interface = connection.get_interface_name()
|
||||
client = nm.Client.new(None)
|
||||
client = get_nm_client()
|
||||
for device in client.get_devices():
|
||||
if device.get_iface() == interface:
|
||||
client.activate_connection_async(connection, device, '/', None,
|
||||
@ -483,7 +490,7 @@ def activate_connection(connection_uuid):
|
||||
def deactivate_connection(connection_uuid):
|
||||
"""Find and de-activate a network connection."""
|
||||
active_connection = get_active_connection(connection_uuid)
|
||||
nm.Client.new(None).deactivate_connection(active_connection)
|
||||
get_nm_client().deactivate_connection(active_connection)
|
||||
return active_connection
|
||||
|
||||
|
||||
@ -501,7 +508,7 @@ def delete_connection(connection_uuid):
|
||||
def wifi_scan():
|
||||
"""Scan for available access points across all Wi-Fi devices."""
|
||||
access_points = []
|
||||
for device in nm.Client.new(None).get_devices():
|
||||
for device in get_nm_client().get_devices():
|
||||
if device.get_device_type() != nm.DeviceType.WIFI:
|
||||
continue
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user