Created
September 10, 2015 19:59
-
-
Save osullivj/4196cfe28a4ca4d5ecfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) Babbington Slade 2015 www.spreadserve.com | |
# | |
import sys | |
import socket | |
import blockspring | |
# To run this at the command line... | |
# c:\python27\python bspring_client2.py --sheet=BlackScholes.xls --age=31 | |
# The JSON msg we send to SpreadServe's BlockSpringServer | |
CalcAction = { | |
"MessageType":"Calc", # Key the correct BlockSpringServer handler | |
"sheet":"BlackScholes.xls", | |
"action":"calc", # Key the correct sseng handler | |
"incells":["B5","B6","B7","B8","B9"], | |
"invals":["72.0","75.0","23.0","0.5","1.0"], | |
"outcells":["B16","B17","B18","B19","B20","B21","B22","B23"], | |
} | |
def param_to_list( request, pname): | |
# extract a parameter from the request object and convert to Python list | |
# eg 'B5,B6,B7' -> ['B5','B6','B7'] | |
rv = [] | |
ls = request.params.get( pname) | |
if ls: | |
rv = [str( lm) for lm in ls.split(',')] | |
return rv | |
def spreadserve( request, response): | |
host = request.params.get( "host") | |
CalcAction["sheet"] = str( '%s.xls' % request.params.get( "sheet")) | |
CalcAction["incells"] = param_to_list( request, "incells") | |
CalcAction["invals"] = param_to_list( request, "invals") | |
CalcAction["outcells"] = param_to_list( request, "outcells") | |
msg = str( CalcAction).replace( "'", '"') | |
try: | |
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect( ( host, 8891)) | |
sock.sendall( '%s\n' % msg) | |
# Nasty blocking call! | |
resp = sock.recv( 1024) | |
sock.close( ) | |
except BaseException, e: | |
rv = 'BADSOCK{%s)' % str(e) | |
response.addOutput( "result", rv) | |
response.end( ) | |
return | |
try: | |
ro = eval( resp) | |
if ro.get('result') == 'failed': | |
# Handle error msg from sseng or blocksvr | |
rv = '#SSERR(%s)' % ro.get('error') | |
else: | |
rv = ro.get('outvals') | |
except: | |
rv = '#BADEVAL(%s)' % resp | |
response.addOutput( "result", rv) | |
response.end( ) | |
blockspring.define( spreadserve) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment