From e873443473f4749d43824680bd169d2f3c27a1d3 Mon Sep 17 00:00:00 2001 From: Peter Sobot Date: Mon, 3 May 2021 11:37:38 -0400 Subject: [PATCH] Fix version warning in Py3.6. --- keynote_parser/__init__.py | 2 +- keynote_parser/bundle_utils.py | 18 ++++++++++++------ tests/test_bundle_utils.py | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 tests/test_bundle_utils.py diff --git a/keynote_parser/__init__.py b/keynote_parser/__init__.py index ea1ee81..6cb6077 100644 --- a/keynote_parser/__init__.py +++ b/keynote_parser/__init__.py @@ -5,7 +5,7 @@ import keynote_parser.macos_app_version __major_version__ = 1 -__patch_version__ = 0 +__patch_version__ = 1 __supported_keynote_version__ = keynote_parser.macos_app_version.MacOSAppVersion( "10.2", "7028.0.88", "1A122" ) diff --git a/keynote_parser/bundle_utils.py b/keynote_parser/bundle_utils.py index 826dff9..1dc906d 100644 --- a/keynote_parser/bundle_utils.py +++ b/keynote_parser/bundle_utils.py @@ -1,4 +1,5 @@ import os +import sys import warnings import plistlib import urllib @@ -38,7 +39,8 @@ def __init__(self, installed_keynote_version): new_issue_url = __new_issue_url__ + "?" + urllib.parse.urlencode({"title": issue_title}) super(UserWarning, self).__init__( ( - "KeynoteVersionWarning: " + Fore.RESET + "KeynoteVersionWarning: " + + Fore.RESET + "This version of keynote_parser (%s) was not built with support " "for\nthe currently installed version of Keynote.\n" "\tkeynote_parser version: %s\n" @@ -73,7 +75,7 @@ def __init__(self, installed_keynote_version): class CleanWarning(object): - def custom_format_warning(self, message, *args): + def custom_format_warning(self, message, *args, **kwargs): # Nasty hack - by putting the initial colour in the formatter, # the Warnings filter still lets users ignore this warning as # the message itself doesn't start with an ANSI escape sequence. @@ -90,22 +92,26 @@ def __exit__(self, *args, **kwargs): DID_WARN = False -def warn_once_on_newer_keynote(): +def warn_once_on_newer_keynote(installed_keynote_version=None): global DID_WARN if DID_WARN: return False - installed_keynote_version = get_installed_keynote_version() + installed_keynote_version = installed_keynote_version or get_installed_keynote_version() if not installed_keynote_version: return False if __supported_keynote_version__ < installed_keynote_version: - with CleanWarning(): + try: + with CleanWarning(): + warnings.warn(KeynoteVersionWarning(installed_keynote_version)) + except Exception: + # In case CleanWarning throws an error warnings.warn(KeynoteVersionWarning(installed_keynote_version)) DID_WARN = True return DID_WARN -if not __command_line_invocation__: +if not __command_line_invocation__ and "pytest" not in sys.modules: warn_once_on_newer_keynote() diff --git a/tests/test_bundle_utils.py b/tests/test_bundle_utils.py new file mode 100644 index 0000000..d62ca49 --- /dev/null +++ b/tests/test_bundle_utils.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +import pytest + +from keynote_parser import bundle_utils +from keynote_parser.macos_app_version import MacOSAppVersion + + +def test_warn_on_old_version(capsys): + dummy_version = MacOSAppVersion("9999.9", "10000.0.00", "9A000") + with pytest.warns(bundle_utils.KeynoteVersionWarning): + bundle_utils.warn_once_on_newer_keynote(installed_keynote_version=dummy_version) + + # Second call/import should not warn + with pytest.warns(None): + bundle_utils.warn_once_on_newer_keynote(installed_keynote_version=dummy_version)