33#
44# Example to create a new WiFi-PSK network connection profile:
55#
6- # $ add-wifi-psk-connection.py --help
6+ # $ examples/async/ add-wifi-psk-connection-async .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
4546
4647import asyncio
4748import functools
49+ import logging
4850import pprint
4951import sdbus
50- import uuid
52+ from uuid import uuid4
5153from argparse import ArgumentParser , Namespace
5254from sdbus_async .networkmanager import NetworkManagerSettings
5355from sdbus_async .networkmanager import NetworkManagerConnectionProperties
5456
5557
56- async def add_wifi_psk_connection_profile_async (args : Namespace ) -> None :
57- """Add a temporary (not yet saved) network connection profile"""
58+ async def add_wifi_psk_connection_profile_async (args : Namespace ) -> str :
59+ """Add a temporary (not yet saved) network connection profile
60+ :param Namespace args: autoconnect, conn_id, psk, save, ssid, uuid
61+ :return: dbus connection path of the created connection profile
62+ """
63+ info = logging .getLogger ().info
5864
5965 # If we add many connections passing the same id, things get messy. Check:
6066 if await NetworkManagerSettings ().get_connections_by_id (args .conn_id ):
6167 print (f'Connection "{ args .conn_id } " exists, remove it first' )
6268 print (f'Run: nmcli connection delete "{ args .conn_id } "' )
63- return
69+ return ""
6470
6571 properties : NetworkManagerConnectionProperties = {
6672 "connection" : {
6773 "id" : ("s" , args .conn_id ),
68- "uuid" : ("s" , str (uuid . uuid4 () )),
74+ "uuid" : ("s" , str (args . uuid )),
6975 "type" : ("s" , "802-11-wireless" ),
7076 "autoconnect" : ("b" , args .auto ),
7177 },
@@ -87,28 +93,30 @@ async def add_wifi_psk_connection_profile_async(args: Namespace) -> None:
8793 if args .interface_name :
8894 properties ["connection" ]["interface-name" ] = ("s" , args .interface_name )
8995
90- networkmanager_settings = NetworkManagerSettings ()
91- if args .save :
92- await networkmanager_settings .add_connection (properties )
93- print ("New connection profile created and saved, show it with:" )
94- else :
95- await networkmanager_settings .add_connection_unsaved (properties )
96- print ("New unsaved connection profile created, show it with:" )
97-
98- print (f'nmcli connection show "{ args .conn_id } "|grep -v -e -- -e default' )
99- print ("Settings used:" )
100- functools .partial (pprint .pprint , sort_dicts = False )(properties )
96+ s = NetworkManagerSettings ()
97+ addconnection = s .add_connection if args .save else s .add_connection_unsaved
98+ connection_settings_dbus_path = await addconnection (properties )
99+ created = "created and saved" if args .save else "created"
100+ info (f"New unsaved connection profile { created } , show it with:" )
101+ info (f'nmcli connection show "{ args .conn_id } "|grep -v -e -- -e default' )
102+ info ("Settings used:" )
103+ info (functools .partial (pprint .pformat , sort_dicts = False )(properties ))
104+ return connection_settings_dbus_path
101105
102106
103107if __name__ == "__main__" :
108+ logging .basicConfig (format = "%(message)s" , level = logging .INFO )
104109 p = ArgumentParser (description = "Optional arguments have example values:" )
105110 conn_id = "MyConnectionExample"
106111 p .add_argument ("-c" , dest = "conn_id" , default = conn_id , help = "Connection Id" )
112+ p .add_argument ("-u" , dest = "uuid" , default = uuid4 (), help = "Connection UUID" )
107113 p .add_argument ("-s" , dest = "ssid" , default = "CafeSSID" , help = "WiFi SSID" )
108114 p .add_argument ("-p" , dest = "psk" , default = "Coffee!!" , help = "WiFi PSK" )
109115 p .add_argument ("-i" , dest = "interface_name" , default = "" , help = "WiFi device" )
110116 p .add_argument ("-a" , dest = "auto" , action = "store_true" , help = "autoconnect" )
111117 p .add_argument ("--save" , dest = "save" , action = "store_true" , help = "Save" )
112118 args = p .parse_args ()
113119 sdbus .set_default_bus (sdbus .sd_bus_open_system ())
114- asyncio .run (add_wifi_psk_connection_profile_async (args ))
120+ connection_dpath = asyncio .run (add_wifi_psk_connection_profile_async (args ))
121+ print (f"Path of the new connection: { connection_dpath } " )
122+ print (f"UUID of the new connection: { args .uuid } " )
0 commit comments