Skip to content

Commit 36d2c12

Browse files
committed
state -> receiver.
This made more sense before senders existed, but now everything sounds good again.
1 parent 9a67c03 commit 36d2c12

File tree

6 files changed

+68
-67
lines changed

6 files changed

+68
-67
lines changed

examples/protocol/netstring_reversal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from netstrings import grammar, NetstringSender
88

99

10-
class NetstringReverserState(object):
10+
class NetstringReverserReceiver(object):
1111
def __init__(self, sender, parser):
1212
self.sender = sender
1313

@@ -22,7 +22,7 @@ def netstringReceived(self, string):
2222

2323

2424
NetstringReverser = makeProtocol(
25-
grammar, NetstringSender, NetstringReverserState)
25+
grammar, NetstringSender, NetstringReverserReceiver)
2626

2727

2828
class NetstringReverserFactory(ServerFactory):

examples/protocol/netstrings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
netstring = digits:length ':' <anything{length}>:string ',' -> string
88
9-
initial = netstring:string -> state.netstringReceived(string)
9+
initial = netstring:string -> receiver.netstringReceived(string)
1010
1111
"""
1212

examples/protocol/test_netstrings.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_sending_two_netstrings():
5252
assert transport.value() == '4:spam,5:egggs,'
5353

5454

55-
class FakeState(object):
55+
class FakeReceiver(object):
5656
def __init__(self, sender, parser):
5757
self.sender = sender
5858
self.parser = parser
@@ -70,7 +70,7 @@ def connectionLost(self, reason):
7070
self.lossReason = reason
7171

7272
TestingNetstringProtocol = parsley.makeProtocol(
73-
netstrings.grammar, netstrings.NetstringSender, FakeState)
73+
netstrings.grammar, netstrings.NetstringSender, FakeReceiver)
7474

7575
def build_testing_protocol():
7676
protocol = TestingNetstringProtocol()
@@ -81,38 +81,38 @@ def build_testing_protocol():
8181
def test_receiving_empty_netstring():
8282
protocol, transport = build_testing_protocol()
8383
protocol.dataReceived('0:,')
84-
assert protocol.state.netstrings == ['']
84+
assert protocol.receiver.netstrings == ['']
8585

8686
def test_receiving_one_netstring_by_byte():
8787
protocol, transport = build_testing_protocol()
8888
for c in '4:spam,':
8989
protocol.dataReceived(c)
90-
assert protocol.state.netstrings == ['spam']
90+
assert protocol.receiver.netstrings == ['spam']
9191

9292
def test_receiving_two_netstrings_by_byte():
9393
protocol, transport = build_testing_protocol()
9494
for c in '4:spam,4:eggs,':
9595
protocol.dataReceived(c)
96-
assert protocol.state.netstrings == ['spam', 'eggs']
96+
assert protocol.receiver.netstrings == ['spam', 'eggs']
9797

9898
def test_receiving_two_netstrings_in_chunks():
9999
protocol, transport = build_testing_protocol()
100100
for c in ['4:', 'spa', 'm,4', ':eg', 'gs,']:
101101
protocol.dataReceived(c)
102-
assert protocol.state.netstrings == ['spam', 'eggs']
102+
assert protocol.receiver.netstrings == ['spam', 'eggs']
103103

104104
def test_receiving_two_netstrings_at_once():
105105
protocol, transport = build_testing_protocol()
106106
protocol.dataReceived('4:spam,4:eggs,')
107-
assert protocol.state.netstrings == ['spam', 'eggs']
107+
assert protocol.receiver.netstrings == ['spam', 'eggs']
108108

109109
def test_establishing_connection():
110-
assert not FakeState(None, None).connected
110+
assert not FakeReceiver(None, None).connected
111111
protocol, transport = build_testing_protocol()
112-
assert protocol.state.connected
112+
assert protocol.receiver.connected
113113

114114
def test_losing_connection():
115115
protocol, transport = build_testing_protocol()
116116
reason = object()
117117
protocol.connectionLost(reason)
118-
assert protocol.state.lossReason == reason
118+
assert protocol.receiver.lossReason == reason

ometa/protocol.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
class ParserProtocol(Protocol):
77
currentRule = 'initial'
88

9-
def __init__(self, grammar, senderFactory, stateFactory, bindings):
9+
def __init__(self, grammar, senderFactory, receiverFactory, bindings):
1010
self.grammar = grammar
1111
self.bindings = dict(bindings)
1212
self.senderFactory = senderFactory
13-
self.stateFactory = stateFactory
13+
self.receiverFactory = receiverFactory
1414
self.disconnecting = False
1515

1616
def setNextRule(self, rule):
1717
self.currentRule = rule
1818

1919
def connectionMade(self):
2020
self.sender = self.senderFactory(self.transport)
21-
self.bindings['state'] = self.state = self.stateFactory(self.sender, self)
22-
self.state.connectionMade()
21+
self.bindings['receiver'] = self.receiver = self.receiverFactory(
22+
self.sender, self)
23+
self.receiver.connectionMade()
2324
self._setupInterp()
2425

2526
def _setupInterp(self):
@@ -51,5 +52,5 @@ def dataReceived(self, data):
5152
def connectionLost(self, reason):
5253
if self.disconnecting:
5354
return
54-
self.state.connectionLost(reason)
55+
self.receiver.connectionLost(reason)
5556
self.disconnecting = True

ometa/test/test_protocol.py

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
testingGrammarSource = """
1515
16-
someA = ('a' 'a') -> state('a')
17-
someB = ('b' 'b') -> state('b')
18-
someC = ('c' 'c') -> state('c')
19-
someExc = 'e' -> state.raiseSomething()
16+
someA = ('a' 'a') -> receiver('a')
17+
someB = ('b' 'b') -> receiver('b')
18+
someC = ('c' 'c') -> receiver('c')
19+
someExc = 'e' -> receiver.raiseSomething()
2020
2121
initial = someA | someExc
2222
@@ -33,7 +33,7 @@ class SomeException(Exception):
3333
pass
3434

