Skip to content

Commit

Permalink
Make pygments optional for pyface.api (#796)
Browse files Browse the repository at this point in the history
* Make it possible to import api even when pygments is not in the environment
  • Loading branch information
kitchoi authored Nov 12, 2020
1 parent bb51b65 commit 8b7311f
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pyface/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
from .key_pressed_event import KeyPressedEvent
from .message_dialog import error, information, warning, MessageDialog
from .progress_dialog import ProgressDialog
from .python_editor import PythonEditor
from .python_shell import PythonShell
try:
from .python_editor import PythonEditor
from .python_shell import PythonShell
except ImportError as _exception:
# Excuse pygments dependency (for Qt), otherwise re-raise
if _exception.name != "pygments":
raise
from .sorter import Sorter
from .single_choice_dialog import choose_one, SingleChoiceDialog
from .splash_screen import SplashScreen
Expand Down
166 changes: 166 additions & 0 deletions pyface/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# (C) Copyright 2005-2020 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" Test for pyface.api """

import unittest

from traits.etsconfig.api import ETSConfig

is_wx = (ETSConfig.toolkit == 'wx')
is_qt = ETSConfig.toolkit.startswith('qt')


class TestApi(unittest.TestCase):
""" Test importable items in any environment."""

def test_api_importable(self):
# make sure api is importable with the most minimal
# required dependencies, including in the absence of toolkit backends.
from pyface import api # noqa: F401


@unittest.skipIf(not is_qt, "This test is for qt.")
class TestApiQt(unittest.TestCase):
""" Test importable items in a Qt environment."""

def test_importable_items_minimal(self):
# Test items should be importable in a minimal Qt environment
# Pygments is excused. Attempt to import PythonEditor or PythonShell
# will fail in an environment without pygments will fail, just as it
# would if these items were imported directly from the corresponding
# subpackages.
from pyface.api import ( # noqa: F401
AboutDialog,
Alignment,
Application,
ApplicationWindow,
Border,
CANCEL,
Clipboard,
ConfirmationDialog,
Dialog,
DirectoryDialog,
ExpandablePanel,
FileDialog,
Filter,
GUI,
GUIApplication,
HasBorder,
HasMargin,
HeadingText,
Image,
ImageCache,
ImageResource,
ImageWidget,
KeyPressedEvent,
LayeredPanel,
MDIApplicationWindow,
MDIWindowMenu,
Margin,
MessageDialog,
MultiToolbarWindow,
NO,
OK,
ProgressDialog,
# PythonEditor and PythonShell are omitted
SingleChoiceDialog,
Sorter,
SplashScreen,
SplitApplicationWindow,
SplitDialog,
SplitPanel,
SystemMetrics,
Widget,
Window,
YES,
beep,
choose_one,
clipboard,
confirm,
error,
information,
warning,
)

def test_python_editor_python_shell_importable(self):
# If pygments is in the environment, PythonEditor and PythonShell
# should be importable.
try:
import pygments # noqa: F401
except ImportError:
raise self.skipTest("This test requires pygments.")

from pyface.api import ( # noqa: F401
PythonEditor,
PythonShell,
)


@unittest.skipIf(not is_wx, "This test is for wx.")
class TestApiWx(unittest.TestCase):
""" Test importable items in a wx environment."""

def test_importable_items(self):
# These items should always be importable for wx environment
from pyface.api import ( # noqa: F401
AboutDialog,
Alignment,
Application,
ApplicationWindow,
Border,
CANCEL,
Clipboard,
ConfirmationDialog,
Dialog,
DirectoryDialog,
ExpandablePanel,
FileDialog,
Filter,
GUI,
GUIApplication,
HasBorder,
HasMargin,
HeadingText,
Image,
ImageCache,
ImageResource,
ImageWidget,
KeyPressedEvent,
LayeredPanel,
MDIApplicationWindow,
MDIWindowMenu,
Margin,
MessageDialog,
MultiToolbarWindow,
NO,
OK,
ProgressDialog,
PythonEditor,
PythonShell,
SingleChoiceDialog,
Sorter,
SplashScreen,
SplitApplicationWindow,
SplitDialog,
SplitPanel,
SystemMetrics,
Widget,
Window,
YES,
beep,
choose_one,
clipboard,
confirm,
error,
fix_introspect_bug,
information,
warning,
)

0 comments on commit 8b7311f

Please sign in to comment.