python-sdbus / python-sdbus-networkmanager

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

Add class ConnectionType #25

Closed bernhardkaindl closed 2 years ago

bernhardkaindl commented 2 years ago

Add enums.ConnectionType (on top of #24)

igo95862 commented 2 years ago

Hmm on a second thought..........

Is mypy going to complain about this? Will python-sdbus be able to encode it?

profile.connection.connection_type = ConnectionType.WIRELESS

Also stackoverflow gives me another example of StrEnum: https://stackoverflow.com/questions/58608361/string-based-enum-in-python/58608362#58608362

bernhardkaindl commented 2 years ago

@igo95862 Good catch!

Is mypy going to complain about this? Will python-sdbus be able to encode it?

profile.connection.connection_type = ConnectionType.WIRELESS

Yes it complains correcty. But it won't work this way with an Enum! (see below)

Also stackoverflow gives me another example of StrEnum: https://stackoverflow.com/questions/58608361/string-based-enum-in-python/58608362#58608362

Yes, I saw these as well and tried them:


Finally, I understood that Enum means that one has to use .value: :-1:

profile.connection.connection_type = ConnectionType.WIRELESS.value

Instead, I really want the simple use:

profile.connection.connection_type = ConnectionType.WIFI

To support this, I pushed the simple class (that I had used all along):

class ConnectionType:
    ...
    WIFI = "802-11-wireless"
    WPAN = "wpan"

BTW:

I renamed WIRELESS to WIFI now to be consistent with enum DeviceType:

class DeviceType(IntEnum):
    """Device Type..."""

https://github.com/python-sdbus/python-sdbus-networkmanager/blob/6815ceaaae9ae4e578497a9c6e87d681244e49a9/sdbus_async/networkmanager/enums.py#L514 and I need to derive the ConnectionType from the DeviceType string at runtime (see #26), so be in line with DeviceType.

PS:

The new commit makes use of ConnectionType.WIFI in examples/async/add-wifi-psk-connection-async.py:

     profile = ConnectionProfile(
         connection=ConnectionSettings(
             uuid=str(uuid.uuid4()),
-            connection_type='802-11-wireless',
+            connection_type=ConnectionType.WIFI,
             connection_id=args.conn_id,
             autoconnect=args.auto,
         ),

*This is working correctly and is checked with mypy --strict, and should be good to go.

igo95862 commented 2 years ago

Ok. I cherry picked cae9fbd3946bad3ad91d68b994f06746faa38a5b .