3535

36-
class StateFactory(object):
36+
class ReceiverFactory(object):
3737
def __init__(self, sender, parser):
3838
self.sender = sender
3939
self.parser = parser
@@ -69,7 +69,7 @@ class ParserProtocolTestCase(unittest.TestCase):
6969

7070
def setUp(self):
7171
self.protocol = ParserProtocol(
72-
testGrammar, SenderFactory, StateFactory, {})
72+
testGrammar, SenderFactory, ReceiverFactory, {})
7373

7474
def test_transportPassed(self):
7575
"""The sender is passed the transport recieved by the protocol."""
@@ -78,93 +78,93 @@ def test_transportPassed(self):
7878
self.assertEqual(transport, self.protocol.sender.transport)
7979

8080
def test_parserPassed(self):
81-
"""The protocol is passed to the state."""
81+
"""The protocol is passed to the receiver."""
8282
transport = object()
8383
self.protocol.makeConnection(transport)
84-
self.assertEqual(self.protocol, self.protocol.state.parser)
84+
self.assertEqual(self.protocol, self.protocol.receiver.parser)
8585

8686
def test_senderPassed(self):
87-
"""The sender is passed to the state."""
87+
"""The sender is passed to the receiver."""
8888
self.protocol.makeConnection(None)
89-
self.assertEqual(self.protocol.sender, self.protocol.state.sender)
89+
self.assertEqual(self.protocol.sender, self.protocol.receiver.sender)
9090

9191
def test_connectionEstablishes(self):
92-
"""connectionMade is called on the state after connection establishment."""
92+
"""connectionMade is called on the receiver after connection establishment."""
9393
self.protocol.makeConnection(None)
94-
self.assert_(self.protocol.state.connected)
94+
self.assert_(self.protocol.receiver.connected)
9595

9696
def test_basicParsing(self):
9797
"""Rules can be parsed multiple times for the same effect."""
9898
self.protocol.makeConnection(None)
9999
self.protocol.dataReceived('aa')
100-
self.assertEqual(self.protocol.state.calls, ['a'])
100+
self.assertEqual(self.protocol.receiver.calls, ['a'])
101101
self.protocol.dataReceived('aa')
102-
self.assertEqual(self.protocol.state.calls, ['a', 'a'])
102+
self.assertEqual(self.protocol.receiver.calls, ['a', 'a'])
103103

