python-sdbus / python-sdbus-networkmanager

python-sdbus binds for NetworkManager
GNU Lesser General Public License v2.1
30 stars 6 forks source link

Show a list of wifi connections by autoconnect-prio #15

Closed bernhardkaindl closed 2 years ago

bernhardkaindl commented 2 years ago

This is a use-case where it is beneficial to read connection settings using object attributes.

In a loop over connections, I've these checks, which would be much harder to read when acessing connection settings by dict instead. In the case of settings.connection.autoconnect, which is an optional setting, defaulting to True, the code also has to be able to handle settings.connection.autoconnect not being set, which would make the dict access even harder to read:

    for connection_path in connection_paths:
        connection_service = NetworkConnectionSettings(connection_path)
        settings = NmMetaDict(await connection_service.get_settings())
        connection = settings.connection

        # Only connections with matching type and where autoconnect isn't False
        if connection.type != type or connection.autoconnect is False:
            continue

        # Only connections without interface-name or where it matches:
        if connection.interface_name and connection.interface_name != ifname:
            continue

        # Update the dict of timestamps from the last use of the connection
        conns[connection.timestamp] = connection.id

        # Update the dict of autoconnect-priorities and last-use timestamps
        if connection.autoconnect_priority not in prios:
            prios[connection.autoconnect_priority] = {}
        prios[connection.autoconnect_priority][connection.timestamp] = settings

This PR is mostly to provide a use-case for demonstrating this. The implementation of providing attributes in this example is kept to a minimum.

MetaDict is a good base layer for because it supports nested key access and the MetaDict object itself does not clutter the output when printing the nested MetaDict.

Features:

Prodict is similar and supports defining default values and data types, is also a possible base layer. It can also be extended to have variant tuples instead of plan values instead: https://github.com/ramazanpolat/prodict/pull/37

igo95862 commented 2 years ago

I assume this is no longer relevant with the settings helper