From 43375493f616ffdf01773b0abfebe30a13ce55ab Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 18 Jun 2019 17:09:37 -0400 Subject: [PATCH 1/3] Remove chunked_request submodule This functionality will be maintained in the plotly.py repo from now on --- submodules/chunked_requests | 1 - 1 file changed, 1 deletion(-) delete mode 160000 submodules/chunked_requests diff --git a/submodules/chunked_requests b/submodules/chunked_requests deleted file mode 160000 index 6d7c5ddc71c..00000000000 --- a/submodules/chunked_requests +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6d7c5ddc71cebc343c22a16f2151b11c68720a1d From d97b0ddb30853b46b7c53984cd2067d426fb5912 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 18 Jun 2019 17:10:24 -0400 Subject: [PATCH 2/3] Remove chunked request from setup_submodules --- setup_submodules.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/setup_submodules.py b/setup_submodules.py index 592e97d2709..23f1566ac5a 100644 --- a/setup_submodules.py +++ b/setup_submodules.py @@ -21,21 +21,3 @@ shutil.copytree( os.path.join(here, "submodules", "mplexporter", "mplexporter"), mpl_dst ) - - # Replace chunked_requests directory - chunked_dst = os.path.join( - here, - "packages", - "python", - "chart-studio", - "chart_studio", - "plotly", - "chunked_requests", - ) - - shutil.rmtree(chunked_dst, ignore_errors=True) - - shutil.copytree( - os.path.join(here, "submodules", "chunked_requests", "chunked_requests"), - os.path.join(chunked_dst), - ) From eb329a5768f4eb05c35279c92cbead8b4221a503 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Tue, 18 Jun 2019 17:11:56 -0400 Subject: [PATCH 3/3] Blacken chunked_request module --- .../plotly/chunked_requests/__init__.py | 2 +- .../chunked_requests/chunked_request.py | 115 +++++++++--------- pyproject.toml | 1 - 3 files changed, 61 insertions(+), 57 deletions(-) diff --git a/packages/python/chart-studio/chart_studio/plotly/chunked_requests/__init__.py b/packages/python/chart-studio/chart_studio/plotly/chunked_requests/__init__.py index 044709703b3..1433ad88e21 100644 --- a/packages/python/chart-studio/chart_studio/plotly/chunked_requests/__init__.py +++ b/packages/python/chart-studio/chart_studio/plotly/chunked_requests/__init__.py @@ -1 +1 @@ -from . chunked_request import Stream \ No newline at end of file +from .chunked_request import Stream diff --git a/packages/python/chart-studio/chart_studio/plotly/chunked_requests/chunked_request.py b/packages/python/chart-studio/chart_studio/plotly/chunked_requests/chunked_request.py index 5f39704c720..6e215a629d1 100644 --- a/packages/python/chart-studio/chart_studio/plotly/chunked_requests/chunked_request.py +++ b/packages/python/chart-studio/chart_studio/plotly/chunked_requests/chunked_request.py @@ -10,11 +10,18 @@ class Stream: - def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False, - ssl_verification_enabled=True): - ''' Initialize a stream object and an HTTP or HTTPS connection + def __init__( + self, + server, + port=80, + headers={}, + url="/", + ssl_enabled=False, + ssl_verification_enabled=True, + ): + """ Initialize a stream object and an HTTP or HTTPS connection with chunked Transfer-Encoding to server:port with optional headers. - ''' + """ self.maxtries = 5 self._tries = 0 self._delay = 1 @@ -27,13 +34,13 @@ def __init__(self, server, port=80, headers={}, url='/', ssl_enabled=False, self._ssl_verification_enabled = ssl_verification_enabled self._connect() - def write(self, data, reconnect_on=('', 200, 502)): - ''' Send `data` to the server in chunk-encoded form. + def write(self, data, reconnect_on=("", 200, 502)): + """ Send `data` to the server in chunk-encoded form. Check the connection before writing and reconnect if disconnected and if the response status code is in `reconnect_on`. The response may either be an HTTPResponse object or an empty string. - ''' + """ if not self._isconnected(): @@ -41,9 +48,11 @@ def write(self, data, reconnect_on=('', 200, 502)): response = self._getresponse() # Reconnect depending on the status code. - if ((response == '' and '' in reconnect_on) or - (response and isinstance(response, http_client.HTTPResponse) and - response.status in reconnect_on)): + if (response == "" and "" in reconnect_on) or ( + response + and isinstance(response, http_client.HTTPResponse) + and response.status in reconnect_on + ): self._reconnect() elif response and isinstance(response, http_client.HTTPResponse): @@ -56,23 +65,25 @@ def write(self, data, reconnect_on=('', 200, 502)): # like Invalid Credentials. # This allows the user to determine when # to reconnect. - raise Exception("Server responded with " - "status code: {status_code}\n" - "and message: {msg}." - .format(status_code=response.status, - msg=response.read())) + raise Exception( + "Server responded with " + "status code: {status_code}\n" + "and message: {msg}.".format( + status_code=response.status, msg=response.read() + ) + ) - elif response == '': - raise Exception("Attempted to write but socket " - "was not connected.") + elif response == "": + raise Exception("Attempted to write but socket " "was not connected.") try: msg = data - msglen = format(len(msg), 'x') # msg length in hex + msglen = format(len(msg), "x") # msg length in hex # Send the message in chunk-encoded form self._conn.sock.setblocking(1) - self._conn.send('{msglen}\r\n{msg}\r\n' - .format(msglen=msglen, msg=msg).encode('utf-8')) + self._conn.send( + "{msglen}\r\n{msg}\r\n".format(msglen=msglen, msg=msg).encode("utf-8") + ) self._conn.sock.setblocking(0) except http_client.socket.error: self._reconnect() @@ -94,11 +105,9 @@ def _get_proxy_config(self): ssl_enabled = self._ssl_enabled if ssl_enabled: - proxy = (os.environ.get("https_proxy") or - os.environ.get("HTTPS_PROXY")) + proxy = os.environ.get("https_proxy") or os.environ.get("HTTPS_PROXY") else: - proxy = (os.environ.get("http_proxy") or - os.environ.get("HTTP_PROXY")) + proxy = os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY") no_proxy = os.environ.get("no_proxy") or os.environ.get("NO_PROXY") no_proxy_url = no_proxy and self._server in no_proxy @@ -131,42 +140,38 @@ def _get_ssl_context(self): return context def _connect(self): - ''' Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding + """ Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding to server:port with optional headers. - ''' + """ server = self._server port = self._port headers = self._headers ssl_enabled = self._ssl_enabled proxy_server, proxy_port, proxy_auth = self._get_proxy_config() - if (proxy_server and proxy_port): + if proxy_server and proxy_port: if ssl_enabled: context = self._get_ssl_context() self._conn = http_client.HTTPSConnection( proxy_server, proxy_port, context=context ) else: - self._conn = http_client.HTTPConnection( - proxy_server, proxy_port - ) + self._conn = http_client.HTTPConnection(proxy_server, proxy_port) tunnel_headers = None if proxy_auth: - tunnel_headers = {'Proxy-Authorization': proxy_auth} + tunnel_headers = {"Proxy-Authorization": proxy_auth} self._conn.set_tunnel(server, port, headers=tunnel_headers) else: if ssl_enabled: context = self._get_ssl_context() - self._conn = http_client.HTTPSConnection( - server, port, context=context - ) + self._conn = http_client.HTTPSConnection(server, port, context=context) else: self._conn = http_client.HTTPConnection(server, port) - self._conn.putrequest('POST', self._url) - self._conn.putheader('Transfer-Encoding', 'chunked') + self._conn.putrequest("POST", self._url) + self._conn.putheader("Transfer-Encoding", "chunked") for header in headers: self._conn.putheader(header, headers[header]) self._conn.endheaders() @@ -174,18 +179,18 @@ def _connect(self): # Set blocking to False prevents recv # from blocking while waiting for a response. self._conn.sock.setblocking(False) - self._bytes = six.b('') + self._bytes = six.b("") self._reset_retries() time.sleep(0.5) def close(self): - ''' Close the connection to server. + """ Close the connection to server. If available, return a http_client.HTTPResponse object. Closing the connection involves sending the Transfer-Encoding terminating bytes. - ''' + """ self._reset_retries() self._closed = True @@ -193,20 +198,20 @@ def close(self): # For some reason, either Python or node.js seems to # require an extra \r\n. try: - self._conn.send('\r\n0\r\n\r\n'.encode('utf-8')) + self._conn.send("\r\n0\r\n\r\n".encode("utf-8")) except http_client.socket.error: # In case the socket has already been closed - return '' + return "" return self._getresponse() def _getresponse(self): - ''' Read from recv and return a HTTPResponse object if possible. + """ Read from recv and return a HTTPResponse object if possible. Either 1 - The client has succesfully closed the connection: Return '' 2 - The server has already closed the connection: Return the response if possible. - ''' + """ # Wait for a response self._conn.sock.setblocking(True) # Parse the response @@ -217,8 +222,8 @@ def _getresponse(self): except http_client.socket.error: # For error 54: Connection reset by peer # (and perhaps others) - return six.b('') - if _bytes == six.b(''): + return six.b("") + if _bytes == six.b(""): break else: response += _bytes @@ -227,7 +232,7 @@ def _getresponse(self): # Convert the response string to a http_client.HTTPResponse # object with a bit of a hack - if response != six.b(''): + if response != six.b(""): # Taken from # http://pythonwise.blogspot.ca/2010/02/parse-http-response.html try: @@ -235,11 +240,11 @@ def _getresponse(self): response.begin() except: # Bad headers ... etc. - response = six.b('') + response = six.b("") return response def _isconnected(self): - ''' Return True if the socket is still connected + """ Return True if the socket is still connected to the server, False otherwise. This check is done in 3 steps: @@ -248,7 +253,7 @@ def _isconnected(self): 3 - Check if the server has returned any data. If they have, assume that the server closed the response after they sent the data, i.e. that the data was the HTTP response. - ''' + """ # 1 - check if we've closed the connection. if self._closed: @@ -263,7 +268,7 @@ def _isconnected(self): # 3 - Check if the server has returned any data. # If they have, then start to store the response # in _bytes. - self._bytes = six.b('') + self._bytes = six.b("") self._bytes = self._conn.sock.recv(1) return False except http_client.socket.error as e: @@ -309,9 +314,9 @@ def _isconnected(self): raise e def _reconnect(self): - ''' Connect if disconnected. + """ Connect if disconnected. Retry self.maxtries times with delays - ''' + """ if not self._isconnected(): try: self._connect() @@ -335,8 +340,8 @@ def _reconnect(self): self._closed = False def _reset_retries(self): - ''' Reset the connect counters and delays - ''' + """ Reset the connect counters and delays + """ self._tries = 0 self._delay = 1 diff --git a/pyproject.toml b/pyproject.toml index cb82ab5fe07..d46a0c5da08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,6 @@ exclude = ''' | submodules | packages/javascript | packages/python/plotly/plotly/matplotlylib/mplexporter - | packages/python/chart-studio/chart_studio/plotly/chunked_requests )/ | versioneer.py )