python-sdbus / python-sdbus-networkmanager

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

ActiveConnections list #56

Closed Regela closed 1 year ago

Regela commented 1 year ago

Need possibility to get list ActiveConnections

bash sample:

qdbus --literal --system org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.ActiveConnections
[Variant: [Argument: ao {[ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/24], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/21], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/19], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/18], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/17], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/16], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/15], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/14], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/13], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/12], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/11], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/10], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/9], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/8], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/7], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/6], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/5], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/4], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/3], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/2], [ObjectPath: /org/freedesktop/NetworkManager/ActiveConnection/1]}]]

qdbus --literal --system org.freedesktop.NetworkManager /org/freedesktop/NetworkManager  
...
property read QList<QDBusObjectPath> org.freedesktop.NetworkManager.ActiveConnections
...

How i can do it? Thanks

Regela commented 1 year ago

This example doing that i need:

import asyncio

import sdbus
from sdbus import DbusInterfaceCommonAsync, dbus_property_async

# qdbus --literal --system org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.ActiveConnections
class ListActiveInterface(DbusInterfaceCommonAsync,
                          interface_name='org.freedesktop.NetworkManager'
                          ):
    @dbus_property_async(
        property_signature='aa{sv}',
        property_name='ActiveConnections'
    )
    def active_connections(self) -> dict:
        return {}

async def list_active_connections():
    active_connections = ListActiveInterface.new_proxy('org.freedesktop.NetworkManager',
                                                       '/org/freedesktop/NetworkManager')
    return await active_connections.active_connections

async def main():
    print(await list_active_connections())

if __name__ == '__main__':
    sdbus.set_default_bus(sdbus.sd_bus_open_system())
    asyncio.run(main())
Regela commented 1 year ago

Sorry, i don't found it

from sdbus_async.networkmanager import NetworkManager
async def main():
    nm = NetworkManager()
    connections_paths: List[str] = await nm.active_connections
    print(connections_paths)

Thank you for your library!

igo95862 commented 1 year ago

Yes, the nm.active_connections property contains the list of all active connection object paths.

You probably want to initialize the active connection proxy objects.

active_connections = [ActiveConnection(x) for x in connections_paths]
igo95862 commented 1 year ago

@Regela BTW you should use the triple backtick ``` for code blocks.