Skip to content

Commit e59e823

Browse files
committed
Update the debug repl to offer line editing
1 parent 64869bf commit e59e823

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

docs/source/change_log.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Version 5.0.0
1414

1515
* **Breaking:** Dropped support for Python versions 3.6, 3.7 and 3.8
1616
* **Breaking:** Changed the precedence of operators to be more aligned with common programming languages
17+
* **Breaking:** The :ref:`debug-repl` utility now requires development dependencies to be installed
1718

1819
Version 4.x.x
1920
-------------
@@ -86,7 +87,7 @@ Version 3.6.0
8687
Released :release:`3.6.0` on June 16th, 2023
8788

8889
* Removed testing for Python versions 3.4 and 3.5 on GitHub Actions
89-
* Add regex error details to the debug REPL
90+
* Add regex error details to the :ref:`debug-repl` utility
9091
* Add support for Python-style comments
9192

9293
Version 3.5.0

lib/rule_engine/debug_repl.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,22 @@
3232

3333
import argparse
3434
import code
35-
import io
36-
import os
3735
import pprint
3836
import re
39-
import sys
4037
import textwrap
4138
import traceback
4239

4340
from . import __version__
4441
from . import engine
4542
from . 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

5752
def 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

Comments
 (0)