Skip to content

Commit f7ebd62

Browse files
committed
refactor conditional importlib for python < 3.12
1 parent ae558f9 commit f7ebd62

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

netbox_qrcode/utilities.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import base64
22
import qrcode
3-
3+
import sys
44
from io import BytesIO
55
from PIL import Image, ImageFont, ImageDraw
6-
from importlib import resources as importlib_resources
6+
7+
if sys.version_info.major == 3 and sys.version_info.minor < 12:
8+
from pkg_resources import resource_stream
9+
else:
10+
from importlib import resources as importlib_resources
711

812
def get_qr_with_text(qr, descr):
913
dsi = get_qr_text(qr.size, descr)
@@ -33,7 +37,10 @@ def get_qr_text(max_size, text, font='TahomaBold', font_size=0):
3337
text_too_large = True
3438
font_size = 56
3539
while text_too_large:
36-
file_path = importlib_resources.files(__name__).joinpath('fonts/{}.ttf'.format(font))
40+
if sys.version_info.major == 3 and sys.version_info.minor < 12:
41+
file_path = resource_stream(__name__, 'fonts/{}.ttf'.format(font))
42+
else:
43+
file_path = importlib_resources.files(__name__).joinpath('fonts/{}.ttf'.format(font))
3744
try:
3845
fnt = ImageFont.truetype(file_path, font_size)
3946
except Exception:
@@ -45,7 +52,10 @@ def get_qr_text(max_size, text, font='TahomaBold', font_size=0):
4552
text_too_large = False
4653
font_size -= 1
4754
else:
48-
file_path=importlib_resources.files(__name__).joinpath('fonts/{}.ttf'.format(font))
55+
if sys.version_info.major == 3 and sys.version_info.minor < 12:
56+
file_path = resource_stream(__name__, 'fonts/{}.ttf'.format(font))
57+
else:
58+
file_path = importlib_resources.files(__name__).joinpath('fonts/{}.ttf'.format(font))
4959
try:
5060
fnt = ImageFont.truetype(file_path, font_size)
5161
except Exception:

0 commit comments

Comments
 (0)