104104
def test_parsingChunks(self):
105105
"""Any number of rules can be called from one dataRecived."""
106106
self.protocol.makeConnection(None)
107107
self.protocol.dataReceived('a')
108-
self.assertEqual(self.protocol.state.calls, [])
108+
self.assertEqual(self.protocol.receiver.calls, [])
109109
self.protocol.dataReceived('aa')
110-
self.assertEqual(self.protocol.state.calls, ['a'])
110+
self.assertEqual(self.protocol.receiver.calls, ['a'])
111111
self.protocol.dataReceived('aaa')
112-
self.assertEqual(self.protocol.state.calls, ['a', 'a', 'a'])
112+
self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'a'])
113113

114114
def test_ruleSwitching(self):
115115
"""The rule being parsed can specify the next rule to be parsed."""
116116
self.protocol.makeConnection(None)
117-
self.protocol.state.returnMap.update(dict(a='someB', b='someA'))
117+
self.protocol.receiver.returnMap.update(dict(a='someB', b='someA'))
118118
self.protocol.dataReceived('aa')
119-
self.assertEqual(self.protocol.state.calls, ['a'])
119+
self.assertEqual(self.protocol.receiver.calls, ['a'])
120120
self.protocol.dataReceived('bb')
121-
self.assertEqual(self.protocol.state.calls, ['a', 'b'])
121+
self.assertEqual(self.protocol.receiver.calls, ['a', 'b'])
122122
self.protocol.dataReceived('aa')
123-
self.assertEqual(self.protocol.state.calls, ['a', 'b', 'a'])
123+
self.assertEqual(self.protocol.receiver.calls, ['a', 'b', 'a'])
124124

125125
def test_ruleSwitchingWithChunks(self):
126126
"""Any number of rules can be called even during rule switching."""
127127
self.protocol.makeConnection(None)
128-
self.protocol.state.returnMap.update(dict(a='someB', b='someA'))
128+
self.protocol.receiver.returnMap.update(dict(a='someB', b='someA'))
129129
self.protocol.dataReceived('a')
130-
self.assertEqual(self.protocol.state.calls, [])
130+
self.assertEqual(self.protocol.receiver.calls, [])
131131
self.protocol.dataReceived('ab')
132-
self.assertEqual(self.protocol.state.calls, ['a'])
132+
self.assertEqual(self.protocol.receiver.calls, ['a'])
133133
self.protocol.dataReceived('baa')
134-
self.assertEqual(self.protocol.state.calls, ['a', 'b', 'a'])
134+
self.assertEqual(self.protocol.receiver.calls, ['a', 'b', 'a'])
135135

136-
def test_ruleSwitchingViaState(self):
136+
def test_ruleSwitchingViaReceiver(self):
137137
"""
138-
The state is able to set the the next rule to be parsed with the parser
139-
passed to it.
138+
The receiver is able to set the the next rule to be parsed with the
139+
parser passed to it.
140140
"""
141141
self.protocol.makeConnection(None)
142142
self.protocol.dataReceived('aa')
143-
self.assertEqual(self.protocol.state.calls, ['a'])
143+
self.assertEqual(self.protocol.receiver.calls, ['a'])
144144
self.protocol.dataReceived('a')
145-
self.assertEqual(self.protocol.state.calls, ['a'])
146-
self.protocol.state.parser.setNextRule('someB')
145+
self.assertEqual(self.protocol.receiver.calls, ['a'])
146+
self.protocol.receiver.parser.setNextRule('someB')
147147
self.protocol.dataReceived('abb')
148-
self.assertEqual(self.protocol.state.calls, ['a', 'a', 'b'])
148+
self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'b'])
149149

