Mercurial > code > home > repos > homeauto
annotate lib/twisted_sse_demo/sse_client.py @ 445:975384ebd88e
remove more crochet usage
Ignore-this: 3e29b61bc4b44aff46407f19166a7072
author | drewp@bigasterisk.com |
---|---|
date | Thu, 18 Apr 2019 16:55:27 -0700 |
parents | 7716b1810d6c |
children | 3d51d4b63497 |
rev | line source |
---|---|
294
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
1 from twisted.protocols.basic import LineReceiver |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
3 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
4 class EventSourceProtocol(LineReceiver): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
5 def __init__(self): |
329 | 6 self.MAX_LENGTH = 1 << 20 |
294
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
7 self.callbacks = {} |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
8 self.finished = None |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
9 # Initialize the event and data buffers |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
10 self.event = 'message' |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
11 self.data = '' |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
12 |
316
02d9915b3bbb
patchsource accept much longer lines from sse_collector
drewp@bigasterisk.com
parents:
294
diff
changeset
|
13 def lineLengthExceeded(self, line): |
02d9915b3bbb
patchsource accept much longer lines from sse_collector
drewp@bigasterisk.com
parents:
294
diff
changeset
|
14 print "line too long" |
02d9915b3bbb
patchsource accept much longer lines from sse_collector
drewp@bigasterisk.com
parents:
294
diff
changeset
|
15 raise NotImplementedError |
02d9915b3bbb
patchsource accept much longer lines from sse_collector
drewp@bigasterisk.com
parents:
294
diff
changeset
|
16 |
294
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
17 def setFinishedDeferred(self, d): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
18 self.finished = d |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
20 def addCallback(self, event, func): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
21 self.callbacks[event] = func |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
23 def lineReceived(self, line): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
24 if line == '': |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
25 # Dispatch event |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
26 self.dispatchEvent() |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
27 else: |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
28 try: |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
29 field, value = line.split(':', 1) |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
30 # If value starts with a space, strip it. |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
31 value = lstrip(value) |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
32 except ValueError: |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
33 # We got a line with no colon, treat it as a field(ignore) |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
34 return |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
36 if field == '': |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
37 # This is a comment; ignore |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
38 pass |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
39 elif field == 'data': |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
40 self.data += value + '\n' |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
41 elif field == 'event': |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
42 self.event = value |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
43 elif field == 'id': |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
44 # Not implemented |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
45 pass |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
46 elif field == 'retry': |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
47 # Not implemented |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
48 pass |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
49 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
50 def connectionLost(self, reason): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
51 if self.finished: |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
52 self.finished.callback(None) |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
53 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
54 def dispatchEvent(self): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
55 """ |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
56 Dispatch the event |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
57 """ |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
58 # If last character is LF, strip it. |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
59 if self.data.endswith('\n'): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
60 self.data = self.data[:-1] |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
61 if self.event in self.callbacks: |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
62 self.callbacks[self.event](self.data) |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
63 self.data = '' |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
64 self.event = 'message' |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
66 def lstrip(value): |
14ac4a210dbc
Copy from from https://github.com/juggernaut/twisted-sse-demo
drewp@bigasterisk.com
parents:
diff
changeset
|
67 return value[1:] if value.startswith(' ') else value |