diff options
Diffstat (limited to 'examples/python/gi/setting-user-data.py')
-rwxr-xr-x | examples/python/gi/setting-user-data.py | 111 |
1 files changed, 57 insertions, 54 deletions
diff --git a/examples/python/gi/setting-user-data.py b/examples/python/gi/setting-user-data.py index fd86445b84..e21940743d 100755 --- a/examples/python/gi/setting-user-data.py +++ b/examples/python/gi/setting-user-data.py @@ -39,59 +39,60 @@ import sys import re import gi -gi.require_version('NM', '1.0') + +gi.require_version("NM", "1.0") from gi.repository import NM + def pr(v): import pprint + pprint.pprint(v, indent=4, depth=5, width=60) + def parse_args(): - args = { - 'set': False, - 'set-gobject': False, - 'filter': [], - 'data': [] - } + args = {"set": False, "set-gobject": False, "filter": [], "data": []} i = 1 while i < len(sys.argv): a = sys.argv[i] if i == 1: - if a in ['s', 'set']: - args['set'] = True + if a in ["s", "set"]: + args["set"] = True i += 1 continue - elif a in ['g', 'get']: - args['set'] = False + elif a in ["g", "get"]: + args["set"] = False i += 1 continue - if a in ['id', 'uuid']: - args['filter'].append((a, sys.argv[i+1])) + if a in ["id", "uuid"]: + args["filter"].append((a, sys.argv[i + 1])) i += 2 continue - if a in ['--set-gobject']: - args['set-gobject'] = True + if a in ["--set-gobject"]: + args["set-gobject"] = True i += 1 continue - if a == 'data': + if a == "data": i += 1 a = sys.argv[i] - if args['set']: - if a == '-d': - args['data'].append((sys.argv[i+1], None)) + if args["set"]: + if a == "-d": + args["data"].append((sys.argv[i + 1], None)) else: - args['data'].append((a, sys.argv[i+1])) + args["data"].append((a, sys.argv[i + 1])) i += 2 else: - args['data'].append(a) + args["data"].append(a) i += 1 return args + def connection_to_str(connection): - return '%s (%s)' % (connection.get_id(), connection.get_uuid()) + return "%s (%s)" % (connection.get_id(), connection.get_uuid()) + def connections_filter(connections, filter_data): connections = list(sorted(connections, key=connection_to_str)) @@ -102,20 +103,21 @@ def connections_filter(connections, filter_data): # them multiple times. l = [] for f in filter_data: - if f[0] == 'id': + if f[0] == "id": for c in connections: if f[1] == c.get_id(): l.append(c) else: - assert(f[0] == 'uuid') + assert f[0] == "uuid" for c in connections: if f[1] == c.get_uuid(): l.append(c) return l -def print_user_data(connection, data_allow_regex, data, prefix=''): + +def print_user_data(connection, data_allow_regex, data, prefix=""): s_u = connection.get_setting(NM.SettingUser) - n = 'none' + n = "none" keys_len = 0 keys = [] if s_u is not None: @@ -123,17 +125,17 @@ def print_user_data(connection, data_allow_regex, data, prefix=''): keys_len = len(all_keys) if data: for d in data: - if data_allow_regex and len(d) > 0 and d[0] == '~': + if data_allow_regex and len(d) > 0 and d[0] == "~": r = re.compile(d[1:]) keys.extend([k for k in all_keys if r.match(k)]) else: - keys.append (d) + keys.append(d) else: keys.extend(all_keys) - n = '%s' % (keys_len) + n = "%s" % (keys_len) - print('%s%s [%s]' % (prefix, connection_to_str(connection), n)) - dd = { } + print("%s%s [%s]" % (prefix, connection_to_str(connection), n)) + dd = {} if s_u is not None: dd = s_u.get_property(NM.SETTING_USER_DATA) for k in keys: @@ -145,7 +147,7 @@ def print_user_data(connection, data_allow_regex, data, prefix=''): else: print('%s MISSING: "%s"' % (prefix, k)) else: - assert(v == dd.get(k, None)) + assert v == dd.get(k, None) print('%s SET: "%s" = "%s"' % (prefix, k, v)) else: print('%s MISSING: "%s"' % (prefix, k)) @@ -155,20 +157,19 @@ def do_get(connections, data): first_line = True connections = list(connections) if not connections: - print('no matching connections (use id|uuid argument)') + print("no matching connections (use id|uuid argument)") sys.exit(1) for c in connections: if first_line: first_line = False else: - print('') + print("") print_user_data(c, True, data) + def do_set(connection, data, set_gobject): - print_user_data(connection, False, - [d[0] for d in data], - prefix = 'BEFORE: ') - print('') + print_user_data(connection, False, [d[0] for d in data], prefix="BEFORE: ") + print("") s_u = connection.get_setting(NM.SettingUser) if s_u is None: connection.add_setting(NM.SettingUser()) @@ -197,35 +198,37 @@ def do_set(connection, data, set_gobject): print('error setting key "%s" = "%s": %s' % (key, val, e)) sys.exit(1) - try: connection.commit_changes(True, None) except Exception as e: - print('failure to commit connection: %s' % (e)) + print("failure to commit connection: %s" % (e)) sys.exit(1) - print('') - print_user_data(connection, False, - [d[0] for d in data], - prefix = 'AFTER: ') + print("") + print_user_data(connection, False, [d[0] for d in data], prefix="AFTER: ") + ############################################################################### -if __name__ == '__main__': +if __name__ == "__main__": args = parse_args() nm_client = NM.Client.new(None) - connections = connections_filter(nm_client.get_connections(), args['filter']) + connections = connections_filter(nm_client.get_connections(), args["filter"]) - if args['set']: - if not args['data']: - print('Requires one or more arguments to set or delete') + if args["set"]: + if not args["data"]: + print("Requires one or more arguments to set or delete") sys.exit(1) if len(connections) != 1: - print('To set the user-data of a connection, exactly one connection must be selected via id|uuid. Instead, %s connection matched ([%s])' % - (len(connections), ', '.join([connection_to_str(c) for c in connections]))) + print( + "To set the user-data of a connection, exactly one connection must be selected via id|uuid. Instead, %s connection matched ([%s])" + % ( + len(connections), + ", ".join([connection_to_str(c) for c in connections]), + ) + ) sys.exit(1) - do_set(connections[0], args['data'], args['set-gobject']) + do_set(connections[0], args["data"], args["set-gobject"]) else: - do_get(connections, args['data']) - + do_get(connections, args["data"]) |