2023-02-18 14:27:24 +01:00
|
|
|
#!/usr/bin/env python
|
2022-04-04 03:00:53 +02:00
|
|
|
# Example of using a Python script as a Nushell plugin
|
2021-12-18 16:52:27 +01:00
|
|
|
#
|
|
|
|
# The example uses JSON encoding but it should be a similar process using
|
2022-09-29 00:06:43 +02:00
|
|
|
# msgpack to move data between Nushell and the plugin. The only difference
|
|
|
|
# would be that you need to use msgpack relative lib(like msgpack) to
|
|
|
|
# decode and encode information that is read and written to stdin and stdout
|
2021-12-18 16:52:27 +01:00
|
|
|
#
|
|
|
|
# To register the plugin use:
|
2022-09-29 00:06:43 +02:00
|
|
|
# register <path-to-py-file>
|
2021-12-18 16:52:27 +01:00
|
|
|
#
|
Fix typos by codespell (#7600)
# Description
Found via `codespell -S target -L
crate,ser,numer,falsy,ro,te,nd,bu,ndoes,statics,ons,fo,rouge,pard`
# User-Facing Changes
None.
# Tests + Formatting
None and done.
# After Submitting
None.
2022-12-26 08:31:26 +01:00
|
|
|
# Be careful with the spans. Miette will crash if a span is outside the
|
2024-02-25 23:32:50 +01:00
|
|
|
# size of the contents vector. We strongly suggest using the span found in the
|
|
|
|
# plugin call head as in this example.
|
2021-12-18 16:52:27 +01:00
|
|
|
#
|
2022-04-04 03:00:53 +02:00
|
|
|
# The plugin will be run using the active Python implementation. If you are in
|
|
|
|
# a Python environment, that is the Python version that is used
|
2021-12-18 16:52:27 +01:00
|
|
|
#
|
|
|
|
# Note: To keep the plugin simple and without dependencies, the dictionaries that
|
2022-04-04 03:00:53 +02:00
|
|
|
# represent the data transferred between Nushell and the plugin are kept as
|
|
|
|
# native Python dictionaries. The encoding and decoding process could be improved
|
2021-12-18 16:52:27 +01:00
|
|
|
# by using libraries like pydantic and marshmallow
|
|
|
|
#
|
2021-12-18 19:13:56 +01:00
|
|
|
# This plugin uses python3
|
2021-12-18 16:52:27 +01:00
|
|
|
# Note: To debug plugins write to stderr using sys.stderr.write
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
2024-04-05 16:24:00 +02:00
|
|
|
NUSHELL_VERSION = "0.92.2"
|
2024-03-08 13:04:22 +01:00
|
|
|
|
|
|
|
|
2021-12-18 16:52:27 +01:00
|
|
|
def signatures():
|
|
|
|
"""
|
2022-04-04 03:00:53 +02:00
|
|
|
Multiple signatures can be sent to Nushell. Each signature will be registered
|
|
|
|
as a different plugin function in Nushell.
|
2021-12-18 16:52:27 +01:00
|
|
|
|
|
|
|
In your plugin logic you can use the name of the signature to indicate what
|
|
|
|
operation should be done with the plugin
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
"Signature": [
|
|
|
|
{
|
2023-02-18 14:27:24 +01:00
|
|
|
"sig": {
|
|
|
|
"name": "nu-python",
|
|
|
|
"usage": "Signature test for Python",
|
|
|
|
"extra_usage": "",
|
|
|
|
"input_type": "Any",
|
|
|
|
"output_type": "Any",
|
|
|
|
"required_positional": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"desc": "required integer value",
|
|
|
|
"shape": "Int",
|
|
|
|
"var_id": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "b",
|
|
|
|
"desc": "required string value",
|
|
|
|
"shape": "String",
|
|
|
|
"var_id": None,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"optional_positional": [
|
|
|
|
{
|
|
|
|
"name": "opt",
|
|
|
|
"desc": "Optional number",
|
|
|
|
"shape": "Int",
|
|
|
|
"var_id": None,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"rest_positional": {
|
|
|
|
"name": "rest",
|
|
|
|
"desc": "rest value string",
|
2021-12-18 16:52:27 +01:00
|
|
|
"shape": "String",
|
|
|
|
"var_id": None,
|
|
|
|
},
|
2023-02-18 14:27:24 +01:00
|
|
|
"named": [
|
|
|
|
{
|
|
|
|
"long": "help",
|
|
|
|
"short": "h",
|
|
|
|
"arg": None,
|
|
|
|
"required": False,
|
|
|
|
"desc": "Display the help message for this command",
|
|
|
|
"var_id": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"long": "flag",
|
|
|
|
"short": "f",
|
|
|
|
"arg": None,
|
|
|
|
"required": False,
|
|
|
|
"desc": "a flag for the signature",
|
|
|
|
"var_id": None,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"long": "named",
|
|
|
|
"short": "n",
|
|
|
|
"arg": "String",
|
|
|
|
"required": False,
|
|
|
|
"desc": "named string",
|
|
|
|
"var_id": None,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"input_output_types": [["Any", "Any"]],
|
|
|
|
"allow_variants_without_examples": True,
|
|
|
|
"search_terms": ["Python", "Example"],
|
|
|
|
"is_filter": False,
|
|
|
|
"creates_scope": False,
|
|
|
|
"allows_unknown_args": False,
|
|
|
|
"category": "Experimental",
|
2021-12-18 16:52:27 +01:00
|
|
|
},
|
2023-02-18 14:27:24 +01:00
|
|
|
"examples": [],
|
2021-12-18 16:52:27 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
def process_call(id, plugin_call):
|
2021-12-18 16:52:27 +01:00
|
|
|
"""
|
|
|
|
plugin_call is a dictionary with the information from the call
|
|
|
|
It should contain:
|
|
|
|
- The name of the call
|
|
|
|
- The call data which includes the positional and named values
|
2022-04-04 03:00:53 +02:00
|
|
|
- The input from the pipeline
|
2021-12-18 16:52:27 +01:00
|
|
|
|
|
|
|
Use this information to implement your plugin logic
|
|
|
|
"""
|
|
|
|
# Pretty printing the call to stderr
|
2021-12-19 11:00:31 +01:00
|
|
|
sys.stderr.write(json.dumps(plugin_call, indent=4))
|
2021-12-18 16:52:27 +01:00
|
|
|
sys.stderr.write("\n")
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
# Get the span from the call
|
|
|
|
span = plugin_call["Run"]["call"]["head"]
|
|
|
|
|
2022-04-04 03:00:53 +02:00
|
|
|
# Creates a Value of type List that will be encoded and sent to Nushell
|
2024-02-25 23:32:50 +01:00
|
|
|
value = {
|
2021-12-18 16:52:27 +01:00
|
|
|
"Value": {
|
|
|
|
"List": {
|
|
|
|
"vals": [
|
|
|
|
{
|
|
|
|
"Record": {
|
2024-02-25 23:32:50 +01:00
|
|
|
"val": {
|
|
|
|
"cols": ["one", "two", "three"],
|
|
|
|
"vals": [
|
|
|
|
{
|
|
|
|
"Int": {
|
|
|
|
"val": x * y,
|
|
|
|
"span": span
|
|
|
|
}
|
|
|
|
} for y in [0, 1, 2]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"span": span
|
2021-12-18 16:52:27 +01:00
|
|
|
}
|
2024-02-25 23:32:50 +01:00
|
|
|
} for x in range(0, 10)
|
2021-12-18 16:52:27 +01:00
|
|
|
],
|
2024-02-25 23:32:50 +01:00
|
|
|
"span": span
|
2021-12-18 16:52:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
write_response(id, {"PipelineData": value})
|
|
|
|
|
2021-12-18 16:52:27 +01:00
|
|
|
|
2022-09-29 00:06:43 +02:00
|
|
|
def tell_nushell_encoding():
|
|
|
|
sys.stdout.write(chr(4))
|
|
|
|
for ch in "json":
|
|
|
|
sys.stdout.write(chr(ord(ch)))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
def tell_nushell_hello():
|
|
|
|
"""
|
|
|
|
A `Hello` message is required at startup to inform nushell of the protocol capabilities and
|
|
|
|
compatibility of the plugin. The version specified should be the version of nushell that this
|
|
|
|
plugin was tested and developed against.
|
|
|
|
"""
|
|
|
|
hello = {
|
|
|
|
"Hello": {
|
|
|
|
"protocol": "nu-plugin", # always this value
|
2024-03-08 13:04:22 +01:00
|
|
|
"version": NUSHELL_VERSION,
|
2024-02-25 23:32:50 +01:00
|
|
|
"features": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sys.stdout.write(json.dumps(hello))
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
sys.stdout.flush()
|
2021-12-18 16:52:27 +01:00
|
|
|
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
def write_response(id, response):
|
|
|
|
"""
|
|
|
|
Use this format to send a response to a plugin call. The ID of the plugin call is required.
|
|
|
|
"""
|
|
|
|
wrapped_response = {
|
|
|
|
"CallResponse": [
|
|
|
|
id,
|
|
|
|
response,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
sys.stdout.write(json.dumps(wrapped_response))
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
sys.stdout.flush()
|
2021-12-18 16:52:27 +01:00
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
|
|
|
|
def write_error(id, msg, span=None):
|
|
|
|
"""
|
|
|
|
Use this error format to send errors to nushell in response to a plugin call. The ID of the
|
|
|
|
plugin call is required.
|
|
|
|
"""
|
|
|
|
error = {
|
|
|
|
"Error": {
|
|
|
|
"label": "ERROR from plugin",
|
|
|
|
"msg": msg,
|
|
|
|
"span": span
|
2021-12-18 16:52:27 +01:00
|
|
|
}
|
2024-02-25 23:32:50 +01:00
|
|
|
}
|
|
|
|
write_response(id, error)
|
|
|
|
|
|
|
|
|
|
|
|
def handle_input(input):
|
|
|
|
if "Hello" in input:
|
2024-03-08 13:04:22 +01:00
|
|
|
if input["Hello"]["version"] != NUSHELL_VERSION:
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
return
|
2024-02-29 03:41:22 +01:00
|
|
|
elif input == "Goodbye":
|
|
|
|
return
|
2024-02-25 23:32:50 +01:00
|
|
|
elif "Call" in input:
|
|
|
|
[id, plugin_call] = input["Call"]
|
|
|
|
if "Signature" in plugin_call:
|
|
|
|
write_response(id, signatures())
|
|
|
|
elif "Run" in plugin_call:
|
|
|
|
process_call(id, plugin_call)
|
|
|
|
else:
|
|
|
|
write_error(id, "Operation not supported: " + str(plugin_call))
|
|
|
|
else:
|
|
|
|
sys.stderr.write("Unknown message: " + str(input) + "\n")
|
|
|
|
exit(1)
|
2021-12-18 16:52:27 +01:00
|
|
|
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
def plugin():
|
|
|
|
tell_nushell_encoding()
|
|
|
|
tell_nushell_hello()
|
|
|
|
for line in sys.stdin:
|
|
|
|
input = json.loads(line)
|
|
|
|
handle_input(input)
|
|
|
|
|
2021-12-18 16:52:27 +01:00
|
|
|
if __name__ == "__main__":
|
2024-02-25 23:32:50 +01:00
|
|
|
if len(sys.argv) == 2 and sys.argv[1] == "--stdio":
|
|
|
|
plugin()
|
|
|
|
else:
|
|
|
|
print("Run me from inside nushell!")
|