Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Console' object has no attribute '_use_filter' #1314

Open
michaelcerne opened this issue Jul 12, 2024 · 2 comments
Open

'Console' object has no attribute '_use_filter' #1314

michaelcerne opened this issue Jul 12, 2024 · 2 comments

Comments

@michaelcerne
Copy link

michaelcerne commented Jul 12, 2024

The Console object does not have the _use_filter attribute that the regular Device class provides.

The Table class expects this to be available. An error is thrown initializing a Table using a Console.

Reproduce with:

from jnpr.junos import Device
from jnpr.junos.op.ethport import EthPortTable

with Device(host="xxx", port=21, user="xxx", passwd="xxx", mode="telnet") as dev:
    EthPortTable(dev).get()

Yields:

Traceback (most recent call last):
  File "<test_file>", line 5, in <module>
    EthPortTable(dev).get()
    ^^^^^^^^^^^^^^^^^
  File "<package_install_dir>\jnpr\junos\factory\table.py", line 37, in __init__
    self._use_filter = self._use_filter and self._dev._use_filter
                                            ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Console' object has no attribute '_use_filter'
@michaelcerne
Copy link
Author

michaelcerne commented Jul 12, 2024

Additionally, manually setting the _use_filter attribute on the allows us to proceed, but the test code then fails with:

File "<package_install_dir>\jnpr\junos\console.py", line 276, in _rpc_reply
    rpc_rsp_e = NCElement(
File "<package_install_dir>\ncclient\xml_.py", line 177, in __init__
    self.__doc = self.__transform_reply(result._root)
                                        ^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_root'

This issue is mitigated by modifying tty_netconf's rpc method to return reply rather than return rsp. rsp is a string, and of course, does not possess a _root attribute.

@michaelcerne
Copy link
Author

michaelcerne commented Jul 12, 2024

The following code manually sets the missing Console attribute and monkey patches the rpc method to return as needed. When executed, the expected output (identical to using the Device class over SSH) is returned.

from jnpr.junos import Device
from jnpr.junos.op.ethport import EthPortTable
from jnpr.junos.transport.tty_netconf import tty_netconf
from ncclient.operations.rpc import RPCReply

# Fixes the rpc reply _root error.
old_rpc_pre_patch = tty_netconf.rpc
def rpc(self, cmd):
    original_response = old_rpc_pre_patch(self, cmd)
    updated_response = RPCReply(original_response, huge_tree=self._tty._huge_tree)
    updated_response.parse()
    return updated_response
tty_netconf.rpc = rpc

with Device(host="xxx", port=21, user="xxx", passwd="xxx", mode="telnet") as dev:

    # Fixes the _use_filter missing issue.
        if not hasattr(dev, "_use_filter"):
            dev._use_filter = False

    EthPortTable(dev).get() # Returns as expected.

If similar changes were introduced upstream, both issues would be corrected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants