# Path: nlglib/utils.py
# class LogPipe(threading.Thread):
#     """ A pipe that runs in a separate thread and logs incoming messages.
#     codereview.stackexchange.com/questions/6567/
#     how-to-redirect-a-subprocesses-output-stdout-and-stderr-to-logging-module
#     
#     """
# 
#     def __init__(self, log_fn):
#         """Setup the object with a logger and a loglevel
#         and start the thread
#         """
#         threading.Thread.__init__(self)
#         self.daemon = False
#         self.log_fn = log_fn
#         self.fdRead, self.fdWrite = os.pipe()
#         self.pipeReader = os.fdopen(self.fdRead)
# 
#     def fileno(self):
#         """Return the write file descriptor of the pipe
#         """
#         return self.fdWrite
# 
#     def run(self):
#         """Run the thread, logging everything.
#         """
#         for line in iter(self.pipeReader.readline, ''):
#             self.log_fn(line.strip('\n'))
# 
#         self.pipeReader.close()
# 
#     def close(self):
#         """Close the write end of the pipe.
#         """
#         os.close(self.fdWrite)
# 
#     def __enter__(self):
#         self.start()
#         return self
# 
#     def __exit__(self, exc_type, exc, tb):
#         self.close()
#         return False


import os
import socket
import struct
import subprocess
import threading
import time
import logging
import urllib.parse
from nlglib.utils import LogPipe
                raise ServerError(result)
            # decode xml symbols
            return urllib.parse.unquote_plus(result, encoding='utf-8')


class SimpleNLGServer(threading.Thread):
    """ A class that can be used as a SimpleNLG server. The actual java server
    is started as a subprocess and listens on the port 50007 for incoming XML
    requests for realisation.

    As the server starts in a separate thread, it requires certain
    synchronisation. In particular, the caller should call the start() method
    after creating the object and wait for the startup to finish.

    """

    def __init__(self, jar_path, port):
        super(SimpleNLGServer, self).__init__()
        log.debug('Creating simpleNLG server (%s)' % jar_path)
        log.debug('simpleNLG server port: ' + str(port))
        if not os.path.exists(jar_path):
            msg = 'The simpleNLG jar file "{}" does not exist.'
            raise ServerError(msg.format(jar_path))
        self.jar_path = jar_path
        self.port = str(port)
        self.start_cv = threading.Condition()
        self.exit_cv = threading.Condition()
        self._ready = False
        self._shutdown = False
        self.error_log = LogPipe(logging.getLogger('nlglib.simplenlg.server').error)

# Path: nlglib/utils.py
# class LogPipe(threading.Thread):
#     """ A pipe that runs in a separate thread and logs incoming messages.
#     codereview.stackexchange.com/questions/6567/
#     how-to-redirect-a-subprocesses-output-stdout-and-stderr-to-logging-module
#     
#     """
# 
#     def __init__(self, log_fn):
#         """Setup the object with a logger and a loglevel
#         and start the thread
#         """
#         threading.Thread.__init__(self)
#         self.daemon = False
#         self.log_fn = log_fn
#         self.fdRead, self.fdWrite = os.pipe()
#         self.pipeReader = os.fdopen(self.fdRead)
# 
#     def fileno(self):
#         """Return the write file descriptor of the pipe
#         """
#         return self.fdWrite
# 
#     def run(self):
#         """Run the thread, logging everything.
#         """
#         for line in iter(self.pipeReader.readline, ''):
#             self.log_fn(line.strip('\n'))
# 
#         self.pipeReader.close()
# 
#     def close(self):
#         """Close the write end of the pipe.
#         """
#         os.close(self.fdWrite)
# 
#     def __enter__(self):
#         self.start()
#         return self
# 
#     def __exit__(self, exc_type, exc, tb):
#         self.close()
#         return False


# Path: nlglib/realisation/simplenlg/client.py
import os
import socket
import struct
import subprocess
import threading
import time
import logging
import urllib.parse
from nlglib.utils import LogPipe

                raise ServerError(result)
            # decode xml symbols
            return urllib.parse.unquote_plus(result, encoding='utf-8')


class SimpleNLGServer(threading.Thread):
    """ A class that can be used as a SimpleNLG server. The actual java server
    is started as a subprocess and listens on the port 50007 for incoming XML
    requests for realisation.

    As the server starts in a separate thread, it requires certain
    synchronisation. In particular, the caller should call the start() method
    after creating the object and wait for the startup to finish.

    """

    def __init__(self, jar_path, port):
        super(SimpleNLGServer, self).__init__()
        log.debug('Creating simpleNLG server (%s)' % jar_path)
        log.debug('simpleNLG server port: ' + str(port))
        if not os.path.exists(jar_path):
            msg = 'The simpleNLG jar file "{}" does not exist.'
            raise ServerError(msg.format(jar_path))
        self.jar_path = jar_path
        self.port = str(port)
        self.start_cv = threading.Condition()
        self.exit_cv = threading.Condition()
        self._ready = False
        self._shutdown = False
        self.error_log = LogPipe(logging.getLogger('nlglib.simplenlg.server').error)

        self.output_log = LogPipe(logging.getLogger('nlglib.simplenlg.server').debug)