Skip to content

Commit

Permalink
Make terminaltables truly optional (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval committed Aug 18, 2018
1 parent de5c038 commit 5bd474d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
19 changes: 15 additions & 4 deletions sample_sheet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from requests.structures import CaseInsensitiveDict
from smart_open import smart_open # type: ignore
from tabulate import tabulate # type: ignore
from terminaltables import SingleTable # type: ignore
from typing import (
Any,
Generator,
Expand All @@ -33,6 +32,15 @@

__version__ = '0.8.0'


try: # pragma: no cover
from terminaltables import SingleTable # type: ignore

HAS_TERMINALTABLES = True
except ImportError:
HAS_TERMINALTABLES = False


DESIGN_HEADER: List[str] = [
'Sample_ID',
'Sample_Name',
Expand Down Expand Up @@ -929,11 +937,14 @@ def __str__(self) -> str:
"""
try:
isatty = os.isatty(sys.stdout.fileno())
in_terminal = os.isatty(sys.stdout.fileno())
except OSError: # pragma: no cover
isatty = False
in_terminal = False

return self._repr_tty_() if isatty else self.__repr__()
if in_terminal and HAS_TERMINALTABLES:
return self._repr_tty_()
else:
return self.__repr__()

def _repr_tty_(self) -> str:
"""Return a summary of this sample sheet in a TTY compatible codec."""
Expand Down
9 changes: 2 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
license='MIT',
zip_safe=True,
packages=find_packages(),
install_requires=[
'click',
'smart_open>=1.5.4',
'tabulate',
'terminaltables',
],
# extras_requires={'cli': ['terminaltables']},
install_requires=['click', 'smart_open>=1.5.4', 'tabulate'],
extras_requires={'cli': ['terminaltables']},
scripts=['scripts/sample-sheet'],
keywords='illumina samplesheet sample sheet parser bioinformatics',
classifiers=[
Expand Down
8 changes: 7 additions & 1 deletion tests/test_sample_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from requests.exceptions import HTTPError

from sample_sheet import * # Test import of __all__
from sample_sheet import HAS_TERMINALTABLES

RESOURCES = Path(__file__).absolute().resolve().parent / 'resources'

Expand Down Expand Up @@ -717,6 +718,11 @@ def test_repr(self):
'SampleSheet(\'{}\')'.format(infile),
)

@pytest.mark.xfail(reasion="Not sure if multi-line diff is correct")
@pytest.mark.skipif(
not HAS_TERMINALTABLES,
reason="We need `terminaltables` installed here",
)
def test_repr_tty(self):
"""Test ``_repr_tty_()``"""
self.maxDiff = 3000
Expand Down Expand Up @@ -763,4 +769,4 @@ def test_repr_tty(self):
'\n└───────────┴──────────────────┘'
)

# self.assertMultiLineEqual(source, target)
self.assertMultiLineEqual(source, target)

0 comments on commit 5bd474d

Please sign in to comment.