Skip to content

Commit f272707

Browse files
authored
Merge pull request #3 from scrapy-plugins/feature/pre-commit
Feature/pre commit
2 parents 2afc98b + 70f8d0c commit f272707

File tree

7 files changed

+94
-13
lines changed

7 files changed

+94
-13
lines changed

.flake8

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[flake8]
2+
ignore =
3+
E501,
4+
# "max-line length" - Handled by black.
5+
W503,
6+
# "line break before binary operator" - Handled by black.
7+
8+
D100,
9+
# Missing docstring in public module
10+
D101,
11+
# Missing docstring in public class
12+
D103,
13+
# Missing docstring in public function
14+
D102,
15+
# Missing docstring in public method
16+
D104,
17+
# Missing docstring in public package
18+
D105,
19+
# Missing docstring in magic method
20+
D107,
21+
# Missing docstring in __init__
22+
D200,
23+
# One-line docstring should fit on one line with quotes
24+
D202,
25+
# No blank lines allowed after function docstring
26+
D205,
27+
# 1 blank line required between summary line and description
28+
D209,
29+
# Multi-line docstring closing quotes should be on a separate line
30+
D400,
31+
# First line should end with a period
32+
D401,
33+
# First line should be in imperative mood
34+
D402,
35+
# First line should not be the function's "signature"
36+
P103,
37+
# Other string does contain unindexed parameters

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d2d60666e425eeda24b7735702978b7dbef509f9

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
default_language_version:
2+
python: python3.10
3+
repos:
4+
- hooks:
5+
- id: black
6+
language_version: python3
7+
repo: https://github.com/ambv/black
8+
rev: 24.2.0
9+
- hooks:
10+
- id: isort
11+
language_version: python3
12+
repo: https://github.com/PyCQA/isort
13+
rev: 5.13.2
14+
- hooks:
15+
- id: flake8
16+
language_version: python3
17+
additional_dependencies:
18+
- flake8-bugbear
19+
- flake8-comprehensions
20+
- flake8-debugger
21+
- flake8-docstrings
22+
- flake8-string-format
23+
repo: https://github.com/pycqa/flake8
24+
rev: 7.0.0

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[build-system]
22
requires = ['setuptools>=42']
3-
build-backend = 'setuptools.build_meta'
3+
build-backend = 'setuptools.build_meta'
4+
5+
[tool.isort]
6+
profile = "black"

src/scrapy_settings_log/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import logging
21
import inspect
32
import json
3+
import logging
44
import re
5-
6-
from collections.abc import Mapping, Iterable
5+
from collections.abc import Iterable, Mapping
76

87
import scrapy
98
from scrapy import signals
10-
from scrapy.settings import BaseSettings
119

1210
logger = logging.getLogger(__name__)
1311

@@ -25,7 +23,10 @@ def prepare_for_json_serialization(obj):
2523
if isinstance(obj, (str, int, float, bool)) or obj is None:
2624
return obj
2725
elif isinstance(obj, Mapping):
28-
return {str(prepare_for_json_serialization(k)): prepare_for_json_serialization(v) for k, v in obj.items()}
26+
return {
27+
str(prepare_for_json_serialization(k)): prepare_for_json_serialization(v)
28+
for k, v in obj.items()
29+
}
2930
elif isinstance(obj, Iterable):
3031
return [prepare_for_json_serialization(v) for v in obj]
3132
elif inspect.isclass(obj):

tests/test_code.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import logging
2+
13
from scrapy import Spider
24
from scrapy.crawler import Crawler
5+
36
from src.scrapy_settings_log import SpiderSettingsLogging
4-
import logging
57

68
LOGGER = logging.getLogger(__name__)
79

@@ -93,11 +95,11 @@ class CustomClass:
9395
settings = {
9496
"SETTINGS_LOGGING_ENABLED": True,
9597
"SETTINGS_LOGGING_REGEX": "DUMMY",
96-
"DUMMY_CUSTOM_CLASS": {CustomClass: "/foo/bar"}
98+
"DUMMY_CUSTOM_CLASS": {CustomClass: "/foo/bar"},
9799
}
98100
spider = MockSpider(settings)
99101
logger = SpiderSettingsLogging()
100102
with caplog.at_level(logging.INFO):
101103
logger.spider_closed(spider)
102104

103-
assert '{"DUMMY_CUSTOM_CLASS": {"CustomClass": "/foo/bar"}}' in caplog.text
105+
assert '{"DUMMY_CUSTOM_CLASS": {"CustomClass": "/foo/bar"}}' in caplog.text

tests/test_process_values.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
from src.scrapy_settings_log import prepare_for_json_serialization
22

3+
34
def test_prepare_for_json_serialization_with_set():
45
"""Test processing a set value."""
5-
obj = {1,2,3}
6+
obj = {1, 2, 3}
67
result = prepare_for_json_serialization(obj)
7-
assert result == [1,2,3]
8+
assert result == [1, 2, 3]
89

910

1011
def test_prepare_for_json_serialization_with_class_object():
1112
"""Test processing a class object."""
13+
1214
class DummyClass:
1315
pass
16+
1417
result = prepare_for_json_serialization(DummyClass)
1518
assert result == "DummyClass"
1619

1720

1821
def test_prepare_for_json_serialization_dict_with_class_as_key():
1922
"""Test processing a dictionary with a class object as a key."""
23+
2024
class DummyClass:
2125
pass
26+
2227
result = prepare_for_json_serialization({DummyClass: "foo"})
2328
assert result == {"DummyClass": "foo"}
2429

2530

2631
def test_prepare_for_json_serialization_with_nested_objects():
2732
"""Test processing nested objects."""
28-
obj = {"foo": {"bar": {1,2,3,}}}
33+
obj = {
34+
"foo": {
35+
"bar": {
36+
1,
37+
2,
38+
3,
39+
}
40+
}
41+
}
2942
result = prepare_for_json_serialization(obj)
30-
assert result == {"foo": {"bar": [1,2,3]}}
43+
assert result == {"foo": {"bar": [1, 2, 3]}}

0 commit comments

Comments
 (0)