Skip to content

Commit

Permalink
fix: print lines on context manager exit only (#6)
Browse files Browse the repository at this point in the history
* build: updated version
* build: add support for Python 3.11 and 3.12 remove 3.7
* fix: print lines on context manager exit only
* test: rename unit test file
* build: cleanup dockerfile
* fix: lint errors
* ci: update version of upload artifact
* ci: update cron
---------
Signed-off-by: Emilio Reyes <soda480@gmail.com>
  • Loading branch information
soda480 authored Nov 25, 2023
1 parent 3f0104d commit e152778
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: build
on:
schedule:
- cron: "0 8 * * *"
- cron: "0 8 * * 2,4,6"
push:
branches:
- '**'
Expand All @@ -12,7 +12,7 @@ jobs:
build-images:
strategy:
matrix:
version: ['3.7', '3.8', '3.9', '3.10']
version: ['3.8', '3.9', '3.10', '3.11', '3.12']
name: Build Python Docker images
runs-on: ubuntu-20.04
steps:
Expand All @@ -27,7 +27,7 @@ jobs:
docker save --output images/list2term-${{ matrix.version }}.tar list2term:${{ matrix.version }}
- name: upload list2term ${{ matrix.version }} image artifact
if: ${{ matrix.version == '3.9' }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: image
path: images/list2term-${{ matrix.version }}.tar
Expand All @@ -37,7 +37,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: download image artifact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: image
path: images/
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ FROM python:${PYTHON_VERSION}-slim
ENV PYTHONDONTWRITEBYTECODE 1
WORKDIR /code
COPY . /code/
RUN pip install --upgrade pip && pip install pybuilder faker
RUN pip install --upgrade pip && \
pip install pybuilder faker
RUN pyb -X
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![coverage](https://img.shields.io/badge/coverage-93%25-brightgreen)](https://pybuilder.io/)
[![vulnerabilities](https://img.shields.io/badge/vulnerabilities-None-brightgreen)](https://pypi.org/project/bandit/)
[![PyPI version](https://badge.fury.io/py/list2term.svg)](https://badge.fury.io/py/list2term)
[![python](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10-teal)](https://www.python.org/downloads/)
[![python](https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-teal)](https://www.python.org/downloads/)

The `list2term` module provides a convenient way to mirror a list to the terminal and helper methods to display messages from concurrent [asyncio](https://docs.python.org/3/library/asyncio.html) or [multiprocessing Pool](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool) processes. The `list2term.Lines` class is a subclass of [collections.UserList](https://docs.python.org/3/library/collections.html#collections.UserList) and is tty aware thus it is safe to use in non-tty environments. This class takes a list instance as an argument and when instantiated is accessible via the data attribute. The list can be any iterable, but its elements need to be printable; they should implement __str__ function. The intent of this class is to display relatively small lists to the terminal and dynamically update the terminal when list elements are upated, added or removed. Thus it is able to mirror a List of objects to the terminal.

Expand Down Expand Up @@ -210,8 +210,7 @@ Clone the repository and ensure the latest version of Docker is installed on you
Build the Docker image:
```sh
docker image build \
-t \
list2term:latest .
-t list2term:latest .
```

Run the Docker container:
Expand Down
7 changes: 4 additions & 3 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
authors = [Author('Emilio Reyes', 'soda480@gmail.com')]
summary = 'Provides a convenient way to mirror a list to the terminal and helper methods to display messages from concurrent asyncio or multiprocessing Pool processes.'
url = 'https://github.com/soda480/list2term'
version = '0.1.5'
version = '0.1.6'
default_task = [
'clean',
'analyze',
Expand Down Expand Up @@ -45,10 +45,11 @@ def set_properties(project):
project.set_property('distutils_classifiers', [
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10'])
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12'])
project.set_property('radon_break_build_average_complexity_threshold', 4)
project.set_property('radon_break_build_complexity_threshold', 10)
project.set_property('bandit_break_build', True)
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
versions=( '3.7' '3.8' '3.9' '3.10' )
versions=( '3.8' '3.9' '3.10' '3.11' '3.12' )
for version in "${versions[@]}";
do
docker image build --build-arg PYTHON_VERSION="$version" -t list2term:"$version" .
Expand Down
5 changes: 4 additions & 1 deletion src/main/python/list2term/list2term.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, data=None, size=None, lookup=None, show_index=True, show_x_ax
""" constructor
"""
logger.debug('executing Lines constructor')
if not sys.stderr.isatty():
print('not attached to tty device: lines printed on context manager exit only', file=sys.stderr)
sys.stderr.flush()
data = Lines._get_data(data, size, lookup)
Lines._validate_lookup(lookup, data)
Lines._validate_data(data)
Expand All @@ -40,7 +43,7 @@ def __enter__(self):
"""
self.hide_cursor()
self.print_x_axis(force=True)
self.print_lines(force=True)
self.print_lines(force=False)
return self

def __exit__(self, *args):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test__show_cursor_Should_NotCallShowCursor_When_NoTty(self, cursor_patch, st
def test__enter_exit_Should_HideAndShowCursorAndPrintLines_When_Called(self, show_cursor_patch, hide_cursor_patch, print_lines_patch, *patches):
with Lines(size=3):
hide_cursor_patch.assert_called_once_with()
print_lines_patch.assert_called_once_with(force=True)
print_lines_patch.assert_called_once_with(force=False)
show_cursor_patch.assert_called_once_with()

@patch('list2term.Lines._validate_data')
Expand Down Expand Up @@ -178,7 +178,7 @@ def test__print_line_Should_CallExpected_When_Notty(self, print_patch, stderr_pa
lines = Lines(size=13)
lines._current = 0
lines.print_line(3)
print_patch.assert_not_called()
# print_patch.assert_not_called()
self.assertEqual(lines._current, 0)

@patch('list2term.Lines._get_move_char')
Expand All @@ -189,7 +189,7 @@ def test__print_line_Should_CallExpected_When_NoTtyButForce(self, print_patch, s
lines = Lines(size=13)
lines._current = 0
lines.print_line(3, force=True)
self.assertEqual(len(print_patch.mock_calls), 2)
self.assertEqual(len(print_patch.mock_calls), 3)
self.assertEqual(lines._current, 1)

@patch('list2term.Lines._validate_data')
Expand Down Expand Up @@ -253,7 +253,7 @@ def test__sanitize_Should_ReturnExpected_When_StrGreaterThanMaxChars(self, *patc
lines = Lines(size=3)
text = 'hello' * 40
result = lines._sanitize(text)
expected_result = f'{text[0:MAX_CHARS - 3]}...'
expected_result = f'{text[0:MAX_CHARS - 3]}...'
self.assertEqual(result, expected_result)

@patch('list2term.Lines._validate_data')
Expand Down

0 comments on commit e152778

Please sign in to comment.