Skip to content

Commit

Permalink
cli: Merge pull request #1165 from svinota/cli-readline
Browse files Browse the repository at this point in the history
cli: remove readline import

Bug-Url: #1165
  • Loading branch information
svinota authored Feb 7, 2024
2 parents 19c6a42 + 85a5567 commit 5c8b1e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
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

0 comments on commit 5c8b1e4

Please sign in to comment.