From b19c15093165e38f72f5d567d07b4940232a1aa4 Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Mon, 26 May 2025 16:12:54 -0700 Subject: [PATCH 1/5] Create __main__.py --- qrcode/__main__.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 qrcode/__main__.py diff --git a/qrcode/__main__.py b/qrcode/__main__.py new file mode 100644 index 00000000..8026a67f --- /dev/null +++ b/qrcode/__main__.py @@ -0,0 +1,3 @@ +from .console_scripts import main + +main() From e8797986cc0fe84f881fd86b097472b17f859ea0 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 22 Jul 2025 12:02:05 +0200 Subject: [PATCH 2/5] Tests for Module feature --- qrcode.png | Bin 0 -> 697 bytes qrcode/tests/test_module.py | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 qrcode.png create mode 100644 qrcode/tests/test_module.py diff --git a/qrcode.png b/qrcode.png new file mode 100644 index 0000000000000000000000000000000000000000..1677bc48bf34a2fb4eb5f32fe93d4f9684634314 GIT binary patch literal 697 zcmV;q0!ICbP)`>N^;^HA9_KgaqpGSVAilIf7SSP}6lhdc zRqy#n>v4W#K3jx}T&@CG{FWz?c5gk-Z^O55U&H~0Fts{%pyYk^IR9XN6cNU*QRxBc zzgv&01_g`NW`Zn0NC?2X1@dhAo&PU>6cLdQhzaC(YSbz%=2AZItH=3`_!7Tw zo0Q{a0>V`+u=msB{I)#B{KN#97n)E_jp`64if39(k`B+N-}w)g?c3_98s4arF)?8l z+4uLTe52B55nvW6);FKZc0;hAz!U3t{-3LyzomK_FHr*c0%&TqxDK5K(6z2-(C)bp31>gVqvdDa_Bqr}wIaw|1u z=Ac8}c;DY+@;We!W#tY^z!I+3^VnUF^IP#XeuG&fVxqdk_F_@#cfBz?=zs%c5r>h<$M`ISF1 zuUpbVdlT%f*hVj)Qm6DaxkQe`dxAV?B f@4WNQ@6P`PtBA^^q&roM00000NkvXXu0mjfqa9x| literal 0 HcmV?d00001 diff --git a/qrcode/tests/test_module.py b/qrcode/tests/test_module.py new file mode 100644 index 00000000..f8505e42 --- /dev/null +++ b/qrcode/tests/test_module.py @@ -0,0 +1,52 @@ +import subprocess +import sys +import tempfile +from pathlib import Path + + +def test_module_help(): + """Test that the module can be executed with the help flag.""" + result = subprocess.run( + [sys.executable, "-m", "qrcode", "-h"], + capture_output=True, + text=True, + check=False, + ) + + # Check that the command executed successfully + assert result.returncode == 0 + + # Check that the help output contains expected information + assert "qr - Convert stdin" in result.stdout + assert "--output" in result.stdout + assert "--factory" in result.stdout + + +def test_module_generate_qrcode(): + """Test that the module can generate a QR code image.""" + with tempfile.TemporaryDirectory() as temp_dir: + output_path = Path(temp_dir) / "qrcode.png" + + # Run the command to generate a QR code + result = subprocess.run( + [ + sys.executable, + "-m", + "qrcode", + "--output", + str(output_path), + "https://github.com/lincolnloop/python-qrcode", + ], + capture_output=True, + text=True, + check=False, + ) + + # Check that the command executed successfully + assert result.returncode == 0 + + # Check that the output file was created + assert output_path.exists() + + # Check that the file is not empty and is a valid image file + assert output_path.stat().st_size > 0 From bbdbeb7fde7b0618a6d5073bcb2950110cd0feaf Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 22 Jul 2025 12:18:36 +0200 Subject: [PATCH 3/5] Skip image creation if PIL is not installed --- qrcode.png | Bin 697 -> 0 bytes qrcode/tests/test_module.py | 10 ++++++++++ 2 files changed, 10 insertions(+) delete mode 100644 qrcode.png diff --git a/qrcode.png b/qrcode.png deleted file mode 100644 index 1677bc48bf34a2fb4eb5f32fe93d4f9684634314..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 697 zcmV;q0!ICbP)`>N^;^HA9_KgaqpGSVAilIf7SSP}6lhdc zRqy#n>v4W#K3jx}T&@CG{FWz?c5gk-Z^O55U&H~0Fts{%pyYk^IR9XN6cNU*QRxBc zzgv&01_g`NW`Zn0NC?2X1@dhAo&PU>6cLdQhzaC(YSbz%=2AZItH=3`_!7Tw zo0Q{a0>V`+u=msB{I)#B{KN#97n)E_jp`64if39(k`B+N-}w)g?c3_98s4arF)?8l z+4uLTe52B55nvW6);FKZc0;hAz!U3t{-3LyzomK_FHr*c0%&TqxDK5K(6z2-(C)bp31>gVqvdDa_Bqr}wIaw|1u z=Ac8}c;DY+@;We!W#tY^z!I+3^VnUF^IP#XeuG&fVxqdk_F_@#cfBz?=zs%c5r>h<$M`ISF1 zuUpbVdlT%f*hVj)Qm6DaxkQe`dxAV?B f@4WNQ@6P`PtBA^^q&roM00000NkvXXu0mjfqa9x| diff --git a/qrcode/tests/test_module.py b/qrcode/tests/test_module.py index f8505e42..b623a38a 100644 --- a/qrcode/tests/test_module.py +++ b/qrcode/tests/test_module.py @@ -3,6 +3,15 @@ import tempfile from pathlib import Path +import pytest + +try: + from PIL import Image, ImageDraw + + PIL_NOT_INSTALLED = False +except ImportError: + PIL_NOT_INSTALLED = True + def test_module_help(): """Test that the module can be executed with the help flag.""" @@ -22,6 +31,7 @@ def test_module_help(): assert "--factory" in result.stdout +@pytest.mark.skipif(PIL_NOT_INSTALLED, reason="PIL is not installed") def test_module_generate_qrcode(): """Test that the module can generate a QR code image.""" with tempfile.TemporaryDirectory() as temp_dir: From 45c2f86c6b0e1c448f03030eeb34776605c40841 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 22 Jul 2025 12:18:41 +0200 Subject: [PATCH 4/5] Changelog Update --- CHANGES.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index b8c7e492..1b7e74bf 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,15 @@ Change log ========== +WIP +=== + +- Allow execution as a Python module (@stefansjs in `#400`_):: + + python -m qrcode --output qrcode.png "hello world" + +.. _#400: https://github.com/lincolnloop/python-qrcode/pull/400 + 8.2 (01 May 2025) ================= From 5071337d9fafadc7724694ec72a90a74fe4e5b6f Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 22 Jul 2025 12:23:20 +0200 Subject: [PATCH 5/5] Ignore unused import --- qrcode/tests/test_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qrcode/tests/test_module.py b/qrcode/tests/test_module.py index b623a38a..82785b0c 100644 --- a/qrcode/tests/test_module.py +++ b/qrcode/tests/test_module.py @@ -6,7 +6,7 @@ import pytest try: - from PIL import Image, ImageDraw + from PIL import Image, ImageDraw # noqa: F401 PIL_NOT_INSTALLED = False except ImportError: