-
Notifications
You must be signed in to change notification settings - Fork 10
/
sse.py
108 lines (81 loc) · 2.81 KB
/
sse.py
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import functools
import re
import sys
class Sse(object):
_retry = None
_buffer = None
_rx_event = re.compile(r'^add_event_([\w\d\_]+)$', flags=re.U)
def __init__(self, default_retry=2000):
self._buffer = []
self.set_retry(default_retry)
def set_retry(self, num):
"""
Set distinct retry timeout instead the default
value.
"""
self._retry = num
self._buffer.append("retry: {0}\n\n".format(self._retry))
def set_event_id(self, event_id):
if event_id:
self._buffer.append("id: {0}\n\n".format(event_id))
else:
# Reset event id
self._buffer.append("id\n\n")
def reset_event_id(self):
"""
Send a reset event id.
"""
self.set_event_id(None)
def _parse_text(self, text, encoding):
# parse text if is list, tuple or set instance
if isinstance(text, (list, tuple, set)):
for item in text:
if isinstance(item, bytes):
item = item.decode(encoding)
for subitem in item.splitlines():
yield subitem
else:
if isinstance(text, bytes):
text = text.decode(encoding)
for item in text.splitlines():
yield item
def add_message(self, event, text, encoding='utf-8'):
"""
Add messaget with eventname to the buffer.
:param str event: event name
:param str/list text: event content. Must be a str or list of str
:param bool split: splits str content by lines. default(true)
"""
self._buffer.append("event: {0}\n".format(event))
for text_item in self._parse_text(text, encoding):
self._buffer.append("data: {0}\n".format(text_item))
self._buffer.append("\n")
def __getattr__(self, attr):
"""
Make a dynamic method for add messages to specific events
like add_event_<eventname>(text="Hello")
Examples:
response.add_foo(text="bar")
This sets event to "foo" and put "bar" as content.
"""
res = self._rx_event.search(attr)
if not res:
return super(Sse, self).__getattr__(attr)
return functools.partial(self.add_message, event=res.group(1))
def __str__(self):
if sys.version_info[0] >= 3: # Python 3
return self.__unicode__()
return self.__unicode__().encode('utf8')
def __unicode__(self):
return "".join(self._buffer)
def flush(self):
"""
Reset the internal buffer to initial state.
"""
self._buffer = []
def __iter__(self):
for item in self._buffer:
yield item
self.flush()