Skip to content

Commit

Permalink
Add Python REPL mixin.
Browse files Browse the repository at this point in the history
Fix some bugs.
  • Loading branch information
Keith Dart committed Nov 19, 2019
1 parent 1b66631 commit aff8f22
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion elicit/fsm.py
Expand Up @@ -44,7 +44,7 @@ def push(self, obj):
self.stack.append(obj)

def pop(self):
self.stack.pop()
return self.stack.pop()

def add_default_transition(self, action, next_state):
if action is None and next_state is None:
Expand Down
3 changes: 1 addition & 2 deletions elicit/parser.py
Expand Up @@ -147,7 +147,6 @@ def feed(self, text):
val = sys.exc_info()[1]
self.pop_controller(val.value)
except exceptions.NewCommand as cmdex:

self.push_controller(controller.CommandController(cmdex.value))
if self._fsm.current_state: # non-zero, stuff left
self._buf = text[i:]
Expand Down Expand Up @@ -207,7 +206,7 @@ def _endvar(self, c, fsm):
else:
fsm.push(c)
try:
val = self._controller.environ.expand(fsm.varname)
val = self._controller.environ.expand(fsm.varname) or ""
except: # noqa
ex, val, tb = sys.exc_info()
self._controller._ui.error("Could not expand variable "
Expand Down
50 changes: 50 additions & 0 deletions elicit/python.py
@@ -0,0 +1,50 @@
# python3.7

# Copyright Ouster, Inc.

"""Python REPL mixin class for Command objects.
"""

from __future__ import annotations

import sys
import code
import readline

from elicit import completer


class PythonMixin:

def _get_ns(self):
if hasattr(self, "_obj"):
try:
name = self._obj.__class__.__name__.split(".")[-1].lower()
except: # noqa
name = "object"
return {name: self._obj, "environ": self._environ}
else:
return globals()

def python(self, arguments):
"""Enter an interactive interpreter.
Usage:
python
"""
ns = self._get_ns()
console = code.InteractiveConsole(ns)
console.raw_input = self._ui.user_input
try:
saveps1, saveps2 = sys.ps1, sys.ps2
except AttributeError:
saveps1, saveps2 = ">>> ", "... "
sys.ps1, sys.ps2 = "%GPython%N> ", "more> "
oc = readline.get_completer()
readline.set_completer(completer.Completer(ns).complete)
try:
console.interact(banner="You are now in Python. ^D exits.",
exitmsg="Resuming command shell.")
finally:
readline.set_completer(oc)
sys.ps1, sys.ps2 = saveps1, saveps2
8 changes: 4 additions & 4 deletions elicit/ui.py
Expand Up @@ -60,10 +60,10 @@ def close(self):

def set_theme(self, theme):
self._theme = theme
self.environ.setdefault("PS1", self._theme.ps1)
self.environ.setdefault("PS2", self._theme.ps2)
self.environ.setdefault("PS3", self._theme.ps3)
self.environ.setdefault("PS4", self._theme.ps4)
self.environ["PS1"] = self._theme.ps1
self.environ["PS2"] = self._theme.ps2
self.environ["PS3"] = self._theme.ps3
self.environ["PS4"] = self._theme.ps4

def clone(self, theme=None):
return self.__class__(self._io, self.environ.copy(), theme or self._theme)
Expand Down

0 comments on commit aff8f22

Please sign in to comment.