From 70002948c127d41f456a1ebac7a9d5020eb1735f Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Mon, 16 Nov 2020 16:16:41 -0500 Subject: Docs shortcut in script console --- python/scriptingprovider.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'python') 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')) -- cgit v1.3.1