soap_client.py 2.18 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-SNAPSHOT/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='port to run the service; default: 80')
    parser.add_option('--host', type='string', action='store',
                      dest='host', default='ws.multiservice.nlp.ipipan.waw.pl',
                      help='host to run the service; default: ws.multiservice.nlp.ipipan.waw.pl')
    (opts, args) = parser.parse_args()

    t1 = time.time()

    client = _getClient(opts.host, opts.port)

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

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

    t2 = time.time()
    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()