Skip to content

Commit

Permalink
🚸 Handle non-Unicode terminals (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
shnizzedy authored Sep 24, 2020
2 parents 0e4f7c9 + e93bba2 commit 9331fad
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
=========
Changelog
=========
`Version 0.3.1 <https://github.com/FCP-INDI/cpac/releases/tag/v0.3.1>`_
=======================================================================
* 🚸 Print without emoji if terminal can't handle extended Unicode set
* 📚 Add PyPI badge to README

`Version 0.3.0 <https://github.com/FCP-INDI/cpac/releases/tag/v0.3.0>`_
=======================================================================
* 📛 Rename project from `shnizzedy/cpac-python-package <https://github.com/shnizzedy/cpac-python-package>`_ to `FCP-INDI/cpac <https://github.com/FCP-INDI/cpac>`_
Expand Down
14 changes: 9 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
========================================================================
C-PAC Python Package |build-status| |github-version| |upload| |coverage|
========================================================================
========================================================================================
C-PAC Python Package |build-status| |github-version| |upload| |pypi-version| |coverage|
========================================================================================


A Python package that wraps `C-PAC <http://fcp-indi.github.io>`_, enabling users to install cpac with `pip <https://pip.pypa.io>`_ and run from the command line.
Expand Down Expand Up @@ -98,6 +98,9 @@ Usage
.. END USAGE
.. |pypi-version| image:: https://badge.fury.io/py/cpac.svg
:target: https://pypi.org/project/cpac/
:alt: PyPI version
.. |github-version| image:: https://img.shields.io/github/tag/FCP-INDI/cpac.svg
:target: https://github.com/FCP-INDI/cpac/releases
:alt: GitHub version
Expand All @@ -108,5 +111,6 @@ Usage
:target: https://coveralls.io/github/FCP-INDI/cpac
:alt: coverage badge
.. |upload| image:: https://github.com/FCP-INDI/cpac/workflows/Upload%20Python%20Package/badge.svg
:target: https://pypi.org/project/cpac-py/
:alt: upload Python package
:target: https://pypi.org/project/cpac/
:alt: upload Python package to PyPI
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

# General information about the project.
project = u'cpac'
copyright = u'2019, anibalsolon'
copyright = u'2019, anibalsolon; 2020 C-PAC Team'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers =
Operating System :: OS Independent
Programming Language :: Python :: 3
Topic :: Scientific/Engineering :: Bio-Informatics
version = 0.3.0
version = 0.3.1

[options]
zip_safe = False
Expand Down
2 changes: 1 addition & 1 deletion src/cpac/backends/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Docker(Backend):
def __init__(self, **kwargs):
self.platform = Platform_Meta('Docker', '🐳')
print(f"Loading {self.platform.symbol} {self.platform.name}")
self._print_loading_with_symbol(self.platform.name)
self.client = docker.from_env()
try:
self.client.ping()
Expand Down
19 changes: 14 additions & 5 deletions src/cpac/backends/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ def _load_logging(self):
) for j in self.bindings['volumes'][i]
])
t.columns = ['local', self.platform.name, 'mode']
print(" ".join([
f"Loading {self.platform.symbol}",
self.image,
"with these directory bindings:"
]))
self._print_loading_with_symbol(
" ".join([
self.image,
"with these directory bindings:"
])
)
print(textwrap.indent(
tabulate(t.applymap(
lambda x: (
Expand All @@ -93,6 +94,14 @@ def _prep_binding(self, binding_path_local, binding_path_remote):
os.path.abspath(binding_path_remote)
)

def _print_loading_with_symbol(self, message, prefix='Loading'):
if prefix is not None:
print(prefix, end=' ')
try:
print(' '.join([self.platform.symbol, message]))
except UnicodeEncodeError:
print(message)

def _set_bindings(self, **kwargs):
tag = kwargs.get('tag', None)
tag = tag if isinstance(tag, str) else None
Expand Down
2 changes: 1 addition & 1 deletion src/cpac/backends/singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, **kwargs):
if kwargs.get("working_dir") is not None:
pwd = kwargs["working_dir"]
os.chdir(pwd)
print(f"Loading {self.platform.symbol} {self.platform.name}")
self._print_loading_with_symbol(self.platform.name)
if image and isinstance(image, str) and os.path.exists(image):
self.image = image
elif tag and isinstance(tag, str): # pragma: no cover
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cpac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from contextlib import redirect_stdout
from cpac.backends import Backends
from io import StringIO, TextIOWrapper, BytesIO


@pytest.mark.parametrize('platform', ['docker', 'singularity'])
def test_loading_message(platform):
redirect_out = StringIO()
with redirect_stdout(redirect_out):
loaded = Backends(platform)
with_symbol = ' '.join([
'Loading',
loaded.platform.symbol,
loaded.platform.name
])
assert with_symbol in redirect_out.getvalue()

redirect_out = TextIOWrapper(
BytesIO(), encoding='latin-1', errors='strict', write_through=True)
with redirect_stdout(redirect_out):
loaded = Backends(platform)
without_symbol = ' '.join([
'Loading',
loaded.platform.name
])
assert without_symbol in redirect_out.buffer.getvalue().decode()

0 comments on commit 9331fad

Please sign in to comment.