32
32
33
33
import argparse
34
34
import code
35
- import io
36
- import os
37
35
import pprint
38
36
import re
39
- import sys
40
37
import textwrap
41
38
import traceback
42
39
43
40
from . import __version__
44
41
from . import engine
45
42
from . import errors
46
43
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
56
51
57
52
def main ():
58
53
parser = argparse .ArgumentParser (description = 'Rule Engine: Debug REPL' , conflict_handler = 'resolve' )
@@ -77,6 +72,9 @@ def main():
77
72
parser .add_argument ('-v' , '--version' , action = 'version' , version = parser .prog + ' Version: ' + __version__ )
78
73
arguments = parser .parse_args ()
79
74
75
+ if not has_development_dependencies :
76
+ parser .error ('development dependencies are not installed, install them with: pipenv install --dev' )
77
+
80
78
context = engine .Context ()
81
79
thing = None
82
80
if arguments .edit_console or arguments .edit_file :
@@ -91,19 +89,21 @@ def main():
91
89
filename = arguments .edit_file .name ,
92
90
symbol = 'exec'
93
91
))
92
+ context = console .locals ['context' ]
93
+ thing = console .locals ['thing' ]
94
94
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 )
102
101
debugging = arguments .debug
102
+ session = PromptSession ()
103
103
104
104
while True :
105
105
try :
106
- rule_text = input ('rule > ' )
106
+ rule_text = session . prompt ('rule > ' )
107
107
except (EOFError , KeyboardInterrupt ):
108
108
break
109
109
0 commit comments