150-
def test_ruleSwitchingViaStateGetsOverridden(self):
150+
def test_ruleSwitchingViaReceiverGetsOverridden(self):
151151
"""Returning a new rule takes priority over calling setNextRule."""
152152
self.protocol.makeConnection(None)
153153
self.protocol.dataReceived('aa')
154-
self.assertEqual(self.protocol.state.calls, ['a'])
154+
self.assertEqual(self.protocol.receiver.calls, ['a'])
155155
self.protocol.dataReceived('a')
156-
self.assertEqual(self.protocol.state.calls, ['a'])
157-
self.protocol.state.parser.setNextRule('someB')
158-
self.protocol.state.returnMap['a'] = 'someC'
156+
self.assertEqual(self.protocol.receiver.calls, ['a'])
157+
self.protocol.receiver.parser.setNextRule('someB')
158+
self.protocol.receiver.returnMap['a'] = 'someC'
159159
self.protocol.dataReceived('acc')
160-
self.assertEqual(self.protocol.state.calls, ['a', 'a', 'c'])
160+
self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'c'])
161161

162162
def test_connectionLoss(self):
163-
"""The reason for connection loss is forwarded to the state."""
163+
"""The reason for connection loss is forwarded to the receiver."""
164164
self.protocol.makeConnection(None)
165165
reason = object()
166166
self.protocol.connectionLost(reason)
167-
self.assertEqual(self.protocol.state.lossReason, reason)
167+
self.assertEqual(self.protocol.receiver.lossReason, reason)
168168

169169
def test_parseFailure(self):
170170
"""
@@ -174,21 +174,21 @@ def test_parseFailure(self):
174174
transport = FakeTransport()
175175
self.protocol.makeConnection(transport)
176176
self.protocol.dataReceived('b')
177-
self.failIfEqual(self.protocol.state.lossReason, None)
178-
self.failUnlessIsInstance(self.protocol.state.lossReason.value,
177+
self.failIfEqual(self.protocol.receiver.lossReason, None)
178+
self.failUnlessIsInstance(self.protocol.receiver.lossReason.value,
179179
ParseError)
180180
self.assert_(transport.aborted)
181181

182-
def test_exceptionsRaisedFromState(self):
182+
def test_exceptionsRaisedFromReceiver(self):
183183
"""
184-
Raising an exception from state methods called from the grammar
184+
Raising an exception from receiver methods called from the grammar
185185
propagate to connectionLost.
186186
"""
187187
transport = FakeTransport()
188188
self.protocol.makeConnection(transport)
189189
self.protocol.dataReceived('e')
190-
self.failIfEqual(self.protocol.state.lossReason, None)
191-
self.failUnlessIsInstance(self.protocol.state.lossReason.value,
190+
self.failIfEqual(self.protocol.receiver.lossReason, None)
191+
self.failUnlessIsInstance(self.protocol.receiver.lossReason.value,
192192
SomeException)
193193
self.assert_(transport.aborted)
194194

@@ -199,5 +199,5 @@ def test_dataIgnoredAfterDisconnection(self):
199199
reason = object()
200200
self.protocol.connectionLost(reason)
201201
self.protocol.dataReceived('d')
202-
self.assertEqual(self.protocol.state.lossReason, reason)
202+
self.assertEqual(self.protocol.receiver.lossReason, reason)
203203
self.assert_(not transport.aborted)

parsley.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def makeGrammar(source, bindings, name='Grammar', unwrap=False,
5252
return wrapGrammar(g, tracefunc=tracefunc)
5353

5454

55-
def makeProtocol(source, senderFactory, stateFactory, bindings=None,
55+
def makeProtocol(source, senderFactory, receiverFactory, bindings=None,
5656
name='Grammar'):
5757
"""
5858
Create a Protocol implementation from a Parsley grammar.
@@ -63,7 +63,7 @@ def makeProtocol(source, senderFactory, stateFactory, bindings=None,
6363
bindings = {}
6464
grammar = OMeta(source).parseGrammar(name)
6565
return functools.partial(
66-
ParserProtocol, grammar, senderFactory, stateFactory, bindings)
66+
ParserProtocol, grammar, senderFactory, receiverFactory, bindings)
6767

6868

6969
def unwrapGrammar(w):

0 commit comments

Comments
 (0)