svinota / pyroute2

Python Netlink and PF_ROUTE library — network configuration and monitoring
https://pyroute2.org/
Other
930 stars 244 forks source link

Fix NDB Source object's counting and update documentation #1156

Closed pkulev closed 5 months ago

pkulev commented 6 months ago

The following code snippet gives me an error, but with other objects that works fine.

>>> from pyroute2 import NDB

>>> ndb = NDB()
>>> ndb.sources
AttributeError: type object 'Source' has no attribute '_count'
list(ndb.sources)
AttributeError: type object 'Source' has no attribute '_count'

With this patchset applied I got the following output:

>>> ndb.sources

NDB view for sources
Number of objects: 2

to list objects use .summary() or .dump()
    -> RecordSet (generator)
        -> Record

key: Union[Record, dict, spec]
to get objects use ...[key] / .__getitem__(key)
    -> RTNL_Object

>>> list(ndb.sources)
['localhost', 'localhost/nsmanager']

The reason behind the absence of this method is that Source subclasses dict, and there is no real interface in terms of typing that is common to Source and other NDB objects (which subclass RTNL_object). Therefore, with this PR, I am addressing only the consequence, not the root cause.

Also sources documentation has usage of an inexistent method .details(), so I gave some love to the docs too. The the one thing I'm not sure, is the following statement:

By default NDB starts with one local RTNL source names localhost

It seems I have 2 sources on my pretty default setup. So waiting for your guidance here :)

svinota commented 5 months ago

The two sources are the default RTNL source and the network namespaces manager, you can run ndb.sources.summary(), or even ndb.sources.dump().format("json")