Skip to content

Commit

Permalink
Merge pull request #701 from chinapandaman/PPF-700
Browse files Browse the repository at this point in the history
PPF-700: audit ruff rules
  • Loading branch information
chinapandaman authored Jul 13, 2024
2 parents bf56759 + b3bca25 commit 9d3d7cf
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 51 deletions.
35 changes: 35 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[lint]
select = [
"A",
"ASYNC",
"B",
"BLE",
"C4",
"DTZ",
"E",
"EM",
"F",
"FLY",
"G",
"ISC",
"ICN",
"INT",
"LOG",
"N",
"PIE",
"PT",
"PERF",
"Q",
"R",
"RSE",
"SLF",
"SLOT",
"SIM",
"T10",
"TRY",
"TCH",
"YTT",
"W"
]
ignore = ["E501", "N999"]
preview = true
2 changes: 1 addition & 1 deletion PyPDFForm/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def update_text_field_attributes(
) -> None:
"""Auto updates text fields' attributes."""

for _, _widgets in get_widgets_by_page(template_stream).items():
for _widgets in get_widgets_by_page(template_stream).values():
for _widget in _widgets:
key = get_widget_key(_widget)

Expand Down
7 changes: 1 addition & 6 deletions PyPDFForm/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ def create_watermarks_and_draw(
watermark = buff.read()
buff.close()

results = []

for i in range(len(pdf_file.pages)):
results.append(watermark if i == page_number - 1 else b"")

return results
return [watermark if i == page_number - 1 else b"" for i in range(len(pdf_file.pages))]


def merge_watermarks_with_pdf(
Expand Down
6 changes: 1 addition & 5 deletions PyPDFForm/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,4 @@ def watermarks(self, stream: bytes) -> List[bytes]:
canvas.save()
watermark.seek(0)

result = []
for i in range(page_count):
result.append(watermark.read() if i == self.page_number - 1 else b"")

return result
return [watermark.read() if i == self.page_number - 1 else b"" for i in range(page_count)]
4 changes: 1 addition & 3 deletions PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,13 @@ def draw_image(
def schema(self) -> dict:
"""Generates a json schema for the PDF form template."""

result = {
return {
"type": "object",
"properties": {
key: value.schema_definition for key, value in self.widgets.items()
},
}

return result

@classmethod
def register_font(
cls, font_name: str, ttf_file: Union[bytes, str, BinaryIO]
Expand Down
3 changes: 0 additions & 3 deletions ruff.toml

This file was deleted.

27 changes: 8 additions & 19 deletions scripts/bump_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,16 @@
with open("PyPDFForm/__init__.py", encoding="utf8") as f:
version = re.search(r'__version__ = "(.*?)"', f.read())
if version:
v = v.group(1)
v = version.group(1)

new_version = ".".join(v.split(".")[:-1] + [str(int(v.split(".")[-1]) + 1)])

with open("PyPDFForm/__init__.py", encoding="utf8") as f:
content = f.read().replace(version, new_version)

os.remove("PyPDFForm/__init__.py")
with open("PyPDFForm/__init__.py", mode="w", encoding="utf8") as f:
f.write(content)

with open("mkdocs.yml", encoding="utf8") as f:
content = f.read().replace(version, new_version)

os.remove("mkdocs.yml")
with open("mkdocs.yml", mode="w", encoding="utf8") as f:
f.write(content)
files_to_update = ["PyPDFForm/__init__.py", "mkdocs.yml", "SECURITY.md"]

with open("SECURITY.md", encoding="utf8") as f:
content = f.read().replace(version, new_version)
for each in files_to_update:
with open(each, encoding="utf8") as f:
content = f.read().replace(v, new_version)

os.remove("SECURITY.md")
with open("SECURITY.md", mode="w", encoding="utf8") as f:
f.write(content)
os.remove(each)
with open(each, mode="w", encoding="utf8") as f:
f.write(content)
9 changes: 4 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ def pytest_addoption(parser):


@pytest.fixture(autouse=True)
def generate_new_pdf_samples(request):
def _generate_new_pdf_samples(request):
request.config.results = {}
yield
if request.config.getoption("--regenerate") == "1":
if request.config.results:
with open(request.config.results["expected_path"], "wb+") as f:
f.write(request.config.results["stream"])
if request.config.getoption("--regenerate") == "1" and request.config.results:
with open(request.config.results["expected_path"], "wb+") as f:
f.write(request.config.results["stream"])


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/scenario/test_issues_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_pdf_form_with_paragraph_fields_new_line_symbol_text(issue_pdf_directory
{"Address": "Mr John Smith\n132, My Street\nKingston, New York 12401"}
)

for _, widgets in get_widgets_by_page(obj.read()).items():
for widgets in get_widgets_by_page(obj.read()).values():
for widget in widgets:
if get_widget_key(widget) == "Address":
assert (
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_pdf_form_with_paragraph_fields_new_line_symbol_short_text(issue_pdf_dir
{"Address": "J Smith\n132 A St\nNYC, NY 12401"}
)

for _, widgets in get_widgets_by_page(obj.read()).items():
for widgets in get_widgets_by_page(obj.read()).values():
for widget in widgets:
if get_widget_key(widget) == "Address":
assert widget[V] == "J Smith\n132 A St\nNYC, NY 12401"
Expand Down
14 changes: 7 additions & 7 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def test_base_schema_definition():
try:
assert Widget("foo").schema_definition
assert False
raise AssertionError
except NotImplementedError:
pass

Expand All @@ -34,7 +34,7 @@ def test_fill(template_stream, pdf_samples, data_dict, request):
assert len(obj.stream) == len(expected)
assert obj.stream == expected

for _, widgets in template.get_widgets_by_page(obj.read()).items():
for widgets in template.get_widgets_by_page(obj.read()).values():
assert not widgets


Expand Down Expand Up @@ -375,7 +375,7 @@ def test_schema(sample_template_with_comb_text_field):
data["LastName"] = "XXXXXXXX"
try:
validate(instance=data, schema=schema)
assert False
raise AssertionError
except ValidationError:
pass

Expand All @@ -386,7 +386,7 @@ def test_schema(sample_template_with_comb_text_field):
data["Gender"] = 2
try:
validate(instance=data, schema=schema)
assert False
raise AssertionError
except ValidationError:
pass

Expand All @@ -396,12 +396,12 @@ def test_sample_data(sejda_template_complex):
try:
validate(instance=obj.sample_data, schema=obj.schema)
except ValidationError:
assert False
raise AssertionError from ValidationError

widget = Widget("foo")
try:
widget.sample_value()
assert False
raise AssertionError
except NotImplementedError:
pass

Expand Down Expand Up @@ -429,7 +429,7 @@ def test_fill_right_aligned(
assert len(obj.stream) == len(expected)
assert obj.stream == expected

for _, widgets in template.get_widgets_by_page(obj.read()).items():
for widgets in template.get_widgets_by_page(obj.read()).values():
assert not widgets


Expand Down

0 comments on commit 9d3d7cf

Please sign in to comment.