diff options
| author | Michael Biebl <biebl@debian.org> | 2020-06-28 18:58:14 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2020-06-28 18:58:14 +0200 |
| commit | a54ac63bbf9b2c71026ac9028a8ffaf186cf3c82 (patch) | |
| tree | 6a32883bd916c4096357b35298beff6db8bcd0b5 /examples/python/gi/nm-add-connection2.py | |
| parent | 45e8e1149027529194982212c804c0468aa01d98 (diff) | |
New upstream version 1.25.90 upstream/1.25.90
Diffstat (limited to 'examples/python/gi/nm-add-connection2.py')
| -rwxr-xr-x | examples/python/gi/nm-add-connection2.py | 109 |
1 files changed, 68 insertions, 41 deletions
diff --git a/examples/python/gi/nm-add-connection2.py b/examples/python/gi/nm-add-connection2.py index 01c1ae7b..f487e0bb 100755 --- a/examples/python/gi/nm-add-connection2.py +++ b/examples/python/gi/nm-add-connection2.py @@ -5,43 +5,49 @@ # import sys -import re import gi -gi.require_version('NM', '1.0') + +gi.require_version("NM", "1.0") from gi.repository import GLib, NM + def find_connections(nm_client, arg_type, arg_id): for c in nm_client.get_connections(): - if arg_type in [None, 'id'] and c.get_id() == arg_id: + if arg_type in [None, "id"] and c.get_id() == arg_id: yield c - if arg_type in [None, 'uuid'] and c.get_uuid() == arg_id: + if arg_type in [None, "uuid"] and c.get_uuid() == arg_id: yield c + def find_connection_first(nm_client, arg_type, arg_id): for f in find_connections(nm_client, arg_type, arg_id): return f + def con_to_str(con): s_con = con.get_setting_connection() return '"%s" (%s)' % (s_con.get_id(), s_con.get_uuid()) + def usage(): arg0 = sys.argv[0] - arg0_spaced = ' ' * len(arg0) - print('Usage: %s [ --clone ( [id] <id> | [uuid] <uuid> ) ] \\' % (arg0)) - print(' %s [ --to-disk | --in-memory ] \\' % (arg0_spaced)) - print(' %s [ --block-autoconnect ] \\' % (arg0_spaced)) - print(' %s [ --id <new-id> ] \\' % (arg0_spaced)) - print(' %s [ --uuid <new-uuid> ] \\' % (arg0_spaced)) + arg0_spaced = " " * len(arg0) + print("Usage: %s [ --clone ( [id] <id> | [uuid] <uuid> ) ] \\" % (arg0)) + print(" %s [ --to-disk | --in-memory ] \\" % (arg0_spaced)) + print(" %s [ --block-autoconnect ] \\" % (arg0_spaced)) + print(" %s [ --id <new-id> ] \\" % (arg0_spaced)) + print(" %s [ --uuid <new-uuid> ] \\" % (arg0_spaced)) return 1 + def die(msg, print_usage=False): print(msg) if print_usage: usage() sys.exit(1) + def main(): main_loop = GLib.MainLoop() @@ -57,56 +63,62 @@ def main(): argv = list(sys.argv[1:]) while argv: - if argv[0] == '--clone': + if argv[0] == "--clone": match_type = None if len(argv) < 2: - die('missing argument for --clone option') - if argv[0] in ['id', 'uuid']: + die("missing argument for --clone option") + if argv[0] in ["id", "uuid"]: match_type = argv[0] if len(argv) < 3: die('missing argument for "--clone %s" option' % (match_type)) argv = argv[1:] if cons: - die('cannot specify --clone argument more than once') + die("cannot specify --clone argument more than once") cons.extend(find_connections(nm_client, match_type, argv[1])) if len(cons) == 0: - die('could not find connection for "--clone %s%s"' % ((match_type or ''), argv[1])) + die( + 'could not find connection for "--clone %s%s"' + % ((match_type or ""), argv[1]) + ) if len(cons) != 1: - die('could not find unique connection for "--clone %s%s"' % ((match_type or ''), argv[1])) + die( + 'could not find unique connection for "--clone %s%s"' + % ((match_type or ""), argv[1]) + ) argv = argv[2:] continue - if argv[0] in ['--block-autoconnect']: + if argv[0] in ["--block-autoconnect"]: arg_block_autoconnect = NM.SettingsAddConnection2Flags.BLOCK_AUTOCONNECT argv = argv[1:] continue - if argv[0] in ['--to-disk', '--in-memory']: - if argv[0] == '--to-disk': + if argv[0] in ["--to-disk", "--in-memory"]: + if argv[0] == "--to-disk": v = NM.SettingsAddConnection2Flags.TO_DISK - elif argv[0] == '--in-memory': + elif argv[0] == "--in-memory": v = NM.SettingsAddConnection2Flags.IN_MEMORY else: - assert(False) + assert False if arg_mode is not None: die('duplicate storage modes ("%s")' % (argv[0])) arg_mode = v argv = argv[1:] continue - if argv[0] in ['--id']: + if argv[0] in ["--id"]: if len(argv) < 2: - die('missing argument for --id option') + die("missing argument for --id option") arg_id = argv[1] argv = argv[2:] continue - if argv[0] in ['--uuid']: + if argv[0] in ["--uuid"]: if len(argv) < 2: - die('missing argument for --uuid option') + die("missing argument for --uuid option") arg_uuid = argv[1] argv = argv[2:] continue die('unknown argument "%s"' % (argv[0])) if len(cons) != 1: - die('missing --clone argument', True) + die("missing --clone argument", True) con = cons[0] @@ -118,31 +130,46 @@ def main(): s_con.set_property(NM.SETTING_CONNECTION_UUID, arg_uuid or NM.utils_uuid_generate()) result = {} + def _add_connection2_cb(cl, async_result, user_data): try: c, r = nm_client.add_connection2_finish(async_result) except Exception as e: - result['error'] = e + result["error"] = e else: - result['result'] = r - result['connection'] = c + result["result"] = r + result["connection"] = c main_loop.quit() - nm_client.add_connection2(con2.to_dbus(NM.ConnectionSerializationFlags.ALL), - (arg_mode if arg_mode is not None else NM.SettingsAddConnection2Flags.TO_DISK) - | arg_block_autoconnect, - None, - False, - None, - _add_connection2_cb, - None) + nm_client.add_connection2( + con2.to_dbus(NM.ConnectionSerializationFlags.ALL), + (arg_mode if arg_mode is not None else NM.SettingsAddConnection2Flags.TO_DISK) + | arg_block_autoconnect, + None, + False, + None, + _add_connection2_cb, + None, + ) main_loop.run() - if 'error' in result: - die('update connection %s failed [%s]: %s' % (con_to_str(con2), ' '.join(sys.argv), result['error'])) + if "error" in result: + die( + "update connection %s failed [%s]: %s" + % (con_to_str(con2), " ".join(sys.argv), result["error"]) + ) + + print( + "update connection %s succeeded [%s]: %s, %s" + % ( + con_to_str(con2), + " ".join(sys.argv), + result["connection"].get_path(), + result["result"], + ) + ) - print('update connection %s succeeded [%s]: %s, %s' % (con_to_str(con2), ' '.join(sys.argv), result['connection'].get_path(), result['result'])) -if __name__ == '__main__': +if __name__ == "__main__": main() |