Skip to content

Commit 8dafbc6

Browse files
authored
V4 remove chunked_requests submodule (plotly#1627)
* Remove chunked_request submodule. This functionality will be maintained in the plotly.py repo from now on * Remove chunked request from setup_submodules * Blacken chunked_request module
1 parent 874bbae commit 8dafbc6

File tree

5 files changed

+61
-76
lines changed

5 files changed

+61
-76
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from . chunked_request import Stream
1+
from .chunked_request import Stream

packages/python/chart-studio/chart_studio/plotly/chunked_requests/chunked_request.py

+60-55
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010

1111

1212
class Stream:
13-
def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False,
14-
ssl_verification_enabled=True):
15-
''' Initialize a stream object and an HTTP or HTTPS connection
13+
def __init__(
14+
self,
15+
server,
16+
port=80,
17+
headers={},
18+
url="/",
19+
ssl_enabled=False,
20+
ssl_verification_enabled=True,
21+
):
22+
""" Initialize a stream object and an HTTP or HTTPS connection
1623
with chunked Transfer-Encoding to server:port with optional headers.
17-
'''
24+
"""
1825
self.maxtries = 5
1926
self._tries = 0
2027
self._delay = 1
@@ -27,23 +34,25 @@ def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False,
2734
self._ssl_verification_enabled = ssl_verification_enabled
2835
self._connect()
2936

30-
def write(self, data, reconnect_on=('', 200, 502)):
31-
''' Send `data` to the server in chunk-encoded form.
37+
def write(self, data, reconnect_on=("", 200, 502)):
38+
""" Send `data` to the server in chunk-encoded form.
3239
Check the connection before writing and reconnect
3340
if disconnected and if the response status code is in `reconnect_on`.
3441
3542
The response may either be an HTTPResponse object or an empty string.
36-
'''
43+
"""
3744

3845
if not self._isconnected():
3946

4047
# Attempt to get the response.
4148
response = self._getresponse()
4249

4350
# Reconnect depending on the status code.
44-
if ((response == '' and '' in reconnect_on) or
45-
(response and isinstance(response, http_client.HTTPResponse) and
46-
response.status in reconnect_on)):
51+
if (response == "" and "" in reconnect_on) or (
52+
response
53+
and isinstance(response, http_client.HTTPResponse)
54+
and response.status in reconnect_on
55+
):
4756
self._reconnect()
4857

4958
elif response and isinstance(response, http_client.HTTPResponse):
@@ -56,23 +65,25 @@ def write(self, data, reconnect_on=('', 200, 502)):
5665
# like Invalid Credentials.
5766
# This allows the user to determine when
5867
# to reconnect.
59-
raise Exception("Server responded with "
60-
"status code: {status_code}\n"
61-
"and message: {msg}."
62-
.format(status_code=response.status,
63-
msg=response.read()))
68+
raise Exception(
69+
"Server responded with "
70+
"status code: {status_code}\n"
71+
"and message: {msg}.".format(
72+
status_code=response.status, msg=response.read()
73+
)
74+
)
6475

65-
elif response == '':
66-
raise Exception("Attempted to write but socket "
67-
"was not connected.")
76+
elif response == "":
77+
raise Exception("Attempted to write but socket " "was not connected.")
6878

6979
try:
7080
msg = data
71-
msglen = format(len(msg), 'x') # msg length in hex
81+
msglen = format(len(msg), "x") # msg length in hex
7282
# Send the message in chunk-encoded form
7383
self._conn.sock.setblocking(1)
74-
self._conn.send('{msglen}\r\n{msg}\r\n'
75-
.format(msglen=msglen, msg=msg).encode('utf-8'))
84+
self._conn.send(
85+
"{msglen}\r\n{msg}\r\n".format(msglen=msglen, msg=msg).encode("utf-8")
86+
)
7687
self._conn.sock.setblocking(0)
7788
except http_client.socket.error:
7889
self._reconnect()
@@ -94,11 +105,9 @@ def _get_proxy_config(self):
94105
ssl_enabled = self._ssl_enabled
95106

96107
if ssl_enabled:
97-
proxy = (os.environ.get("https_proxy") or
98-
os.environ.get("HTTPS_PROXY"))
108+
proxy = os.environ.get("https_proxy") or os.environ.get("HTTPS_PROXY")
99109
else:
100-
proxy = (os.environ.get("http_proxy") or
101-
os.environ.get("HTTP_PROXY"))
110+
proxy = os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY")
102111

103112
no_proxy = os.environ.get("no_proxy") or os.environ.get("NO_PROXY")
104113
no_proxy_url = no_proxy and self._server in no_proxy
@@ -131,82 +140,78 @@ def _get_ssl_context(self):
131140
return context
132141

133142
def _connect(self):
134-
''' Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding
143+
""" Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding
135144
to server:port with optional headers.
136-
'''
145+
"""
137146
server = self._server
138147
port = self._port
139148
headers = self._headers
140149
ssl_enabled = self._ssl_enabled
141150
proxy_server, proxy_port, proxy_auth = self._get_proxy_config()
142151

