summaryrefslogtreecommitdiff
path: root/crates/nu_plugin_python/nu_plugin_python_example.py
blob: 4a4b2a6632c0068ab058767888a0108ad01628ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# Example of using a Python script as a Nushell plugin
#
# The example uses JSON encoding but it should be a similar process using
# 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
#
# To register the plugin use:
# 	plugin add <path-to-py-file>
#
# Be careful with the spans. Miette will crash if a span is outside the
# size of the contents vector. We strongly suggest using the span found in the
# plugin call head as in this example.
#
# 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
#
# Note: To keep the plugin simple and without dependencies, the dictionaries that
#   represent the data transferred between Nushell and the plugin are kept as
#   native Python dictionaries. The encoding and decoding process could be improved
#   by using libraries like pydantic and marshmallow
#
# This plugin uses python3
# Note: To debug plugins write to stderr using sys.stderr.write
import sys
import json


NUSHELL_VERSION = "0.97.0"
PLUGIN_VERSION = "0.1.1"  # bump if you change commands!


def signatures():
    """
    Multiple signatures can be sent to Nushell. Each signature will be registered
    as a different plugin function in Nushell.

    In your plugin logic you can use the name of the signature to indicate what
    operation should be done with the plugin
    """
    return {
        "Signature": [
            {
                "sig": {
                    "name": "nu-python",
                    "usage": "Signature test for Python",
                    "extra_usage": "",
                    "required_positional": [
                        {
                            "name": "a",
                            "desc": "required integer value",
                            "shape": "Int",
                        },
                        {
                            "name": "b",
                            "desc": "required string value",
                            "shape": "String",
                        },
                    ],
                    "optional_positional": [
                        {
                            "name": "opt",
                            "desc": "Optional number",
                            "shape": "Int",
                        }
                    ],
                    "rest_positional": {
                        "name": "rest",
                        "desc": "rest value string",
                        "shape": "String",
                    },
                    "named": [
                        {
                            "long": "help",
                            "short": "h",
                            "arg": None,
                            "required": False,
                            "desc": "Display the help message for this command",
                        },
                        {
                            "long": "flag",
                            "short": "f",
                            "arg": None,
                            "required": False,
                            "desc": "a flag for the signature",
                        },
                        {
                            "long": "named",
                            "short": "n",
                            "arg": "String",
                            "required": False,
                            "desc": "named string",
                        },
                    ],
                    "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",
                },
                "examples": [],
            }
        ]
    }


def process_call(id, plugin_call):
    """
    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
            - The input from the pipeline

    Use this information to implement your plugin logic
    """
    # Pretty printing the call to stderr
    sys.stderr.write(json.dumps(plugin_call, indent=4))
    sys.stderr.write("\n")

    # Get the span from the call
    span = plugin_call["call"]["head"]

    # Creates a Value of type List that will be encoded and sent to Nushell
    def f(x, y):
        return {"Int": {"val": x * y, "span": span}}

    value = {
        "Value": [
            {
                "List": {
                    "vals": [
                        {
                            "Record": {
                                "val": {
                                    "one": f(x, 0),
                                    "two": f(x, 1),
                                    "three": f(x, 2),
                                },
                                "span": span,
                            }
                        }
                        for x in range(0, 10)
                    ],
                    "span": span,
                }
            },
            None,
        ]
    }

    write_response(id, {"PipelineData": value})


def tell_nushell_encoding():
    sys.stdout.write(chr(4))
    for ch in "json":
        sys.stdout.write(chr(ord(ch)))
    sys.stdout.flush()


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
            "version": NUSHELL_VERSION,
            "features": [],
        }
    }
    sys.stdout.write(json.dumps(hello))
    sys.stdout.write("\n")
    sys.stdout.flush()


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()


def write_error(id, text, 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": {
                "msg": "ERROR from plugin",
                "labels": [
                    {
                        "text": text,
                        "span": span,
                    }
                ],
            }
        }
        if span is not None
        else {
            "Error": {
                "msg": "ERROR from plugin",
                "help": text,
            }
        }
    )
    write_response(id, error)


def handle_input(input):
    if "Hello" in input:
        if input["Hello"]["version"] != NUSHELL_VERSION:
            exit(1)
        else:
            return
    elif input == "Goodbye":
        exit(0)
    elif "Call" in input:
        [id, plugin_call] = input["Call"]
        if plugin_call == "Metadata":
            write_response(
                id,
                {
                    "Metadata": {
                        "version": PLUGIN_VERSION,
                    }
                },
            )
        elif plugin_call == "Signature":
            write_response(id, signatures())
        elif "Run" in plugin_call:
            process_call(id, plugin_call["Run"])
        else:
            write_error(id, "Operation not supported: " + str(plugin_call))
    else:
        sys.stderr.write("Unknown message: " + str(input) + "\n")
        exit(1)


def plugin():
    tell_nushell_encoding()
    tell_nushell_hello()
    for line in sys.stdin:
        input = json.loads(line)
        handle_input(input)


if __name__ == "__main__":
    if len(sys.argv) == 2 and sys.argv[1] == "--stdio":
        plugin()
    else:
        print("Run me from inside nushell!")