Fix IPython: check for old versions

This commit is contained in:
Pierre LALET 2017-10-18 14:19:54 +02:00
parent 32c8d95f7f
commit 6931cc9743
2 changed files with 50 additions and 37 deletions

View File

@ -17,11 +17,6 @@ import logging
from random import choice from random import choice
import types import types
try:
import IPython # Allow testing
except:
pass
# Never add any global import, in main.py, that would trigger a warning messsage # Never add any global import, in main.py, that would trigger a warning messsage
# before the console handlers gets added in interact() # before the console handlers gets added in interact()
from scapy.error import log_interactive, log_loading, log_scapy, warning from scapy.error import log_interactive, log_loading, log_scapy, warning
@ -477,44 +472,55 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
if mybanner is not None: if mybanner is not None:
the_banner += "\n" the_banner += "\n"
the_banner += mybanner the_banner += mybanner
IPYTHON=False if not conf.interactive_shell or conf.interactive_shell.lower() in [
if not conf.interactive_shell or conf.interactive_shell.lower() in ["ipython", "auto"]: "ipython", "auto"
]:
try: try:
IPython import IPython
IPYTHON=True from IPython.terminal.embed import InteractiveShellEmbed
except NameError as e: except ImportError:
log_loading.warning("IPython not available. Using standard Python shell instead. " log_loading.warning(
"AutoCompletion, History are disabled.") "IPython not available. Using standard Python shell "
IPYTHON=False "instead.\nAutoCompletion, History are disabled."
)
IPYTHON = False
else:
IPYTHON = True
else:
IPYTHON = False
init_session(session_name, mydict) init_session(session_name, mydict)
if IPYTHON: if IPYTHON:
banner = the_banner + " using IPython %s\n" % IPython.__version__ banner = the_banner + " using IPython %s\n" % IPython.__version__
from IPython.terminal.embed import InteractiveShellEmbed
from IPython.terminal.prompts import Prompts, Token
from traitlets.config.loader import Config
from scapy.packet import Packet
cfg = Config()
try: try:
get_ipython from traitlets.config.loader import Config
except NameError: except ImportError:
# Set "classic" prompt style when launched from run_scapy(.bat) files log_loading.warning(
apply_ipython_style(shell=cfg.TerminalInteractiveShell) # Register and apply scapy color+prompt style "traitlets not available. Some Scapy shell features won't be "
cfg.TerminalInteractiveShell.confirm_exit = False # Remove confirm exit "available."
cfg.TerminalInteractiveShell.separate_in = u'' # Remove spacing line )
ipshell = InteractiveShellEmbed(
cfg.TerminalInteractiveShell.hist_file = conf.histfile banner1=banner,
user_ns=SESSION,
# configuration can thus be specified here. )
ipshell = InteractiveShellEmbed(config=cfg, else:
banner1=banner, cfg = Config()
hist_file=conf.histfile if conf.histfile else None, try:
user_ns=SESSION) get_ipython
except NameError:
# Set "classic" prompt style when launched from run_scapy(.bat) files
# Register and apply scapy color+prompt style
apply_ipython_style(shell=cfg.TerminalInteractiveShell)
cfg.TerminalInteractiveShell.confirm_exit = False
cfg.TerminalInteractiveShell.separate_in = u''
cfg.TerminalInteractiveShell.hist_file = conf.histfile
# configuration can thus be specified here.
ipshell = InteractiveShellEmbed(config=cfg,
banner1=banner,
hist_file=conf.histfile if conf.histfile else None,
user_ns=SESSION)
ipshell(local_ns=SESSION) ipshell(local_ns=SESSION)
else: else:
code.interact(banner = the_banner, local=SESSION) code.interact(banner = the_banner, local=SESSION)

View File

@ -291,7 +291,14 @@ class HTMLTheme2(HTMLTheme):
def apply_ipython_style(shell): def apply_ipython_style(shell):
"""Updates the specified IPython console shell with """Updates the specified IPython console shell with
the conf.color_theme scapy theme.""" the conf.color_theme scapy theme."""
from IPython.terminal.prompts import Prompts, Token try:
from IPython.terminal.prompts import Prompts, Token
except:
from scapy.error import log_loading
log_loading.warning(
"IPython too old. Some Scapy shell features won't be available."
)
return
from scapy.config import conf from scapy.config import conf
if isinstance(conf.prompt, Prompts): if isinstance(conf.prompt, Prompts):
shell.prompts_class = conf.prompt # Set custom prompt style shell.prompts_class = conf.prompt # Set custom prompt style