143-
if (proxy_server and proxy_port):
152+
if proxy_server and proxy_port:
144153
if ssl_enabled:
145154
context = self._get_ssl_context()
146155
self._conn = http_client.HTTPSConnection(
147156
proxy_server, proxy_port, context=context
148157
)
149158
else:
150-
self._conn = http_client.HTTPConnection(
151-
proxy_server, proxy_port
152-
)
159+
self._conn = http_client.HTTPConnection(proxy_server, proxy_port)
153160

154161
tunnel_headers = None
155162
if proxy_auth:
156-
tunnel_headers = {'Proxy-Authorization': proxy_auth}
163+
tunnel_headers = {"Proxy-Authorization": proxy_auth}
157164

158165
self._conn.set_tunnel(server, port, headers=tunnel_headers)
159166
else:
160167
if ssl_enabled:
161168
context = self._get_ssl_context()
162-
self._conn = http_client.HTTPSConnection(
163-
server, port, context=context
164-
)
169+
self._conn = http_client.HTTPSConnection(server, port, context=context)
165170
else:
166171
self._conn = http_client.HTTPConnection(server, port)
167172

168-
self._conn.putrequest('POST', self._url)
169-
self._conn.putheader('Transfer-Encoding', 'chunked')
173+
self._conn.putrequest("POST", self._url)
174+
self._conn.putheader("Transfer-Encoding", "chunked")
170175
for header in headers:
171176
self._conn.putheader(header, headers[header])
172177
self._conn.endheaders()
173178

174179
# Set blocking to False prevents recv
175180
# from blocking while waiting for a response.
176181
self._conn.sock.setblocking(False)
177-
self._bytes = six.b('')
182+
self._bytes = six.b("")
178183
self._reset_retries()
179184
time.sleep(0.5)
180185

