Skip to content

Commit 2743534

Browse files
authored
Merge pull request #6 from python-tableformatter/unit_tests
Added set_color_library class method to TableColors
2 parents d32d4e9 + d02de19 commit 2743534

File tree

9 files changed

+549
-60
lines changed

9 files changed

+549
-60
lines changed

.appveyor.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
install:
2+
- python -m pip install tox
3+
4+
build: off
5+
6+
test_script:
7+
- python -m tox -e py35-win,py36-win

.coveragerc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# .coveragerc to control coverage.py
2+
[run]
3+
# Source
4+
source = tableformatter.py
5+
# (boolean, default False): whether to measure branch coverage in addition to statement coverage.
6+
branch = False
7+
8+
9+
[report]
10+
# A list of regular expressions. Any line that matches one of these regexes is excluded from being reported as missing
11+
exclude_lines =
12+
# Have to re-enable the standard pragma
13+
pragma: no cover
14+
15+
# Don't complain if non-runnable code isn't run:
16+
if __name__ == .__main__.:
17+
18+
# (integer): the number of digits after the decimal point to display for reported coverage percentages.
19+
precision = 1
20+
21+
22+
[html]
23+
# (string, default “htmlcov”): where to write the HTML report files.
24+
directory = htmlcov

.travis.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
language: python
2+
3+
sudo: false # false enables container-based build for fast boot times on Linux
4+
5+
matrix:
6+
include:
7+
- os: linux
8+
python: 3.4
9+
env: TOXENV=py34
10+
- os: linux
11+
python: 3.5
12+
env: TOXENV=py35
13+
- os: linux
14+
python: 3.6
15+
env: TOXENV=py36
16+
- os: linux
17+
python: 3.7-dev
18+
env: TOXENV=py37
19+
# # Warning: Don't try to use code coverage analysis with pypy as it is insanely slow
20+
# - os: linux
21+
# python: pypy3
22+
# env: TOXENV=pypy3
23+
# # Latest Python 3.x from Homebrew
24+
# - os: osx
25+
# language: generic
26+
# env:
27+
# - TOXENV=py36
28+
# - BREW_INSTALL=python3
29+
30+
install:
31+
- pip install tox
32+
# - |
33+
# if [[ $TRAVIS_OS_NAME == 'osx' ]]; then
34+
# if [[ -n "$BREW_INSTALL" ]]; then
35+
# brew update
36+
# brew install "$BREW_INSTALL"
37+
# fi
38+
# fi
39+
# pip install tox
40+
41+
script:
42+
- tox

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ It converts your data into a string form suitable for pretty-printing as a table
55
for developers to display tabular data in an aesthetically pleasing fashion. It provides a simple public API, but allows
66
fine-grained control over almost every aspect of how the data is formatted.
77

8+
Many other modules for formatting tabular data require the developer to create a transition layer to convert their
9+
objects/data into a structure the formatter can consume. One relatively novel aspect of tableformatter is the ability to directly
10+
receive arbitrary Python objects.
11+
812
Main Features
913
-------------
1014
- Easy to display simple tables with just one function call when you don't need the fine-grained control
@@ -25,11 +29,12 @@ pip install tableformatter
2529

