|
1 | | -# For backwards compatibility, importing the PIL drawers here. |
2 | | -import contextlib |
3 | | - |
4 | | -with contextlib.suppress(ImportError): |
5 | | - from .pil import ( |
6 | | - CircleModuleDrawer, # noqa: F401 |
7 | | - GappedCircleModuleDrawer, # noqa: F401 |
8 | | - GappedSquareModuleDrawer, # noqa: F401 |
9 | | - HorizontalBarsDrawer, # noqa: F401 |
10 | | - RoundedModuleDrawer, # noqa: F401 |
11 | | - SquareModuleDrawer, # noqa: F401 |
12 | | - VerticalBarsDrawer, # noqa: F401 |
13 | | - ) |
| 1 | +""" |
| 2 | +Module for lazy importing of PIL drawers with a deprecation warning. |
| 3 | +
|
| 4 | +Currently, importing a PIL drawer from this module is allowed for backwards |
| 5 | +compatibility but will raise a DeprecationWarning. |
| 6 | +
|
| 7 | +This will be removed in v9.0. |
| 8 | +""" |
| 9 | + |
| 10 | +import warnings |
| 11 | + |
| 12 | +from qrcode.constants import PIL_AVAILABLE |
| 13 | + |
| 14 | + |
| 15 | +def __getattr__(name): |
| 16 | + """Lazy import with deprecation warning for PIL drawers.""" |
| 17 | + # List of PIL drawer names that should trigger deprecation warnings |
| 18 | + pil_drawers = { |
| 19 | + "CircleModuleDrawer", |
| 20 | + "GappedCircleModuleDrawer", |
| 21 | + "GappedSquareModuleDrawer", |
| 22 | + "HorizontalBarsDrawer", |
| 23 | + "RoundedModuleDrawer", |
| 24 | + "SquareModuleDrawer", |
| 25 | + "VerticalBarsDrawer", |
| 26 | + } |
| 27 | + |
| 28 | + if name in pil_drawers: |
| 29 | + # Only render a warning if PIL is actually installed. Otherwise it would |
| 30 | + # raise an ImportError directly, which is fine. |
| 31 | + if PIL_AVAILABLE: |
| 32 | + warnings.warn( |
| 33 | + f"Importing '{name}' directly from this module is deprecated." |
| 34 | + f"Please use 'from qrcode.image.styles.moduledrawers.pil import {name}' " |
| 35 | + f"instead. This backwards compatibility import will be removed in v9.0.", |
| 36 | + DeprecationWarning, |
| 37 | + stacklevel=2, |
| 38 | + ) |
| 39 | + |
| 40 | + # Import and return the drawer from the pil module |
| 41 | + from . import pil # noqa: PLC0415 |
| 42 | + |
| 43 | + return getattr(pil, name) |
| 44 | + |
| 45 | + # For any other attribute, raise AttributeError |
| 46 | + msg = f"module {__name__!r} has no attribute {name!r}" |
| 47 | + raise AttributeError(msg) |
0 commit comments