summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorGlenn Smith <glenn@vector35.com>2020-11-16 16:16:41 -0500
committerGlenn Smith <glenn@vector35.com>2020-11-16 16:51:24 -0500
commit70002948c127d41f456a1ebac7a9d5020eb1735f (patch)
treeb85a4fede02ab920bea340e11c460859e4ae8f7b /python
parent79c529b6b6ba9156229ff718f7a2456e51e6cc27 (diff)
Docs shortcut in script console
Diffstat (limited to 'python')
-rw-r--r--python/scriptingprovider.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/python/scriptingprovider.py b/python/scriptingprovider.py
index d8f87ba8..5ddc838f 100644
--- a/python/scriptingprovider.py
+++ b/python/scriptingprovider.py
@@ -529,6 +529,32 @@ class BlacklistedDict(dict):
self._blacklist_enabled = value
+def bninspect(code, globals_, locals_):
+ """
+ ``bninspect`` prints documentation about a command that is about to be run
+ The interpreter will invoke this function if you input a line ending in `?` e.g. `bv?`
+
+ :param str code: Python code to be evaluated
+ :param dict globals_: globals() from callsite
+ :param dict locals_: locals() from callsite
+ """
+ try:
+ import inspect
+ value = eval(code, globals_, locals_)
+ doc = inspect.getdoc(value)
+ if doc is None:
+ comments = inspect.getcomments(value)
+ if comments is None:
+ print(f"No documentation found for {code}")
+ else:
+ print(comments)
+ else:
+ print(doc)
+ except:
+ # Hide exceptions so the normal execution can report them
+ pass
+
+
class PythonScriptingInstance(ScriptingInstance):
_interpreter = threading.local()
@@ -612,6 +638,13 @@ class PythonScriptingInstance(ScriptingInstance):
try:
self.update_locals()
+ # If a single-line command ends in ?, show docs as well
+ if code[-2:] == b'?\n' and len(code.split(b'\n')) < 3:
+ escaped_code = repr(code[:-2])
+ self.interpreter.push(f'bninspect({escaped_code}, globals(), locals())\n')
+ # Strip ? from the evaluated input
+ code = code[:-2] + b'\n'
+
for line in code.split(b'\n'):
self.interpreter.push(line.decode('charmap'))