181186
def close(self):
182-
''' Close the connection to server.
187+
""" Close the connection to server.
183188
184189
If available, return a http_client.HTTPResponse object.
185190
186191
Closing the connection involves sending the
187192
Transfer-Encoding terminating bytes.
188-
'''
193+
"""
189194
self._reset_retries()
190195
self._closed = True
191196

192197
# Chunked-encoded posts are terminated with '0\r\n\r\n'
193198
# For some reason, either Python or node.js seems to
194199
# require an extra \r\n.
195200
try:
196-
self._conn.send('\r\n0\r\n\r\n'.encode('utf-8'))
201+
self._conn.send("\r\n0\r\n\r\n".encode("utf-8"))
197202
except http_client.socket.error:
198203
# In case the socket has already been closed
199-
return ''
204+
return ""
200205

201206
return self._getresponse()
202207

203208
def _getresponse(self):
204-
''' Read from recv and return a HTTPResponse object if possible.
209+
""" Read from recv and return a HTTPResponse object if possible.
205210
Either
206211
1 - The client has succesfully closed the connection: Return ''
207212
2 - The server has already closed the connection: Return the response
208213
if possible.
209-
'''
214+
"""
210215
# Wait for a response
211216
self._conn.sock.setblocking(True)
212217
# Parse the response
@@ -217,8 +222,8 @@ def _getresponse(self):
217222
except http_client.socket.error:
218223
# For error 54: Connection reset by peer
219224
# (and perhaps others)
220-
return six.b('')
221-
if _bytes == six.b(''):
225+
return six.b("")
226+
if _bytes == six.b(""):
222227
break
223228
else:
224229
response += _bytes
@@ -227,19 +232,19 @@ def _getresponse(self):
227232

228233
# Convert the response string to a http_client.HTTPResponse
229234
# object with a bit of a hack
230-
if response != six.b(''):
235+
if response != six.b(""):
231236
# Taken from
232237
# http://pythonwise.blogspot.ca/2010/02/parse-http-response.html
233238
try:
234239
response = http_client.HTTPResponse(_FakeSocket(response))
235240
response.begin()
236241
except:
237242
# Bad headers ... etc.
238-
response = six.b('')
243+
response = six.b("")
239244
return response
240245

241246
def _isconnected(self):
242-
''' Return True if the socket is still connected
247+
""" Return True if the socket is still connected
243248
to the server, False otherwise.
244249
245250
This check is done in 3 steps:
@@ -248,7 +253,7 @@ def _isconnected(self):
248253
3 - Check if the server has returned any data. If they have,
249254
assume that the server closed the response after they sent
250255
the data, i.e. that the data was the HTTP response.
251-
'''
256+
"""
252257

253258
# 1 - check if we've closed the connection.
254259
if self._closed:
@@ -263,7 +268,7 @@ def _isconnected(self):
263268
# 3 - Check if the server has returned any data.
264269
# If they have, then start to store the response
265270
# in _bytes.
266-
self._bytes = six.b('')
271+
self._bytes = six.b("")
267272
self._bytes = self._conn.sock.recv(1)
268273
return False
269274
except http_client.socket.error as e:
@@ -309,9 +314,9 @@ def _isconnected(self):
309314
raise e
310315

311316
def _reconnect(self):
312-
''' Connect if disconnected.
317+
""" Connect if disconnected.
313318
Retry self.maxtries times with delays
314-
'''
319+
"""
315320
if not self._isconnected():
316321
try:
317322
self._connect()
@@ -335,8 +340,8 @@ def _reconnect(self):
335340
self._closed = False
336341

337342
def _reset_retries(self):
338-
''' Reset the connect counters and delays
339-
'''
343+
""" Reset the connect counters and delays
344+
"""
340345
self._tries = 0
341346
self._delay = 1
342347

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ exclude = '''
1919
| submodules
2020
| packages/javascript
2121
| packages/python/plotly/plotly/matplotlylib/mplexporter
22-
| packages/python/chart-studio/chart_studio/plotly/chunked_requests
2322
)/
2423
| versioneer.py
2524
)

setup_submodules.py

-18
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,3 @@
2121
shutil.copytree(
2222
os.path.join(here, "submodules", "mplexporter", "mplexporter"), mpl_dst
2323
)
24-
25-
# Replace chunked_requests directory
26-
chunked_dst = os.path.join(
27-
here,
28-
"packages",
29-
"python",
30-
"chart-studio",
31-
"chart_studio",
32-
"plotly",
33-
"chunked_requests",
34-
)
35-
36-
shutil.rmtree(chunked_dst, ignore_errors=True)
37-
38-
shutil.copytree(
39-
os.path.join(here, "submodules", "chunked_requests", "chunked_requests"),
40-
os.path.join(chunked_dst),
41-
)

submodules/chunked_requests

Submodule chunked_requests deleted from 6d7c5dd

0 commit comments

Comments
 (0)