soap_client.py 2.2 KB
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Michał Lenart
# This is SOAP client for Multiservice platform.
# It is available on the same license as the Multiservice itself.
#

from optparse import OptionParser
import time
import logging
import sys
import codecs
import suds


logging.basicConfig(level=logging.INFO)

sys.stdin = codecs.getreader('utf8')(sys.stdin)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
sys.stdout = codecs.getwriter('utf8')(sys.stdout)

def _getClient(host, port):
    url = 'http://%s:%d/WebService-1.0/ClarinWS?wsdl' % (host, port)
    return suds.client.Client(url)


def _getParts(client, serviceNames):
    partsList = client.factory.create('RequestPartsList')
    for serviceName in serviceNames:
        part = client.factory.create('RequestPart')
        part['serviceName'] = serviceName.decode('utf8')
        partsList.part.append(part)
    return partsList


def go():
    parser = OptionParser()
    parser.add_option('-p', '--port', type='int', action='store',
                      dest='port', default=80,
                      help='multiservice port; default: 80')
    parser.add_option('--host', type='string', action='store',
                      dest='host', default='ws.multiservice.nlp.ipipan.waw.pl',
                      help='multiservice host; default: ws.multiservice.nlp.ipipan.waw.pl')
    (opts, args) = parser.parse_args()

    if len(args) == 0:
    	print "Processing chain was not specified!"
    	return
    
    client = _getClient(opts.host, opts.port)

    token = client.service.analyzeChain(
        text=sys.stdin.read(),
        parts=_getParts(client, args),
        inputFormat='TEXT',
        outputFormat='TEXT')

    logging.info('TOKEN ' + str(token))

    status = client.service.getStatus(token)

    logging.info(u'STATUS ' + str(status))
    while not status in ['DONE', 'FAILED']:
        time.sleep(0.1)
        status = client.service.getStatus(token)
        logging.info(u'STATUS ' + str(status))

    if status == 'DONE':
        res = client.service.getResult(token)
        print res
    else:
        res = client.service.getException(token)
        logging.error('ERROR')
        print >> sys.stderr, res

if __name__ == '__main__':
    go()