3232
3333import argparse
3434import code
35- import io
36- import os
3735import pprint
3836import re
39- import sys
4037import textwrap
4138import traceback
4239
4340from . import __version__
4441from . import engine
4542from . import errors
4643
47- def _console_interact (console , * args , ** kwargs ):
48- # see: https://bugs.python.org/issue34115
49- stdin = os .dup (0 )
50- try :
51- console .interact (* args , ** kwargs )
52- except SystemExit :
53- if 'exitmsg' in kwargs :
54- print (kwargs ['exitmsg' ])
55- sys .stdin = io .TextIOWrapper (io .BufferedReader (io .FileIO (stdin , mode = 'rb' , closefd = False )))
44+ try :
45+ from IPython import embed
46+ from prompt_toolkit import PromptSession
47+ except ImportError :
48+ has_development_dependencies = False
49+ else :
50+ has_development_dependencies = True
5651
5752def main ():
5853 parser = argparse .ArgumentParser (description = 'Rule Engine: Debug REPL' , conflict_handler = 'resolve' )
@@ -77,6 +72,9 @@ def main():
7772 parser .add_argument ('-v' , '--version' , action = 'version' , version = parser .prog + ' Version: ' + __version__ )
7873 arguments = parser .parse_args ()
7974
75+ if not has_development_dependencies :
76+ parser .error ('development dependencies are not installed, install them with: pipenv install --dev' )
77+
8078 context = engine .Context ()
8179 thing = None
8280 if arguments .edit_console or arguments .edit_file :
@@ -91,19 +89,21 @@ def main():
9189 filename = arguments .edit_file .name ,
9290 symbol = 'exec'
9391 ))
92+ context = console .locals ['context' ]
93+ thing = console .locals ['thing' ]
9494 if arguments .edit_console :
95- _console_interact (
96- console ,
97- banner = 'edit the \' context\' and \' thing\' objects as necessary' ,
98- exitmsg = 'exiting the edit console...'
99- )
100- context = console .locals ['context' ]
101- thing = console .locals ['thing' ]
95+ namespace = {'context' : context , 'thing' : thing }
96+ print ("Starting IPython shell..." )
97+ print ("Edit the \' context\' and \' thing\' objects as necessary" )
98+ embed (colors = "neutral" , user_ns = namespace )
99+ context = namespace .get ('context' , context )
100+ thing = namespace .get ('thing' , thing )
102101 debugging = arguments .debug
102+ session = PromptSession ()
103103
104104 while True :
105105 try :
106- rule_text = input ('rule > ' )
106+ rule_text = session . prompt ('rule > ' )
107107 except (EOFError , KeyboardInterrupt ):
108108 break
109109
0 commit comments