From 85a5567cf42d5ba38a110352b2f66c8a9e61ddd1 Mon Sep 17 00:00:00 2001 From: Peter Saveliev Date: Wed, 31 Jan 2024 17:08:31 +0100 Subject: [PATCH] cli: move readline import Remove readline import from the module and move it to the script. Thus the module will not affect other projects that use readline. Bug-Url: https://github.com/svinota/pyroute2/issues/1161 --- pyroute2/cli/console.py | 17 +++++------------ pyroute2/ndb/cli.py | 6 ++++++ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pyroute2/cli/console.py b/pyroute2/cli/console.py index d643177db..7c44ff5fd 100644 --- a/pyroute2/cli/console.py +++ b/pyroute2/cli/console.py @@ -6,17 +6,9 @@ from pyroute2.cli.session import Session from pyroute2.ndb.main import NDB -try: - import readline - - HAS_READLINE = True -except ImportError: - HAS_READLINE = False - class Console(code.InteractiveConsole): def __init__(self, stdout=None, log=None, sources=None): - global HAS_READLINE self.db = NDB(log=log, sources=sources) self.db.config.update( {'show_format': 'json', 'recordset_pipe': 'true'} @@ -28,10 +20,6 @@ def __init__(self, stdout=None, log=None, sources=None): self.prompt = '' self.set_prompt() code.InteractiveConsole.__init__(self) - if HAS_READLINE: - readline.parse_and_bind('tab: complete') - readline.set_completer(self.completer) - readline.set_completion_display_matches_hook(self.display) def close(self): self.db.close() @@ -92,6 +80,11 @@ def interact(self, readfunc=None): self.showtraceback() continue + def set_completer(self, readline): + readline.parse_and_bind('tab: complete') + readline.set_completer(self.completer) + readline.set_completion_display_matches_hook(self.display) + def completer(self, text, state): if state == 0: d = [x for x in dir(self.session.ptr) if x.startswith(text)] diff --git a/pyroute2/ndb/cli.py b/pyroute2/ndb/cli.py index 87c5775ec..9c8d7f88f 100755 --- a/pyroute2/ndb/cli.py +++ b/pyroute2/ndb/cli.py @@ -14,6 +14,10 @@ from pyroute2.cli.auth.auth_radius import RadiusAuthManager except ImportError: RadiusAuthManager = None +try: + import readline +except ImportError: + readline = None def run(): @@ -61,6 +65,8 @@ def run(): return 0 else: console = Console(log=args.l, sources=sources) + if readline is not None: + console.set_completer(readline) if args.r: console.loadrc(args.r)