/usr/share/pyshared/ometa/protocol.py is in python-parsley 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 | from twisted.internet.protocol import Protocol
from twisted.python.failure import Failure
from ometa.tube import TrampolinedParser
class ParserProtocol(Protocol):
"""
A Twisted ``Protocol`` subclass for parsing stream protocols.
"""
def __init__(self, grammar, senderFactory, receiverFactory, bindings):
"""
Initialize the parser.
:param grammar: An OMeta grammar to use for parsing.
:param senderFactory: A unary callable that returns a sender given a
transport.
:param receiverFactory: A unary callable that returns a receiver given
a sender.
:param bindings: A dict of additional globals for the grammar rules.
"""
self._grammar = grammar
self._bindings = dict(bindings)
self._senderFactory = senderFactory
self._receiverFactory = receiverFactory
self._disconnecting = False
def connectionMade(self):
"""
Start parsing, since the connection has been established.
"""
self.sender = self._senderFactory(self.transport)
self.receiver = self._receiverFactory(self.sender)
self.receiver.prepareParsing(self)
self._parser = TrampolinedParser(
self._grammar, self.receiver, self._bindings)
def dataReceived(self, data):
"""
Receive and parse some data.
:param data: A ``str`` from Twisted.
"""
if self._disconnecting:
return
try:
self._parser.receive(data)
except Exception:
self.connectionLost(Failure())
self.transport.abortConnection()
return
def connectionLost(self, reason):
"""
Stop parsing, since the connection has been lost.
:param reason: A ``Failure`` instance from Twisted.
"""
if self._disconnecting:
return
self.receiver.finishParsing(reason)
self._disconnecting = True
|