Skip to content

Commit

Permalink
Merge pull request #4 from justmobilize/files_arg_no_str_concat
Browse files Browse the repository at this point in the history
Remove str concatenation
  • Loading branch information
FoamyGuy authored May 4, 2024
2 parents 5a2934e + 0f9b71f commit 55f159f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
36 changes: 16 additions & 20 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,22 @@ def _build_boundary_data(self, files: dict): # pylint: disable=too-many-locals
file_name = field_values[0]
file_handle = field_values[1]

boundary_data = f"--{boundary_string}\r\n"
boundary_data += f'Content-Disposition: form-data; name="{field_name}"'
boundary_objects.append(
f'--{boundary_string}\r\nContent-Disposition: form-data; name="{field_name}"'
)
if file_name is not None:
boundary_data += f'; filename="{file_name}"'
boundary_data += "\r\n"
boundary_objects.append(f'; filename="{file_name}"')
boundary_objects.append("\r\n")
if len(field_values) >= 3:
file_content_type = field_values[2]
boundary_data += f"Content-Type: {file_content_type}\r\n"
boundary_objects.append(f"Content-Type: {file_content_type}\r\n")
if len(field_values) >= 4:
file_headers = field_values[3]
for file_header_key, file_header_value in file_headers.items():
boundary_data += f"{file_header_key}: {file_header_value}\r\n"
boundary_data += "\r\n"

content_length += len(boundary_data)
boundary_objects.append(boundary_data)
boundary_objects.append(
f"{file_header_key}: {file_header_value}\r\n"
)
boundary_objects.append("\r\n")

if hasattr(file_handle, "read"):
is_binary = False
Expand All @@ -400,19 +400,15 @@ def _build_boundary_data(self, files: dict): # pylint: disable=too-many-locals
file_handle.seek(0, SEEK_END)
content_length += file_handle.tell()
file_handle.seek(0)
boundary_objects.append(file_handle)
boundary_data = ""
else:
boundary_data = file_handle

boundary_data += "\r\n"
content_length += len(boundary_data)
boundary_objects.append(boundary_data)
boundary_objects.append(file_handle)
boundary_objects.append("\r\n")

boundary_data = f"--{boundary_string}--\r\n"
boundary_objects.append(f"--{boundary_string}--\r\n")

content_length += len(boundary_data)
boundary_objects.append(boundary_data)
for boundary_object in boundary_objects:
if isinstance(boundary_object, str):
content_length += len(boundary_object)

return boundary_string, content_length, boundary_objects

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ envlist = py311
description = run tests
deps =
pytest==7.4.3
requests
commands = pytest

[testenv:coverage]
description = run coverage
deps =
pytest==7.4.3
pytest-cov==4.1.0
requests
package = editable
commands =
coverage run --source=. --omit=tests/* --branch {posargs} -m pytest
Expand Down

0 comments on commit 55f159f

Please sign in to comment.