From c5c35c4dca99ded82d746141bc16e525e1a18148 Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 19:58:23 +0000 Subject: [PATCH 1/7] PPF-700: good start --- ruff.toml | 3 ++- tests/test_functional.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ruff.toml b/ruff.toml index 381084e1..d9ff9e43 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,3 +1,4 @@ [lint] -select = ["A", "E4", "E7", "E9", "F", "N802", "C405", "PLE0101", "PLR6301", "B006"] +select = ["A", "ASYNC", "B", "BLE", "E", "F", "N", "C405", "PLE0101", "PLR6301", "B006", "YTT", "W"] +ignore = ["E501", "N999"] preview = true diff --git a/tests/test_functional.py b/tests/test_functional.py index cb5123ed..24b99aff 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -12,7 +12,7 @@ def test_base_schema_definition(): try: assert Widget("foo").schema_definition - assert False + raise AssertionError() except NotImplementedError: pass @@ -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 @@ -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 @@ -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 From 1fcf8b103f23054321a3236e30faec19ba761de9 Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:03:12 +0000 Subject: [PATCH 2/7] PPF-700: renaming this file --- ruff.toml => .ruff.toml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ruff.toml => .ruff.toml (100%) diff --git a/ruff.toml b/.ruff.toml similarity index 100% rename from ruff.toml rename to .ruff.toml From 4c09a54be2a1896a5e39f9f24d635ae195f2521b Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:06:51 +0000 Subject: [PATCH 3/7] PPF-700: fix bump version script --- scripts/bump_version.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/scripts/bump_version.py b/scripts/bump_version.py index ee47d2d5..5dd29b89 100644 --- a/scripts/bump_version.py +++ b/scripts/bump_version.py @@ -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) From 01002a90325d6ef560730fb417334dbf97d28152 Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:17:38 +0000 Subject: [PATCH 4/7] PPF-700: update list --- .ruff.toml | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/.ruff.toml b/.ruff.toml index d9ff9e43..7c821139 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -1,4 +1,25 @@ [lint] -select = ["A", "ASYNC", "B", "BLE", "E", "F", "N", "C405", "PLE0101", "PLR6301", "B006", "YTT", "W"] +select = [ + "A", + "ASYNC", + "B", + "BLE", + "C4", + "DTZ", + "E", + "EM", + "F", + "G", + "ISC", + "ICN", + "LOG", + "N", + "PIE", + "PLE0101", + "PLR6301", + "T10", + "YTT", + "W" +] ignore = ["E501", "N999"] preview = true From a557e7ed04eb16ef060d84f60f1f5eef47bbd26a Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:22:29 +0000 Subject: [PATCH 5/7] PPF-700: stopped at (SIM) --- .ruff.toml | 6 ++++++ tests/conftest.py | 9 ++++----- tests/test_functional.py | 10 +++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.ruff.toml b/.ruff.toml index 7c821139..8c1e9501 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -15,8 +15,14 @@ select = [ "LOG", "N", "PIE", + "PT", "PLE0101", "PLR6301", + "Q", + "RSE", + "SLF", + "SLOT", + "SIM", "T10", "YTT", "W" diff --git a/tests/conftest.py b/tests/conftest.py index 5822be74..b4a2cbdd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_functional.py b/tests/test_functional.py index 24b99aff..36ebf603 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -12,7 +12,7 @@ def test_base_schema_definition(): try: assert Widget("foo").schema_definition - raise AssertionError() + raise AssertionError except NotImplementedError: pass @@ -375,7 +375,7 @@ def test_schema(sample_template_with_comb_text_field): data["LastName"] = "XXXXXXXX" try: validate(instance=data, schema=schema) - raise AssertionError() + raise AssertionError except ValidationError: pass @@ -386,7 +386,7 @@ def test_schema(sample_template_with_comb_text_field): data["Gender"] = 2 try: validate(instance=data, schema=schema) - raise AssertionError() + raise AssertionError except ValidationError: pass @@ -396,12 +396,12 @@ def test_sample_data(sejda_template_complex): try: validate(instance=obj.sample_data, schema=obj.schema) except ValidationError: - raise AssertionError() from ValidationError + raise AssertionError from ValidationError widget = Widget("foo") try: widget.sample_value() - raise AssertionError() + raise AssertionError except NotImplementedError: pass From 8a80ffe4db53439c560ff42b0cc4095acfb959c1 Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:33:05 +0000 Subject: [PATCH 6/7] PPF-700: stop at (PERF) --- .ruff.toml | 6 ++++++ PyPDFForm/template.py | 2 +- PyPDFForm/watermark.py | 7 +------ PyPDFForm/widgets/base.py | 6 +----- PyPDFForm/wrapper.py | 4 +--- tests/scenario/test_issues_simple.py | 4 ++-- tests/test_functional.py | 4 ++-- 7 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.ruff.toml b/.ruff.toml index 8c1e9501..83dff2de 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -9,21 +9,27 @@ select = [ "E", "EM", "F", + "FLY", "G", "ISC", "ICN", + "INT", "LOG", "N", "PIE", "PT", + "PERF", "PLE0101", "PLR6301", "Q", + "R", "RSE", "SLF", "SLOT", "SIM", "T10", + "TRY", + "TCH", "YTT", "W" ] diff --git a/PyPDFForm/template.py b/PyPDFForm/template.py index 0a4b839b..bd02d933 100644 --- a/PyPDFForm/template.py +++ b/PyPDFForm/template.py @@ -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) diff --git a/PyPDFForm/watermark.py b/PyPDFForm/watermark.py index 8fb58bf4..17c7327d 100644 --- a/PyPDFForm/watermark.py +++ b/PyPDFForm/watermark.py @@ -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( diff --git a/PyPDFForm/widgets/base.py b/PyPDFForm/widgets/base.py index 8e0e4c71..29d9e938 100644 --- a/PyPDFForm/widgets/base.py +++ b/PyPDFForm/widgets/base.py @@ -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)] diff --git a/PyPDFForm/wrapper.py b/PyPDFForm/wrapper.py index a5f1ccd5..6d962364 100644 --- a/PyPDFForm/wrapper.py +++ b/PyPDFForm/wrapper.py @@ -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] diff --git a/tests/scenario/test_issues_simple.py b/tests/scenario/test_issues_simple.py index 5fa726b5..18725cc5 100644 --- a/tests/scenario/test_issues_simple.py +++ b/tests/scenario/test_issues_simple.py @@ -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 ( @@ -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" diff --git a/tests/test_functional.py b/tests/test_functional.py index 36ebf603..ef1a611a 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -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 @@ -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 From b3bca25658aa10cd390e3d1bc93b71726a154c96 Mon Sep 17 00:00:00 2001 From: Jinge Li <9894243+chinapandaman@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:36:03 +0000 Subject: [PATCH 7/7] PPF-700: finish updating rules --- .ruff.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.ruff.toml b/.ruff.toml index 83dff2de..d0b8f385 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -19,8 +19,6 @@ select = [ "PIE", "PT", "PERF", - "PLE0101", - "PLR6301", "Q", "R", "RSE",