/usr/lib/python3/dist-packages/pynlpl/net.py is in python3-pynlpl 1.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #-*- coding:utf-8 -*-
#---------------------------------------------------------------
# PyNLPl - Network utilities
# by Maarten van Gompel
# Centre for Language Studies
# Radboud University Nijmegen
# http://www.github.com/proycon/pynlpl
# proycon AT anaproy DOT nl
#
# Generic Server for Language Models
#
#----------------------------------------------------------------
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from pynlpl.common import u,b
import sys
if sys.version < '3':
from codecs import getwriter
stderr = getwriter('utf-8')(sys.stderr)
stdout = getwriter('utf-8')(sys.stdout)
else:
stderr = sys.stderr
stdout = sys.stdout
from twisted.internet import protocol, reactor # will fail on Python 3 for now
from twisted.protocols import basic
import shlex
class GWSNetProtocol(basic.LineReceiver):
def connectionMade(self):
print("Client connected", file=stderr)
self.factory.connections += 1
if self.factory.connections < 1:
self.transport.loseConnection()
else:
self.sendLine(b("READY"))
def lineReceived(self, line):
try:
if sys.version >= '3' and isinstance(line,bytes):
print("Client in: " + str(line,'utf-8'),file=stderr)
else:
print("Client in: " + line,file=stderr)
except UnicodeDecodeError:
print("Client in: (unicodeerror)",file=stderr)
if sys.version < '3':
if isinstance(line,unicode):
self.factory.processprotocol.transport.write(line.encode('utf-8'))
else:
self.factory.processprotocol.transport.write(line)
self.factory.processprotocol.transport.write(b('\n'))
else:
self.factory.processprotocol.transport.write(b(line) + b('\n'))
self.factory.processprotocol.currentclient = self
def connectionLost(self, reason):
self.factory.connections -= 1
if self.factory.processprotocol.currentclient == self:
self.factory.processprotocol.currentclient = None
class GWSFactory(protocol.ServerFactory):
protocol = GWSNetProtocol
def __init__(self, processprotocol):
self.connections = 0
self.processprotocol = processprotocol
class GWSProcessProtocol(protocol.ProcessProtocol):
def __init__(self, printstderr=True, sendstderr= False, filterout = None, filtererr = None):
self.currentclient = None
self.printstderr = printstderr
self.sendstderr = sendstderr
if not filterout:
self.filterout = lambda x: x
else:
self.filterout = filterout
if not filtererr:
self.filtererr = lambda x: x
else:
self.filtererr = filtererr
def connectionMade(self):
pass
def outReceived(self, data):
try:
if sys.version >= '3' and isinstance(data,bytes):
print("Process out " + str(data, 'utf-8'),file=stderr)
else:
print("Process out " + data,file=stderr)
except UnicodeDecodeError:
print("Process out (unicodeerror)",file=stderr)
print("DEBUG:", repr(b(data).strip().split(b('\n'))))
for line in b(data).strip().split(b('\n')):
line = self.filterout(line.strip())
if self.currentclient and line:
self.currentclient.sendLine(b(line))
def errReceived(self, data):
try:
if sys.version >= '3' and isinstance(data,bytes):
print("Process err " + str(data,'utf-8'), file=sys.stderr)
else:
print("Process err " + data,file=stderr)
except UnicodeDecodeError:
print("Process out (unicodeerror)",file=stderr)
if self.printstderr and data:
print(data.strip(),file=stderr)
for line in b(data).strip().split(b('\n')):
line = self.filtererr(line.strip())
if self.sendstderr and self.currentclient and line:
self.currentclient.sendLine(b(line))
def processExited(self, reason):
print("Process exited",file=stderr)
def processEnded(self, reason):
print("Process ended",file=stderr)
if self.currentclient:
self.currentclient.transport.loseConnection()
reactor.stop()
class GenericWrapperServer:
"""Generic Server around a stdin/stdout based CLI tool. Only accepts one client at a time to prevent concurrency issues !!!!!"""
def __init__(self, cmdline, port, printstderr= True, sendstderr= False, filterout = None, filtererr = None):
gwsprocessprotocol = GWSProcessProtocol(printstderr, sendstderr, filterout, filtererr)
cmdline = shlex.split(cmdline)
reactor.spawnProcess(gwsprocessprotocol, cmdline[0], cmdline)
gwsfactory = GWSFactory(gwsprocessprotocol)
reactor.listenTCP(port, gwsfactory)
reactor.run()
|