make PYTHON workaround uniform

cv2_1
scito 1 year ago
parent 67c4f737c4
commit a3bda6ff8e

@ -41,7 +41,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations # for compatibility with PYTHON < 3.11 from __future__ import annotations # workaround for PYTHON <= 3.10
import argparse import argparse
import base64 import base64
@ -56,7 +56,7 @@ from enum import Enum
from operator import add from operator import add
from typing import Any, List, Optional, TextIO, Tuple, Union from typing import Any, List, Optional, TextIO, Tuple, Union
# PYTHON < 3.8: compatibility # workaround for PYTHON <= 3.7: compatibility
if sys.version_info >= (3, 8): if sys.version_info >= (3, 8):
from typing import Final, TypedDict from typing import Final, TypedDict
else: else:
@ -82,7 +82,7 @@ See in README.md for the installation of the libzbar0.
Exception: {e}""") Exception: {e}""")
# Types # Types
# PYTHON > 3.9: Final[tuple[int]] # workaround for PYTHON <= 3.9: Final[tuple[int]]
ColorBGR = Tuple[int, int, int] # RGB Color specified as Blue, Green, Red ColorBGR = Tuple[int, int, int] # RGB Color specified as Blue, Green, Red
Point = Tuple[int, int] Point = Tuple[int, int]
@ -92,7 +92,7 @@ Exception: {e}""")
FONT_THICKNESS: Final[int] = 1 FONT_THICKNESS: Final[int] = 1
FONT_LINE_STYLE: Final[int] = cv2.LINE_AA FONT_LINE_STYLE: Final[int] = cv2.LINE_AA
RECT_THICKNESS: Final[int] = 5 RECT_THICKNESS: Final[int] = 5
# PYTHON <= 3.7: must use () for assignments # workaround for PYTHON <= 3.7: must use () for assignments
START_POS_TEXT: Final[Point] = (5, 20) START_POS_TEXT: Final[Point] = (5, 20)
NORMAL_COLOR: Final[ColorBGR] = (255, 0, 255) NORMAL_COLOR: Final[ColorBGR] = (255, 0, 255)
SUCCESS_COLOR: Final[ColorBGR] = (0, 255, 0) SUCCESS_COLOR: Final[ColorBGR] = (0, 255, 0)
@ -103,16 +103,16 @@ Exception: {e}""")
except ImportError: except ImportError:
qreader_available = False qreader_available = False
# Workaround for PYTHON < 3.10: Union[int, None] used instead of int | None # Workaround for PYTHON <= 3.9: Union[int, None] used instead of int | None
# Types # Types
Args = argparse.Namespace Args = argparse.Namespace
OtpUrl = str OtpUrl = str
# PYTHON > 3.7: Otp = TypedDict('Otp', {'name': str, 'secret': str, 'issuer': str, 'type': str, 'counter': int | None, 'url': OtpUrl}) # workaround for PYTHON <= 3.7: Otp = TypedDict('Otp', {'name': str, 'secret': str, 'issuer': str, 'type': str, 'counter': int | None, 'url': OtpUrl})
Otp = TypedDict('Otp', {'name': str, 'secret': str, 'issuer': str, 'type': str, 'counter': Union[int, None], 'url': OtpUrl}) Otp = TypedDict('Otp', {'name': str, 'secret': str, 'issuer': str, 'type': str, 'counter': Union[int, None], 'url': OtpUrl})
# PYTHON > 3.9: Otps = list[Otp] # workaround for PYTHON <= 3.9: Otps = list[Otp]
Otps = List[Otp] Otps = List[Otp]
# PYTHON > 3.9: OtpUrls = list[OtpUrl] # workaround for PYTHON <= 3.9: OtpUrls = list[OtpUrl]
OtpUrls = List[OtpUrl] OtpUrls = List[OtpUrl]
QRMode = Enum('QRMode', ['QREADER', 'DEEP_QREADER', 'ZBAR', 'CV2', 'WECHAT'], start=0) QRMode = Enum('QRMode', ['QREADER', 'DEEP_QREADER', 'ZBAR', 'CV2', 'WECHAT'], start=0)
@ -351,7 +351,7 @@ def get_otp_urls_from_file(filename: str, args: Args) -> OtpUrls:
def read_lines_from_text_file(filename: str) -> list[str]: def read_lines_from_text_file(filename: str) -> list[str]:
if verbose: print(f"Reading lines of {filename}") if verbose: print(f"Reading lines of {filename}")
# PYTHON >= 3.10 support encoding # workaround for PYTHON <= 3.9 support encoding
if sys.version_info >= (3, 10): if sys.version_info >= (3, 10):
finput = fileinput.input(filename, encoding='utf-8') finput = fileinput.input(filename, encoding='utf-8')
else: else:
@ -461,7 +461,7 @@ def convert_img_to_otp_url(filename: str, args: Args) -> OtpUrls:
return otp_urls return otp_urls
# PYTHON >= 3.10 use: pb.MigrationPayload | None # workaround for PYTHON <= 3.9 use: pb.MigrationPayload | None
def get_payload_from_otp_url(otp_url: str, i: int, source: str) -> Optional[pb.MigrationPayload]: def get_payload_from_otp_url(otp_url: str, i: int, source: str) -> Optional[pb.MigrationPayload]:
if not otp_url.startswith('otpauth-migration://'): if not otp_url.startswith('otpauth-migration://'):
msg = f"input is not a otpauth-migration:// url\nsource: {source}\ninput: {otp_url}" msg = f"input is not a otpauth-migration:// url\nsource: {source}\ninput: {otp_url}"
@ -474,7 +474,7 @@ def get_payload_from_otp_url(otp_url: str, i: int, source: str) -> Optional[pb.M
if verbose > 2: print(f"\nDEBUG: parsed_url={parsed_url}") if verbose > 2: print(f"\nDEBUG: parsed_url={parsed_url}")
try: try:
params = urlparse.parse_qs(parsed_url.query, strict_parsing=True) params = urlparse.parse_qs(parsed_url.query, strict_parsing=True)
except Exception: # Necessary for PYTHON < 3.11 except Exception: # workaround for PYTHON <= 3.10
params = {} params = {}
if verbose > 2: print(f"\nDEBUG: querystring params={params}") if verbose > 2: print(f"\nDEBUG: querystring params={params}")
if 'data' not in params: if 'data' not in params:

@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations # for compatibility with PYTHON < 3.11 from __future__ import annotations # workaround for PYTHON <= 3.10
import unittest import unittest
import extract_otp_secrets import extract_otp_secrets

@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations # for compatibility with PYTHON < 3.11 from __future__ import annotations # workaround for PYTHON <= 3.10
import io import io
import os import os
import pathlib import pathlib
@ -356,6 +356,7 @@ def test_normalize_bytes() -> None:
# Generate verbose output: python3.11 src/extract_otp_secrets.py example_export.txt -v -n > tests/data/print_verbose_output.txt # Generate verbose output: python3.11 src/extract_otp_secrets.py example_export.txt -v -n > tests/data/print_verbose_output.txt
# workaround for PYTHON <= 3.10
@pytest.mark.skipif(sys.version_info < (3, 10), reason="fileinput.input encoding exists since PYTHON 3.10") @pytest.mark.skipif(sys.version_info < (3, 10), reason="fileinput.input encoding exists since PYTHON 3.10")
def test_extract_verbose(capsys: pytest.CaptureFixture[str], relaxed: bool) -> None: def test_extract_verbose(capsys: pytest.CaptureFixture[str], relaxed: bool) -> None:
# Act # Act

@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations # for compatibility with PYTHON < 3.11 from __future__ import annotations # workaround for PYTHON <= 3.10
import io import io
import os import os
import sys import sys

@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations # for compatibility with PYTHON < 3.11 from __future__ import annotations # workaround for PYTHON <= 3.10
import csv import csv
import glob import glob
import io import io
@ -27,12 +27,12 @@ from typing import BinaryIO, Any, Union, List
# Types # Types
# PYTHON < 3.10: Workaround for str | pathlib.Path # workaround for PYTHON <= 3.9: Workaround for str | pathlib.Path
PathLike = Union[str, pathlib.Path] PathLike = Union[str, pathlib.Path]
# Ref. https://stackoverflow.com/a/16571630 # Ref. https://stackoverflow.com/a/16571630
# PYTHON 3.11: class Capturing(list[Any]): # workaround for PYTHON <= 3.10: class Capturing(list[Any]):
class Capturing(List[Any]): class Capturing(List[Any]):
'''Capture stdout and stderr '''Capture stdout and stderr
Usage: Usage:

Loading…
Cancel
Save