-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
executable file
·70 lines (56 loc) · 1.67 KB
/
plugin.py
File metadata and controls
executable file
·70 lines (56 loc) · 1.67 KB
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
#!/usr/bin/env python3
import argparse
import json
import sys
# Spec that defines the plugin's capabilities
plugin = {
"name": "Calculator",
"directives": [
{
"name": "calc",
"doc": "An example directive for running Python expressions.",
"body": {
"type": "string",
"doc": "The expression to compute",
},
}
],
}
# Helper to publish result to stdout as JSON
def declare_result(content):
"""Declare result as JSON to stdout
:param content: content to declare as the result
"""
# Format result and write to stdout
json.dump(content, sys.stdout, indent=2)
# Successfully exit
raise SystemExit(0)
# Execute a directive
def run_directive(name, data):
"""Execute a directive with the given name and data
:param name: name of the directive to run
:param data: data of the directive to run
"""
assert name == "calc"
# Run user program
result = eval(data["body"], {**globals(), **vars(__import__("math"))})
result = {"type": "code", "value": str(result)}
return [result]
def main(argv=None):
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--role")
group.add_argument("--directive")
group.add_argument("--transform")
args = parser.parse_args(argv)
if args.directive:
data = json.load(sys.stdin)
declare_result(run_directive(args.directive, data))
elif args.transform:
raise NotImplementedError
elif args.role:
raise NotImplementedError
else:
declare_result(plugin)
if __name__ == "__main__":
main()