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

cli: remove readline import #1165

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions pyroute2/cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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()
Expand Down Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions pyroute2/ndb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)

Expand Down