33#
44# Example to create a new WiFi-PSK network connection profile:
55#
6- # $ add-wifi-psk-connection.py --help
6+ # $ examples/block/ add-wifi-psk-connection.py --help
77# usage: add-wifi-psk-connection.py [-h] [-c CONN_ID] [-s SSID] [-p PSK]
88# [-i INTERFACE_NAME] [-a] [--save]
99#
1212# optional arguments:
1313# -h, --help show this help message and exit
1414# -c CONN_ID Connection Id
15+ # -u UUID Connection UUID
1516# -s SSID WiFi SSID
1617# -p PSK WiFi PSK
1718# -i INTERFACE_NAME WiFi device
4445# -> org.freedesktop.NetworkManager.Settings (Settings Profile Manager)
4546
4647import functools
48+ import logging
4749import pprint
4850import sdbus
49- import uuid
51+ from uuid import uuid4
5052from argparse import ArgumentParser , Namespace
5153from sdbus_block .networkmanager import NetworkManagerSettings
5254from sdbus_block .networkmanager import NetworkManagerConnectionProperties
5355
5456
55- def add_wifi_psk_connection_profile (args : Namespace ) -> None :
56- """Add a temporary (not yet saved) network connection profile"""
57+ def add_wifi_psk_connection_profile (args : Namespace ) -> str :
58+ """Add a temporary (not yet saved) network connection profile
59+ :param Namespace args: autoconnect, conn_id, psk, save, ssid, uuid
60+ :return: dbus connection path of the created connection profile
61+ """
62+ info = logging .getLogger ().info
5763
5864 # If we add many connections passing the same id, things get messy. Check:
5965 if NetworkManagerSettings ().get_connections_by_id (args .conn_id ):
6066 print (f'Connection "{ args .conn_id } " exists, remove it first' )
6167 print (f'Run: nmcli connection delete "{ args .conn_id } "' )
62- return
68+ return ""
6369
6470 properties : NetworkManagerConnectionProperties = {
6571 "connection" : {
6672 "id" : ("s" , args .conn_id ),
67- "uuid" : ("s" , str (uuid . uuid4 () )),
73+ "uuid" : ("s" , str (args . uuid )),
6874 "type" : ("s" , "802-11-wireless" ),
6975 "autoconnect" : ("b" , args .auto ),
7076 },
@@ -86,27 +92,30 @@ def add_wifi_psk_connection_profile(args: Namespace) -> None:
8692 if args .interface_name :
8793 properties ["connection" ]["interface-name" ] = ("s" , args .interface_name )
8894
89- if args .save :
90- NetworkManagerSettings ().add_connection (properties )
91- print ("New connection profile created and saved, show it with:" )
92- else :
93- NetworkManagerSettings ().add_connection_unsaved (properties )
94- print ("New unsaved connection profile created, show it with:" )
95-
96- print (f'nmcli connection show "{ args .conn_id } "|grep -v -e -- -e default' )
97- print ("Settings used:" )
98- functools .partial (pprint .pprint , sort_dicts = False )(properties )
95+ s = NetworkManagerSettings ()
96+ addconnection = s .add_connection if args .save else s .add_connection_unsaved
97+ connection_settings_dbus_path = addconnection (properties )
98+ created = "created and saved" if args .save else "created"
99+ info (f"New unsaved connection profile { created } , show it with:" )
100+ info (f'nmcli connection show "{ args .conn_id } "|grep -v -e -- -e default' )
101+ info ("Settings used:" )
102+ info (functools .partial (pprint .pformat , sort_dicts = False )(properties ))
103+ return connection_settings_dbus_path
99104
100105
101106if __name__ == "__main__" :
107+ logging .basicConfig (format = "%(message)s" , level = logging .INFO )
102108 p = ArgumentParser (description = "Optional arguments have example values:" )
103109 conn_id = "MyConnectionExample"
104110 p .add_argument ("-c" , dest = "conn_id" , default = conn_id , help = "Connection Id" )
111+ p .add_argument ("-u" , dest = "uuid" , default = uuid4 (), help = "Connection UUID" )
105112 p .add_argument ("-s" , dest = "ssid" , default = "CafeSSID" , help = "WiFi SSID" )
106113 p .add_argument ("-p" , dest = "psk" , default = "Coffee!!" , help = "WiFi PSK" )
107114 p .add_argument ("-i" , dest = "interface_name" , default = "" , help = "WiFi device" )
108115 p .add_argument ("-a" , dest = "auto" , action = "store_true" , help = "autoconnect" )
109116 p .add_argument ("--save" , dest = "save" , action = "store_true" , help = "Save" )
110117 args = p .parse_args ()
111118 sdbus .set_default_bus (sdbus .sd_bus_open_system ())
112- add_wifi_psk_connection_profile (args )
119+ connection_dpath = add_wifi_psk_connection_profile (args )
120+ print (f"Path of the new connection: { connection_dpath } " )
121+ print (f"UUID of the new connection: { args .uuid } " )
0 commit comments