From 17db2ec411ae0482a4c125cb2af3e51a42f4073d Mon Sep 17 00:00:00 2001 From: Mike A Date: Thu, 3 Apr 2025 15:43:52 +0100 Subject: [PATCH 1/3] Shift the image factory code to work with the output parameter --- qrcode/console_scripts.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/qrcode/console_scripts.py b/qrcode/console_scripts.py index ebe8810f..36462bbb 100755 --- a/qrcode/console_scripts.py +++ b/qrcode/console_scripts.py @@ -112,29 +112,28 @@ def raise_error(msg: str) -> NoReturn: else: qr.add_data(data, optimize=opts.optimize) + if image_factory is None and (os.isatty(sys.stdout.fileno()) or opts.ascii): + qr.print_ascii(tty=not opts.ascii) + return + + kwargs = {} + aliases: Optional[DrawerAliases] = getattr(qr.image_factory, "drawer_aliases", None) + if opts.factory_drawer: + if not aliases: + raise_error("The selected factory has no drawer aliases.") + if opts.factory_drawer not in aliases: + raise_error( + f"{opts.factory_drawer} factory drawer not found." + f" Expected {commas(aliases)}" + ) + drawer_cls, drawer_kwargs = aliases[opts.factory_drawer] + kwargs["module_drawer"] = drawer_cls(**drawer_kwargs) + if opts.output: - img = qr.make_image() + img = qr.make_image(**kwargs) with open(opts.output, "wb") as out: img.save(out) else: - if image_factory is None and (os.isatty(sys.stdout.fileno()) or opts.ascii): - qr.print_ascii(tty=not opts.ascii) - return - - kwargs = {} - aliases: Optional[DrawerAliases] = getattr( - qr.image_factory, "drawer_aliases", None - ) - if opts.factory_drawer: - if not aliases: - raise_error("The selected factory has no drawer aliases.") - if opts.factory_drawer not in aliases: - raise_error( - f"{opts.factory_drawer} factory drawer not found." - f" Expected {commas(aliases)}" - ) - drawer_cls, drawer_kwargs = aliases[opts.factory_drawer] - kwargs["module_drawer"] = drawer_cls(**drawer_kwargs) img = qr.make_image(**kwargs) sys.stdout.flush() From f11fcdfe11c9ff7f372458fb878dec2fd593a2b7 Mon Sep 17 00:00:00 2001 From: Mike A Date: Thu, 3 Apr 2025 15:50:29 +0100 Subject: [PATCH 2/3] Prevent the tool always outputting as ascii by default --- qrcode/console_scripts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qrcode/console_scripts.py b/qrcode/console_scripts.py index 36462bbb..6bfeed0c 100755 --- a/qrcode/console_scripts.py +++ b/qrcode/console_scripts.py @@ -112,7 +112,7 @@ def raise_error(msg: str) -> NoReturn: else: qr.add_data(data, optimize=opts.optimize) - if image_factory is None and (os.isatty(sys.stdout.fileno()) or opts.ascii): + if opts.ascii: qr.print_ascii(tty=not opts.ascii) return From 89bf3e74ee2b048a2a40724b049a0c18d39e85c0 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Wed, 23 Jul 2025 12:59:39 +0200 Subject: [PATCH 3/3] Removed unused import --- qrcode/console_scripts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qrcode/console_scripts.py b/qrcode/console_scripts.py index 6bfeed0c..330b10ea 100755 --- a/qrcode/console_scripts.py +++ b/qrcode/console_scripts.py @@ -7,7 +7,6 @@ """ import optparse -import os import sys from typing import NoReturn, Optional from collections.abc import Iterable