#!/usr/bin/env python
# -*- coding: utf-8 -*-

import optparse
import time
import logging
import pexpect
from SimpleXMLRPCServer import SimpleXMLRPCServer

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

class Pipeline(object):
    def __init__(self, args):
        cmd = "java %s" % args
        logger.info(cmd)
        
        self.pipeline = pexpect.spawn(cmd)
        self.pipeline.expect("> ", timeout=500)
        logger.info("Model load done!")

    def parse(self, text):
        logger.info(text)
        while True:
            try:
                self.pipeline.read_nonblocking (4000, 0.3)
            except pexpect.TIMEOUT:
                break
        
        self.pipeline.sendline(text)

        # How much time should we give the parser to parse it?
        # the idea here is that you increase the timeout as a 
        # function of the text's length.
        # anything longer than 5 seconds requires that you also
        # increase timeout=5 in jsonrpc.py
        max_expected_time = min(40, 3 + len(text) / 20.0)
        end_time = time.time() + max_expected_time

        incoming = ""
        while True:
            try:
                incoming += self.pipeline.read_nonblocking(2000, 1)
                end = incoming.find("\n>")
                if end != -1:
                    incoming = incoming[:end]
                    break
                time.sleep(0.0001)
            except pexpect.TIMEOUT:
                if end_time - time.time() < 0:
                    logger.error("Error: Timeout with input '%s'" % (incoming))
                    return {'error': "timed out after %f seconds" % max_expected_time}
                else:
                    continue
            except pexpect.EOF:
                break
        
        logger.info("%s\n%s" % ('='*40, incoming))

        # try:
        #     results = parse_parser_results(incoming)
        # except Exception, e:
        #     if VERBOSE: 
        #         logger.debug(traceback.format_exc())
        #     raise e

        start = incoming.find("<sentences>")
        incoming = incoming[start:]
        return incoming

if __name__ == '__main__':
    """
    Example usage:

    ./script/pipeline_server.py  -P "-Xmx4g -cp transccg-0.2.jar enju.pipeline.Pipeline -annotators ssplit,kuromoji -numKbest 3"
    
    """
    
    parser = optparse.OptionParser(usage="%prog [OPTIONS]")
    parser.add_option('-p', '--port', default='8080',
                      help='Port to serve on (default: 8080)')
    parser.add_option('-H', '--host', default='127.0.0.1',
                      help='Host to serve on (default: 127.0.0.1. Use 0.0.0.0 to make public)')
    parser.add_option('-P', '--pipeline',
                      help='arguments required to run transccg pipeline.')
    options, args = parser.parse_args()

    server = SimpleXMLRPCServer((options.host, int(options.port)), logRequests=True)

    pipeline = Pipeline(options.pipeline)
    
    server.register_function(pipeline.parse)

    logger.info('Serving on http://%s:%s' % (options.host, options.port))
    server.serve_forever()

@@ -338,32 +338,72 @@
 fo(cmd)%0A        
+logger.info(%22Setting up a pipeline...%22)%0A
 %0A        self.pi
@@ -426,24 +426,41 @@
 .spawn(cmd)%0A
+        try:%0A    
         self
@@ -507,38 +507,255 @@
     
-logger.info(%22Model load done!%22
+except pexpect.exceptions.EOF:%0A            logger.error(%22Failed to set up the pipeline! Maybe the arguments given to jigg are corrupted?%22)%0A            logger.error(self.pipeline.before)%0A            exit(1)%0A%0A        logger.info(self.pipeline.before
 )%0A%0A 
@@ -956,32 +956,24 @@
       break%0A
-        
 %0A        sel
@@ -1123,17 +1123,16 @@
 out as a
- 
 %0A       
@@ -2044,24 +2044,16 @@
   break%0A
-        
 %0A       
@@ -2101,214 +2101,8 @@
 ))%0A%0A
-        # try:%0A        #     results = parse_parser_results(incoming)%0A        # except Exception, e:%0A        #     if VERBOSE: %0A        #         logger.debug(traceback.format_exc())%0A        #     raise e%0A%0A
     
@@ -2133,17 +2133,12 @@
 d(%22%3C
-sentences
+root
 %3E%22)%0A
@@ -2305,29 +2305,23 @@
 -cp 
-transccg-0.2
+jigg-*
 .jar 
-enju
+jigg
 .pip
@@ -2351,35 +2351,66 @@
 ors 
-ssplit,kuromoji -numKbest 3
+corenlp%5Btokenize,ssplit%5D -corenlp.tokenize.whitespace true
 %22%0A  
@@ -2857,15 +2857,13 @@
 run 
-transcc
+a jig
 g pi

Revise server