2630
Dependencies
2731
------------
28-
``tableformatter`` does not have any required dependencies.
32+
``tableformatter`` depends on the [wcwidth](https://github.com/jquast/wcwidth) module for measuring the width of
33+
unicode strings rendered to a terminal.
2934

30-
However, if you wish to use the provided optional support for color, then at least one of the following two modules must be installed:
31-
* [colorama](https://github.com/tartley/colorama)
32-
* [colored](https://github.com/dslackw/colored)
35+
If you wish to use the optional support for color, then at least one of the following two modules must be installed:
36+
* [colorama](https://github.com/tartley/colorama) - simple cross-platform colored terminal text with about 16 colors
37+
* [colored](https://github.com/dslackw/colored) - library for color in terminal with 256 colors, macOS and Linux only
3338

3439
If both ``colorama`` and ``colored`` are installed, then ``colored`` will take precedence.
3540

setup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@
3030
Topic :: Software Development :: Libraries :: Python Modules
3131
""".splitlines())))
3232

33+
INSTALL_REQUIRES = ['wcwidth']
34+
3335
EXTRAS_REQUIRE = {
34-
# POSIX OSes also require wcwidth for correctly estimating the displayed width of unicode chars
35-
":sys_platform!='win32'": ['wcwidth'],
36-
# Python 3.4 and earlier the typing module backport for optional type hinting support
36+
# Python 3.4 and earlier require the typing module backport for type hinting support
3737
":python_version<'3.5'": ['typing'],
3838
# development only dependencies - install with 'pip install -e .[dev]'
3939
'dev': [
40-
'pytest', 'pytest-cov', 'tox', 'pylint', 'sphinx', 'sphinx-rtd-theme',
41-
'sphinx-autobuild', 'invoke', 'twine>=1.11',
40+
'pytest', 'pytest-cov', 'tox', 'invoke', 'twine>=1.11', 'setuptools>=39.1', 'wheel>=0.31'
4241
]
4342
}
4443

@@ -56,5 +55,6 @@
5655
py_modules = ['tableformatter'],
5756
keywords='table tabular formatter',
5857
python_requires='>=3.4',
58+
install_requires=INSTALL_REQUIRES,
5959
extras_require=EXTRAS_REQUIRE,
6060
)

tableformatter.py

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,22 @@
1414
# Python 3.6+ should have Collection in the typing module
1515
from typing import Collection
1616
except ImportError:
17-
from typing import Container, Sized
18-
import sys
19-
20-
# Unfortunately need slightly different solutions for Python 3.4 vs 3.5
21-
if sys.version_info < (3, 5):
22-
# Python 3.4
23-
# noinspection PyAbstractClass
24-
class Collection(Container, Sized, Iterable):
25-
"""hack to enable Collection typing"""
26-
__slots__ = ()
27-
28-
# noinspection PyPep8Naming
29-
@classmethod
30-
def __subclasshook__(cls, C):
31-
if cls is Collection:
32-
if any("__len__" in B.__dict__ for B in C.__mro__) and \
33-
any("__iter__" in B.__dict__ for B in C.__mro__) and \
34-
any("__contains__" in B.__dict__ for B in C.__mro__):
35-
return True
36-
return NotImplemented
37-
else:
38-
# Python 3.5
39-
# noinspection PyAbstractClass
40-
from typing import Generic, TypeVar
41-
class Collection(Generic[TypeVar('T_co', covariant=True)], Container, Sized, Iterable):
42-
"""hack to enable Collection typing"""
43-
__slots__ = ()
44-
45-
# noinspection PyPep8Naming
46-
@classmethod
47-
def __subclasshook__(cls, C):
48-
if cls is Collection:
49-
if any("__len__" in B.__dict__ for B in C.__mro__) and \
50-
any("__iter__" in B.__dict__ for B in C.__mro__) and \
51-
any("__contains__" in B.__dict__ for B in C.__mro__):
52-
return True
53-
return NotImplemented
17+
from typing import Container, Generic, Sized, TypeVar
18+
# Python 3.5
19+
# noinspection PyAbstractClass
20+
class Collection(Generic[TypeVar('T_co', covariant=True)], Container, Sized, Iterable):
21+
"""hack to enable Collection typing"""
22+
__slots__ = ()
23+
24+
# noinspection PyPep8Naming
25+
@classmethod
26+
def __subclasshook__(cls, C):
27+
if cls is Collection:
28+
if any("__len__" in B.__dict__ for B in C.__mro__) and \
29+
any("__iter__" in B.__dict__ for B in C.__mro__) and \
30+
any("__contains__" in B.__dict__ for B in C.__mro__):
31+
return True
32+
return NotImplemented
5433

5534

5635
ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m')
@@ -303,6 +282,44 @@ class TableColors(object):
303282
BOLD = ''
304283
RESET = ''
305284

285+
@classmethod
286+
def set_color_library(cls, library_name: str) -> None:
287+
"""Manually override the color library being used."""
288+
if library_name == 'colored':
289+
from colored import fg, bg, attr
290+
291+
cls.TEXT_COLOR_WHITE = fg('white')
292+
cls.TEXT_COLOR_YELLOW = fg(226)
293+
cls.TEXT_COLOR_RED = fg(196)
294+
cls.TEXT_COLOR_GREEN = fg(119)
295+
cls.TEXT_COLOR_BLUE = fg(27)
296+
cls.BG_COLOR_ROW = bg(234)
297+
cls.BG_RESET = bg(0)
298+
cls.BOLD = attr('bold')
299+
cls.RESET = attr('reset')
300+
elif library_name == 'colorama':
301+
from colorama import Fore, Back, Style
302+
303+
cls.TEXT_COLOR_WHITE = Fore.WHITE
304+
cls.TEXT_COLOR_YELLOW = Fore.LIGHTYELLOW_EX
305+
cls.TEXT_COLOR_RED = Fore.LIGHTRED_EX
306+
cls.TEXT_COLOR_GREEN = Fore.LIGHTGREEN_EX
307+
cls.TEXT_COLOR_BLUE = Fore.LIGHTBLUE_EX
308+
cls.BG_COLOR_ROW = Back.LIGHTBLACK_EX
309+
cls.BG_RESET = Back.RESET
310+
cls.BOLD = Style.BRIGHT
311+
cls.RESET = Fore.RESET + Back.RESET
312+
else:
313+
cls.TEXT_COLOR_WHITE = ''
314+
cls.TEXT_COLOR_YELLOW = ''
315+
cls.TEXT_COLOR_RED = ''
316+
cls.TEXT_COLOR_GREEN = ''
317+
cls.TEXT_COLOR_BLUE = ''
318+
cls.BG_COLOR_ROW = ''
319+
cls.BG_RESET = ''
320+
cls.BOLD = ''
321+
cls.RESET = ''
322+
306323

307324
class ColumnAlignment(enum.Enum):
308325
"""Column alignment"""

0 commit comments

Comments
 (0)