From 0f556b865d9b62e016d6a576238568d90ecdd614 Mon Sep 17 00:00:00 2001 From: Georgy Frolov Date: Sun, 21 Feb 2021 16:35:32 +0300 Subject: [PATCH 1/2] Cache pygments formatter to speedup styling (Python 2.7 backport) --- cli_helpers/utils.py | 21 ++++++++++++++++++++- setup.py | 5 ++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cli_helpers/utils.py b/cli_helpers/utils.py index d25a011..fb8bdb6 100644 --- a/cli_helpers/utils.py +++ b/cli_helpers/utils.py @@ -4,7 +4,12 @@ import binascii import re -from cli_helpers.compat import binary_type, text_type +from cli_helpers.compat import binary_type, text_type, PY2 + +if PY2: + from repoze.lru import lru_cache +else: + from functools import lru_cache def bytes_to_string(b): @@ -68,3 +73,17 @@ def replace(s, replace): for r in replace: s = s.replace(*r) return s + + +@lru_cache(maxsize=128) +def _get_formatter(style): + return Terminal256Formatter(style=style) + + +def style_field(token, field, style): + """Get the styled text for a *field* using *token* type.""" + formatter = _get_formatter(style) + s = StringIO() + formatter.format(((token, field),), s) + return s.getvalue() + diff --git a/setup.py b/setup.py index bc98da5..b7a2887 100755 --- a/setup.py +++ b/setup.py @@ -24,7 +24,10 @@ def open_file(filename): readme = open_file('README.rst') if sys.version_info[0] == 2: - py2_reqs = ['backports.csv >= 1.0.0'] + py2_reqs = [ + 'backports.csv >= 1.0.0', + 'repoze.lru', + ] else: py2_reqs = [] From 40c9538e32f86cf1b34e0c5bb505367f6f258c6c Mon Sep 17 00:00:00 2001 From: Georgy Frolov Date: Sun, 21 Feb 2021 17:19:49 +0300 Subject: [PATCH 2/2] fix check-manifest --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 6a32375..bc35b4a 100644 --- a/tox.ini +++ b/tox.ini @@ -37,7 +37,7 @@ deps = readme_renderer[md] -r{toxinidir}/requirements-dev.txt commands = - check-manifest + check-manifest --ignore .travis/* ./setup.py sdist twine check dist/* ./setup.py check -m -s