Skip to content

Commit

Permalink
Merge pull request #31 from psobot/psobot/fix-version-warning
Browse files Browse the repository at this point in the history
Fix Keynote version warning in Py3.6.
  • Loading branch information
psobot committed May 3, 2021
2 parents 0f602ed + e873443 commit 502e725
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion keynote_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
18 changes: 12 additions & 6 deletions keynote_parser/bundle_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import warnings
import plistlib
import urllib
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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()
15 changes: 15 additions & 0 deletions tests/test_bundle_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 502e725

Please sign in to comment.