From 45444cf41d46c2a25bd4d34fb756fafa42ba4275 Mon Sep 17 00:00:00 2001 From: abcprintf Date: Mon, 17 Mar 2025 14:23:27 +0700 Subject: [PATCH] feat: implement PDF unlocking functionality with tests and setup --- README.md | 10 ++++++++++ eservice_unlock_pdf/__init__.py | 0 eservice_unlock_pdf/tests/test_unlock.py | 22 ++++++++++++++++++++++ eservice_unlock_pdf/unlock.py | 14 ++++++++++++++ setup.py | 9 +++++++++ tox.ini | 8 ++++++++ 6 files changed, 63 insertions(+) create mode 100644 eservice_unlock_pdf/__init__.py create mode 100644 eservice_unlock_pdf/tests/test_unlock.py create mode 100644 eservice_unlock_pdf/unlock.py create mode 100644 setup.py create mode 100644 tox.ini diff --git a/README.md b/README.md index d71c6a3..8c97c7b 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,14 @@ jupyter lab // run the script python unlock_pdf.py locked.pdf unlocked.pdf password +``` + +### Testing +```bash +tox +``` + +### uploade to pypi +```bash +twine upload dist/* ``` \ No newline at end of file diff --git a/eservice_unlock_pdf/__init__.py b/eservice_unlock_pdf/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/eservice_unlock_pdf/tests/test_unlock.py b/eservice_unlock_pdf/tests/test_unlock.py new file mode 100644 index 0000000..0742502 --- /dev/null +++ b/eservice_unlock_pdf/tests/test_unlock.py @@ -0,0 +1,22 @@ +import os +import unittest +from eservice_unlock_pdf.unlock import unlock_pdf + +class TestUnlockPDF(unittest.TestCase): + def setUp(self): + """กำหนด path ให้แน่นอน""" + self.input_pdf = os.path.abspath("files/simple.pdf") # ✅ ใช้ absolute path + self.output_pdf = os.path.abspath("outputs/simple-unlocked.pdf") + + def test_unlock_pdf(self): + """ทดสอบการปลดล็อก PDF ด้วยรหัสผ่านที่ถูกต้อง""" + result = unlock_pdf(self.input_pdf, self.output_pdf, "123456789A") + self.assertTrue(result) + + def test_wrong_password(self): + """ทดสอบกรณีใส่รหัสผิด ต้องคืนค่า False""" + result = unlock_pdf(self.input_pdf, self.output_pdf, "wrongpassword") + self.assertFalse(result) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/eservice_unlock_pdf/unlock.py b/eservice_unlock_pdf/unlock.py new file mode 100644 index 0000000..6e2d6ca --- /dev/null +++ b/eservice_unlock_pdf/unlock.py @@ -0,0 +1,14 @@ +import pikepdf + +def unlock_pdf(input_pdf, output_pdf, password): + try: + pdf = pikepdf.open(input_pdf, password=password) + pdf.save(output_pdf) + print(f"✅ PDF ปลดล็อกสำเร็จ! บันทึกที่: {output_pdf}") + return True + except pikepdf.PasswordError: # เช็คกรณีรหัสผ่านผิด + print("❌ รหัสผ่านไม่ถูกต้อง") + return False + except Exception as e: # เช็ค Error อื่น ๆ เช่น ไฟล์ไม่พบ หรือไฟล์ไม่ใช่ PDF + print(f"⚠️ Error: {e}") + return False \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7c0e63f --- /dev/null +++ b/setup.py @@ -0,0 +1,9 @@ +from setuptools import setup, find_packages + +setup( + name="eservice-unlock-pdf", + version="0.1.0", + packages=find_packages(), + install_requires=[], + test_suite="tests", +) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..ebb4832 --- /dev/null +++ b/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist = py38, py39, py310, py311, py312, py313 + +[testenv] +deps = + pytest + pikepdf +commands = pytest \ No newline at end of file