Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix some bugs.
- Loading branch information
Keith Dart
committed
Nov 19, 2019
1 parent
1b66631
commit aff8f22
Showing
4 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters