From cea91bd6e04351ad6ec59e4f211f46350c2a28b0 Mon Sep 17 00:00:00 2001 From: Ayush Kaushik Date: Wed, 12 Jun 2024 23:34:08 -0400 Subject: [PATCH] Renamed classes and fixed tests - Fixed failing tests - Renamed classes to align with design strategy --- .github/workflows/main.yml | 2 +- .github/workflows/release.yml | 11 ++++++--- README.md | 3 +-- pdf_merger/src/app.py | 6 ++--- pdf_merger/src/services/__init__.py | 6 ++--- ...nterface.py => abstract_merger_service.py} | 3 +-- ...age_service.py => image_merger_service.py} | 4 ++-- .../{pdf_service.py => pdf_merger_service.py} | 4 ++-- pdf_merger/src/views/drag_drop_area.py | 6 ++--- .../src/views/file_collection_merge_view.py | 4 ++-- pdf_merger/src/views/view_aggregator.py | 8 +++---- tests/test_image_to_pdf_service.py | 24 ++++++++++++------- 12 files changed, 46 insertions(+), 35 deletions(-) rename pdf_merger/src/services/{merger_service_interface.py => abstract_merger_service.py} (93%) rename pdf_merger/src/services/{image_service.py => image_merger_service.py} (93%) rename pdf_merger/src/services/{pdf_service.py => pdf_merger_service.py} (86%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d6ba16e..6297c32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: PDF Merger +name: PDF Merger [main] on: push: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 06f1c13..2f83f1d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: PDF Merger +name: PDF Merger [release] on: pull_request: @@ -18,7 +18,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install pyinstaller PyPDF2 PyQt5 img2pdf + python -m pip install -r requirements.txt - name: Check contents of workspace run: | @@ -54,7 +54,12 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps + # Pulls from the CREATE RELEASE step above, + # referencing its ID to get its outputs object, + # which include a `upload_url`. + # See this blog post for more info: + # https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps + upload_url: ${{ steps.create_release.outputs.upload_url }} asset_path: ./release.zip asset_name: release.zip asset_content_type: application/zip diff --git a/README.md b/README.md index 5fcf3d1..bd683fa 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,7 @@ The tool currently supports following formats: - PyPDF2 - PyQt5 -## 👏 Acknowledgements -- The free landing page has been acquired from here https://cruip.com/demos/solid/. All credit goes to their respective owners. +## 👏 Acknowledgements - Free PDF's for testing found here: https://freetestdata.com/document-files/pdf/ - Sample image files found here: https://file-examples.com/ diff --git a/pdf_merger/src/app.py b/pdf_merger/src/app.py index b3bcaaa..0c81fb9 100644 --- a/pdf_merger/src/app.py +++ b/pdf_merger/src/app.py @@ -8,7 +8,7 @@ from PyPDF2 import PdfFileMerger from PyQt5.QtWidgets import QHBoxLayout, QWidget, QMainWindow, QTabWidget, QApplication -from pdf_merger.src.services import ImageService, PDFService +from pdf_merger.src.services import ImageMergerService, PdfMergerService from pdf_merger.src.views import AppLayoutConfig, ViewAggregator from pdf_merger.src.views.labels import Labels @@ -20,8 +20,8 @@ def configure(self, binder: Binder) -> None: class ViewAggregatorContainerModule(Module): def configure(self, binder: Binder) -> None: - binder.bind(PDFService, to=PDFService(PdfFileMerger(), [], ""), scope=injector.singleton) - binder.bind(ImageService, to=ImageService(img2pdf, [], ""), scope=injector.singleton) + binder.bind(PdfMergerService, to=PdfMergerService(PdfFileMerger(), [], ""), scope=injector.singleton) + binder.bind(ImageMergerService, to=ImageMergerService(img2pdf, [], ""), scope=injector.singleton) binder.bind(Labels, to=Labels()) binder.bind(ViewAggregator, to=ViewAggregator, scope=injector.singleton) diff --git a/pdf_merger/src/services/__init__.py b/pdf_merger/src/services/__init__.py index bd120ee..a52aaad 100644 --- a/pdf_merger/src/services/__init__.py +++ b/pdf_merger/src/services/__init__.py @@ -1,8 +1,8 @@ # __init__.py -from .image_service import ImageService -from .pdf_service import PDFService +from .image_merger_service import ImageMergerService +from .pdf_merger_service import PdfMergerService -__all__ = ['ImageService', 'PDFService'] +__all__ = ['ImageMergerService', 'PdfMergerService'] VERSION = '1.0.0' diff --git a/pdf_merger/src/services/merger_service_interface.py b/pdf_merger/src/services/abstract_merger_service.py similarity index 93% rename from pdf_merger/src/services/merger_service_interface.py rename to pdf_merger/src/services/abstract_merger_service.py index 11fbc31..b4ad4cb 100644 --- a/pdf_merger/src/services/merger_service_interface.py +++ b/pdf_merger/src/services/abstract_merger_service.py @@ -5,7 +5,7 @@ import pathlib -class FileMergerServiceInterface(ABC): +class AbstractFileMergerService(ABC): def __init__(self, allowed_extensions, target_file_path: pathlib.Path): self.allowed_file_extensions = allowed_extensions self.target_file_path = target_file_path @@ -29,4 +29,3 @@ def set_output_target(self, file_path): @abstractmethod def get_output_target(self): pass - diff --git a/pdf_merger/src/services/image_service.py b/pdf_merger/src/services/image_merger_service.py similarity index 93% rename from pdf_merger/src/services/image_service.py rename to pdf_merger/src/services/image_merger_service.py index 230d066..325232d 100644 --- a/pdf_merger/src/services/image_service.py +++ b/pdf_merger/src/services/image_merger_service.py @@ -3,14 +3,14 @@ from PyQt5.QtCore import QUrl import pathlib import img2pdf -from pdf_merger.src.services.merger_service_interface import FileMergerServiceInterface +from pdf_merger.src.services.abstract_merger_service import AbstractFileMergerService ''' Operations related to merging Images to PDF ''' -class ImageService(FileMergerServiceInterface): +class ImageMergerService(AbstractFileMergerService): def __init__(self, merger: img2pdf, file_list, target_path: pathlib.Path): super().__init__({ ".jpg": ".jpg", diff --git a/pdf_merger/src/services/pdf_service.py b/pdf_merger/src/services/pdf_merger_service.py similarity index 86% rename from pdf_merger/src/services/pdf_service.py rename to pdf_merger/src/services/pdf_merger_service.py index f9c55e0..2870769 100644 --- a/pdf_merger/src/services/pdf_service.py +++ b/pdf_merger/src/services/pdf_merger_service.py @@ -2,14 +2,14 @@ import pathlib from PyPDF2 import PdfFileMerger -from pdf_merger.src.services.merger_service_interface import FileMergerServiceInterface +from pdf_merger.src.services.abstract_merger_service import AbstractFileMergerService ''' Deals with operations related to PDF merging and creation ''' -class PDFService(FileMergerServiceInterface): +class PdfMergerService(AbstractFileMergerService): def __init__(self, merger: PdfFileMerger, path_list, target_path: pathlib.Path): super().__init__({".pdf": ".pdf"}, target_path) # passing valid extensions self.merger = merger diff --git a/pdf_merger/src/views/drag_drop_area.py b/pdf_merger/src/views/drag_drop_area.py index ff3d3ce..f78d5ae 100644 --- a/pdf_merger/src/views/drag_drop_area.py +++ b/pdf_merger/src/views/drag_drop_area.py @@ -5,10 +5,10 @@ from PyQt5.QtWidgets import QAbstractItemView, QListWidget from pdf_merger.src.exceptions import InvalidExtensionError from pdf_merger.src.views.popup_factory import PopupFactory -from pdf_merger.src.services.merger_service_interface import FileMergerServiceInterface +from pdf_merger.src.services.abstract_merger_service import AbstractFileMergerService ''' - List Widget Box which accepts multiple PDF files + List Widget Box which accepts multiple files Using polymorphism here to be able to pass any services PDFService, ImageService etc. @@ -16,7 +16,7 @@ class DragAndDropArea(QListWidget): - def __init__(self, service: FileMergerServiceInterface, parent=None): + def __init__(self, service: AbstractFileMergerService, parent=None): super().__init__(parent) self.setAcceptDrops(True) diff --git a/pdf_merger/src/views/file_collection_merge_view.py b/pdf_merger/src/views/file_collection_merge_view.py index 795ce3a..5fcc024 100644 --- a/pdf_merger/src/views/file_collection_merge_view.py +++ b/pdf_merger/src/views/file_collection_merge_view.py @@ -8,7 +8,7 @@ from pdf_merger.src.views.labels import Labels from pdf_merger.src.views.popup_factory import PopupFactory -from pdf_merger.src.services.merger_service_interface import FileMergerServiceInterface +from pdf_merger.src.services.abstract_merger_service import AbstractFileMergerService ''' Includes the complete view for Merging files into single PDF @@ -18,7 +18,7 @@ class FileCollectionMergeView(QWidget): def __init__( self, - service: FileMergerServiceInterface, + service: AbstractFileMergerService, config: AppLayoutConfig, labels: Labels ): diff --git a/pdf_merger/src/views/view_aggregator.py b/pdf_merger/src/views/view_aggregator.py index de176fb..066a298 100644 --- a/pdf_merger/src/views/view_aggregator.py +++ b/pdf_merger/src/views/view_aggregator.py @@ -2,8 +2,8 @@ from pdf_merger.src.views import AppLayoutConfig, Labels from pdf_merger.src.views.file_collection_merge_view import FileCollectionMergeView -from pdf_merger.src.services.image_service import ImageService -from pdf_merger.src.services.pdf_service import PDFService +from pdf_merger.src.services.image_merger_service import ImageMergerService +from pdf_merger.src.services.pdf_merger_service import PdfMergerService from injector import inject ''' @@ -15,8 +15,8 @@ class ViewAggregator: @inject def __init__( self, - pdf_service: PDFService, - image_service: ImageService, + pdf_service: PdfMergerService, + image_service: ImageMergerService, widget_layout_config: AppLayoutConfig, labels: Labels ): diff --git a/tests/test_image_to_pdf_service.py b/tests/test_image_to_pdf_service.py index 79b8743..b544065 100644 --- a/tests/test_image_to_pdf_service.py +++ b/tests/test_image_to_pdf_service.py @@ -2,24 +2,32 @@ import pathlib import img2pdf from PyQt5.QtCore import QUrl -from pdf_merger.src.services import ImageService +from pdf_merger.src.services import ImageMergerService class TestImagesToPdfService(unittest.TestCase): def setUp(self): - self.merger = img2pdf - self.target_file_path = pathlib.Path("./pdf_merger/tests/output/test_output.pdf") - self.path_list = [ - "./pdf_merger/tests/data/image/file_example_JPG_1MB.jpg", - "./pdf_merger/tests/data/image/file_example_JPG_1MB.jpg"] - self.service = ImageService(self.merger, self.path_list, self.target_file_path) + test_path = str(pathlib.Path(__file__).parent.resolve()) + self.target_file_path = pathlib.Path(test_path + "/output/test_output.pdf") + + self.service = ImageMergerService( + img2pdf, + [ + test_path + "/tests/data/image/file_example_JPG_1MB.jpg", + test_path + "/tests/data/image/file_example_JPG_1MB.jpg" + ], + self.target_file_path) def test_merge_files(self): + + print("Path of test file") + print() + self.service.merge_files() self.assertTrue(self.service.target_file_path.exists()) def test_set_target_path(self): - new_target_path = pathlib.Path("./pdf_merger/tests/output/image.jpg") + new_target_path = pathlib.Path("./tests/output/image.pdf") self.service.set_output_target(new_target_path) self.assertEqual(self.service.get_output_target(), new_target_path)