Skip to content

Commit fdff0c2

Browse files
author
Emmanuel Rondan
committed
Merge branch 'adding-pre-commit' of github.com:emarondan/scrapy-zyte-smartproxy into adding-pre-commit
2 parents 2935dea + 47ed9a4 commit fdff0c2

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

.bandit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ skips:
33
- B311
44
- B320
55
- B410
6-
exclude_dirs: ['tests']
6+
exclude_dirs: ['tests']

.git-blame-ignore-revs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# applying pre-commit hooks to the project
2-
05665a6fb1717ef513d7a8ac87b8eb499a64cdc9
2+
05665a6fb1717ef513d7a8ac87b8eb499a64cdc9

scrapy_zyte_smartproxy/middleware.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def _translate_headers(self, request, targets_zyte_api):
239239
continue
240240
request.headers[translation] = value = request.headers.pop(header)
241241
logger.warning(
242-
"Translating (and dropping) header %r (%r) as %r on request %r",
242+
"Translating header %r (%r) to %r on request %r",
243243
header,
244244
value,
245245
translation,
@@ -371,8 +371,8 @@ def process_response(self, request, response, spider):
371371
"retries/auth/max_reached", targets_zyte_api=targets_zyte_api
372372
)
373373
logger.warning(
374-
"Max retries for authentication issues reached, please check auth"
375-
" information settings",
374+
"Max retries for authentication issues reached,"
375+
"please check auth information settings",
376376
extra={"spider": self.spider},
377377
)
378378

@@ -392,7 +392,8 @@ def process_response(self, request, response, spider):
392392
self._inc_stat("response/banned", targets_zyte_api=targets_zyte_api)
393393
else:
394394
self._bans[key] = 0
395-
# If placed behind `RedirectMiddleware`, it would not count 3xx responses
395+
# If placed behind `RedirectMiddleware`,
396+
# it would not count 3xx responses
396397
self._inc_stat("response", targets_zyte_api=targets_zyte_api)
397398
self._inc_stat(
398399
"response/status/{}".format(response.status),
@@ -402,7 +403,8 @@ def process_response(self, request, response, spider):
402403
self._inc_stat("response/error", targets_zyte_api=targets_zyte_api)
403404
error_msg = zyte_smartproxy_error.decode("utf8")
404405
self._inc_stat(
405-
"response/error/{}".format(error_msg), targets_zyte_api=targets_zyte_api
406+
"response/error/{}".format(error_msg),
407+
targets_zyte_api=targets_zyte_api,
406408
)
407409
return response
408410

@@ -428,7 +430,8 @@ def _handle_not_enabled_response(self, request, response, targets_zyte_api):
428430
retryreq = request.copy()
429431
retryreq.dont_filter = True
430432
self._inc_stat(
431-
"retries/should_have_been_enabled", targets_zyte_api=targets_zyte_api
433+
"retries/should_have_been_enabled",
434+
targets_zyte_api=targets_zyte_api,
432435
)
433436
return retryreq
434437
return response
@@ -531,7 +534,8 @@ def _clean_zyte_smartproxy_headers(self, request, targets_zyte_api=None):
531534
"request is proxied with %s and not with %s, and "
532535
"automatic translation is not supported for this "
533536
"header. See "
534-
"https://docs.zyte.com/zyte-api/migration/zyte/smartproxy.html#parameter-mapping" # noqa
537+
"https://docs.zyte.com/zyte-api/migration/zyte/"
538+
"smartproxy.html#parameter-mapping"
535539
" to learn the right way to translate this header "
536540
"manually."
537541
),
@@ -557,11 +561,13 @@ def _set_zyte_smartproxy_default_headers(self, request):
557561
header.decode("utf-8").lower() for header in request.headers
558562
]
559563
if all(h.lower() in lower_case_headers for h in self.conflicting_headers):
560-
# Send a general warning once, and specific urls if LOG_LEVEL = DEBUG
564+
# Send a general warning once,
565+
# and specific urls if LOG_LEVEL = DEBUG
561566
warnings.warn(
562567
"The headers %s are conflicting on some of your requests. "
563568
"Please check "
564-
"https://docs.zyte.com/smart-proxy-manager.html#request-headers "
569+
"https://docs.zyte.com/smart-proxy-manager.html"
570+
"#request-headers "
565571
"for more information. You can set LOG_LEVEL=DEBUG to see the "
566572
"urls with problems." % str(self.conflicting_headers)
567573
)

tests/test_all.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,9 @@ def test_is_banned(self):
740740
res = mw.process_response(req, res, self.spider)
741741
self.assertFalse(mw._is_banned(res))
742742
res = Response(
743-
req.url, status=503, headers={"Zyte-Error": "/limits/over-global-limit"}
743+
req.url,
744+
status=503,
745+
headers={"Zyte-Error": "/limits/over-global-limit"},
744746
)
745747
res = mw.process_response(req, res, self.spider)
746748
self.assertFalse(mw._is_banned(res))
@@ -754,7 +756,9 @@ def test_is_banned(self):
754756
res = mw.process_response(req, res, self.spider)
755757
self.assertTrue(mw._is_banned(res))
756758
res = Response(
757-
req.url, status=521, headers={"Zyte-Error": "/download/internal-error"}
759+
req.url,
760+
status=521,
761+
headers={"Zyte-Error": "/download/internal-error"},
758762
)
759763
res = mw.process_response(req, res, self.spider)
760764
self.assertTrue(mw._is_banned(res))
@@ -1311,7 +1315,8 @@ def test_header_drop_warnings(self, mock_logger):
13111315
"request is proxied with %s and not with %s, and "
13121316
"automatic translation is not supported for this "
13131317
"header. See "
1314-
"https://docs.zyte.com/zyte-api/migration/zyte/smartproxy.html#parameter-mapping" # noqa
1318+
"https://docs.zyte.com/zyte-api/migration/zyte/smartproxy.html"
1319+
"#parameter-mapping"
13151320
" to learn the right way to translate this header "
13161321
"manually."
13171322
),
@@ -1335,7 +1340,8 @@ def test_header_drop_warnings(self, mock_logger):
13351340
"request is proxied with %s and not with %s, and "
13361341
"automatic translation is not supported for this "
13371342
"header. See "
1338-
"https://docs.zyte.com/zyte-api/migration/zyte/smartproxy.html#parameter-mapping" # noqa
1343+
"https://docs.zyte.com/zyte-api/migration/zyte/smartproxy.html"
1344+
"#parameter-mapping"
13391345
" to learn the right way to translate this header "
13401346
"manually."
13411347
),
@@ -1355,7 +1361,8 @@ def test_header_drop_warnings(self, mock_logger):
13551361
headers={"Zyte-Foo": "bar", "X-Crawlera-Foo": "bar"},
13561362
)
13571363
self.assertEqual(mw.process_request(request, self.spider), None)
1358-
mock_logger.warning.assert_not_called() # No warnings for "drop all" scenarios
1364+
# No warnings for "drop all" scenarios
1365+
mock_logger.warning.assert_not_called()
13591366

13601367
def test_header_based_handling(self):
13611368
self.spider.zyte_smartproxy_enabled = True

0 commit comments

Comments
 (0)