Skip to content

Instantly share code, notes, and snippets.

@lonelycode
Last active August 29, 2015 14:21
Show Gist options
  • Save lonelycode/14a6f2dc6798cd38b1a6 to your computer and use it in GitHub Desktop.
Save lonelycode/14a6f2dc6798cd38b1a6 to your computer and use it in GitHub Desktop.
A plugin example for pie written in Python. Note: pyjsonrpc doesn;t work well with ID's of 0, needed to mod source to get it to work.
#!/usr/bin/python
from __future__ import print_function
import sys
import time
import pyjsonrpc
# pyjsonrpc has a bug in that it does not support id's of 0, you will need to modify it to work, change lines 214 in rpclib.py from:
```
for response in responses:
if response.id:
responses_.append(response.to_dict())
responses = responses_
```
# To:
```
for response in responses:
if response.id != None:
responses_.append(response.to_dict())
responses = responses_
```
# handy method to print warnings for us
def warning(*objs):
print("[plugin log] ", *objs, file=sys.stderr)
# RPC Server, simple library implementation
class JsonRpc(pyjsonrpc.JsonRpc):
# Adds an array of ints
@pyjsonrpc.rpcmethod
def add(self, ints):
"""Test method"""
v = 0
for add in ints:
v += add
return v
rpc = JsonRpc()
# Create a stdin-driven server
line = sys.stdin.readline()
while line:
this_input = line
warning(this_input)
out = rpc.call(this_input)
warning(out)
sys.stdout.write(out + "\n")
sys.stdout.flush()
line = sys.stdin.readline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment