From ae33bac9d7f5cf35c9fe34bf4c855ef9eb1204bd Mon Sep 17 00:00:00 2001 From: Csaba Kertesz Date: Tue, 7 Aug 2018 09:04:16 +0300 Subject: [PATCH 01/17] Update the PPA information --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 187f828..7688266 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,9 @@ or sudo easy_install colout -Ubuntu 13.04's ppa +Ubuntu PPA for 16.04 (Xenial)/18.04 (Bionic) - sudo add-apt-repository ppa:ciici123/colout + sudo add-apt-repository ppa:csaba-kertesz/random sudo apt-get update sudo apt-get/aptitude install colout From b62b57512d4b344728cb53ebc0ecf0d2c1cf81bb Mon Sep 17 00:00:00 2001 From: nojhan Date: Tue, 14 Aug 2018 14:31:55 +0200 Subject: [PATCH 02/17] (almost) silently handle (weird) errors from pygments --- colout/colout.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index 656adc7..29ca071 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -306,14 +306,17 @@ def load_lexers(): from pygments.lexers import get_all_lexers except ImportError: logging.warning("the pygments module has not been found, syntax coloring is not available") - pass else: - for lexer in get_all_lexers(): - try: - lexers.append(lexer[1][0]) - except IndexError: - logging.warning("cannot load lexer: %s" % lexer[1][0]) - pass + try: + for lexer in get_all_lexers(): + try: + lexers.append(lexer[1][0]) + except IndexError: + logging.warning("cannot load lexer: %s" % lexer[1][0]) + pass + except: + logging.warning("error while executing the pygment module, syntax coloring is not available") + lexers.sort() logging.debug("loaded %i lexers: %s" % (len(lexers), ", ".join(lexers))) From a261a2dfa8e644f60d214eb87904a8e3394f637f Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 17:30:49 +0100 Subject: [PATCH 03/17] Use version from SCM --- .gitignore | 1 + setup.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9839280..56a1c24 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/ dist/ colout.egg-info/ +.eggs/ diff --git a/setup.py b/setup.py index a79d0cf..a693306 100644 --- a/setup.py +++ b/setup.py @@ -16,9 +16,11 @@ packages = ['colout'] requires = ['argparse; python_version < "2.7"', 'pygments', 'babel'] +setup_requires = ['setuptools_scm'] + setup( name='colout', - version='0.6', + use_scm_version=True, description='Color Up Arbitrary Command Output.', long_description=open('README.md').read(), author='nojhan', @@ -28,6 +30,7 @@ setup( package_data={'': ['LICENSE', 'README.md']}, package_dir={'colout': 'colout'}, scripts=['bin/colout'], + setup_requires=setup_requires, include_package_data=True, install_requires=requires, license='GPLv3', From cfcd61fd2f470e6a220a9c5e97992df068c737ca Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 17:52:58 +0100 Subject: [PATCH 04/17] Add Travis smoke test --- .travis.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0ddc514 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: python +python: + - "2.6" + - "2.7" + - "3.2" + - "3.3" + - "3.4" + - "3.5" + - "3.6" + - "3.7-dev" + - "pypy" + - "pypy3" +install: + - pip install . +script: colout --help From 7d848d09e8b5152c804e325b70099274dc88787f Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 18:26:17 +0100 Subject: [PATCH 05/17] Add package classifiers --- setup.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/setup.py b/setup.py index a693306..a96f95c 100644 --- a/setup.py +++ b/setup.py @@ -18,9 +18,22 @@ requires = ['argparse; python_version < "2.7"', 'pygments', 'babel'] setup_requires = ['setuptools_scm'] +classifiers = """ +Environment :: Console +License :: OSI Approved :: GNU General Public License v3 (GPLv3) +Programming Language :: Python :: 2 +Programming Language :: Python :: 2.7 +Programming Language :: Python :: 3 +Programming Language :: Python :: 3.4 +Programming Language :: Python :: 3.5 +Programming Language :: Python :: 3.6 +Programming Language :: Python :: 3.7 +""".strip().split('\n') + setup( name='colout', use_scm_version=True, + classifiers=classifiers, description='Color Up Arbitrary Command Output.', long_description=open('README.md').read(), author='nojhan', From b0d41ab172bb4e13ad79cca5a43ac3e7107e92d7 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 18:39:23 +0100 Subject: [PATCH 06/17] Remove argparse requirement as code not 2.6 compatible --- .travis.yml | 1 - setup.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0ddc514..7d737cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: python python: - - "2.6" - "2.7" - "3.2" - "3.3" diff --git a/setup.py b/setup.py index a96f95c..f9fcbac 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ if sys.argv[-1] == 'publish': packages = ['colout'] -requires = ['argparse; python_version < "2.7"', 'pygments', 'babel'] +requires = ['pygments', 'babel'] setup_requires = ['setuptools_scm'] From e49b42db45a810c41524f9508d3a7bba0525832c Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 18:43:50 +0100 Subject: [PATCH 07/17] Add 3.3 classifier --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index f9fcbac..fdd69d2 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ License :: OSI Approved :: GNU General Public License v3 (GPLv3) Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 +Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 From 513552d5be29888f87dac2d0bf58d65465a877f4 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 18:44:11 +0100 Subject: [PATCH 08/17] Remove 3.2 smoke test as incompatible syntax --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7d737cb..4362143 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: python python: - "2.7" - - "3.2" - "3.3" - "3.4" - "3.5" From 7e3502d66757aa577b042e0e51d42307bec9bf38 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 18:50:39 +0100 Subject: [PATCH 09/17] Add another smoke test --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4362143..bd53a0b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,6 @@ python: - "pypy3" install: - pip install . -script: colout --help +script: + - colout --help + - echo heyoo | colout hey yellow From a7a5f6adb8ca3e587d5c3b812e75f68a5a6d3e42 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 19:13:43 +0100 Subject: [PATCH 10/17] Publish universal wheel --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fdd69d2..890b887 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ except ImportError: from distutils.core import setup if sys.argv[-1] == 'publish': - os.system('python3 setup.py sdist upload') + os.system('python setup.py bdist_wheel --universal upload') sys.exit() packages = ['colout'] From f9ec8cba39c7f52deaf40b3b8d036c6f2aeddf6e Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 20:18:21 +0100 Subject: [PATCH 11/17] Replace shell script with console scripts --- bin/colout | 11 ----------- colout/colout.py | 7 +++++-- setup.py | 4 +++- 3 files changed, 8 insertions(+), 14 deletions(-) delete mode 100644 bin/colout diff --git a/bin/colout b/bin/colout deleted file mode 100644 index 7702d17..0000000 --- a/bin/colout +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -# Copyright (c) 2013 Martin Ueding - -# Small launcher script for the main module. - -# Licence: GPL 3 - -set -e -set -u - -python3 -m colout.colout "$@" diff --git a/colout/colout.py b/colout/colout.py index 29ca071..052d77e 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -927,8 +927,8 @@ def write_all( as_all, stream_in, stream_out, function, *args ): map_write( stream_in, stream_out, function, *args ) -if __name__ == "__main__": - +def main(): + global context error_codes = {"UnknownColor":1, "DuplicatedPalette":2, "MixedModes":3} usage = "A regular expression based formatter that color up an arbitrary text stream." @@ -1108,3 +1108,6 @@ if __name__ == "__main__": + " Check the following 'color:mode' pairs: %s." % e ) sys.exit( error_codes["MixedModes"] ) + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 890b887..d844893 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,9 @@ setup( use_scm_version=True, classifiers=classifiers, description='Color Up Arbitrary Command Output.', + entry_points={ + 'console_scripts': ['colout=colout.colout:main'], + }, long_description=open('README.md').read(), author='nojhan', author_email='nojhan@nojhan.net', @@ -43,7 +46,6 @@ setup( packages=packages, package_data={'': ['LICENSE', 'README.md']}, package_dir={'colout': 'colout'}, - scripts=['bin/colout'], setup_requires=setup_requires, include_package_data=True, install_requires=requires, From 177a37dd3768e286c36f36e4222c9ef1282ee74b Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 20:49:01 +0100 Subject: [PATCH 12/17] Update documentation --- README.md | 59 ++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 7688266..5fb788e 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ colout(1) -- Color Up Arbitrary Command Output ============================================== -## SYNOPSIS +## Synopsis `colout` [-h] [-r RESOURCE] `colout` [-g] [-c] [-l min,max] [-a] [-t] [-T DIR] [-P DIR] [-d COLORMAP] [-s] [-e CHAR] [-E CHAR] [--debug] PATTERN [COLOR(S) [STYLE(S)]] -## DESCRIPTION +## Description `colout` read lines of text stream on the standard input and output characters matching a given regular expression *PATTERN* in given *COLOR* and *STYLE*. @@ -77,37 +77,28 @@ use the `-r` switch (see below). `colout` is released under the GNU Public License v3. -## INSTALLATION +## Installation - sudo python3 setup.py install +The reccomended method is using [pipsi](https://github.com/mitsuhiko/pipsi) +``` +pipsi install colout +``` -and then soft link `/usr/local/bin/colout` to your colout.py under your installation -directory, which is usually something like +Another method is to use pip to install the package for the local user: - /usr/local/lib/python3/dist-packages/colout-0.1-py3.egg/colout/colout.py +``` +pip install --user colout +``` +There is also a PPA for Ubuntu 16.04 (Xenial)/18.04 (Bionic) -## OTHER INSTALLATION METHOD +``` +sudo add-apt-repository ppa:csaba-kertesz/random +sudo apt-get update +sudo apt-get/aptitude install colout +``` -Pypi (the Python Package Index) - - sudo pip install colout - -or - - sudo easy_install colout - -Ubuntu PPA for 16.04 (Xenial)/18.04 (Bionic) - - sudo add-apt-repository ppa:csaba-kertesz/random - sudo apt-get update - sudo apt-get/aptitude install colout - -Gentoo - - sudo emerge colout - -## OPTIONS +## Options * `-h`, `--help`: Show a help message and exit @@ -164,7 +155,7 @@ Gentoo Debug mode: print what's going on internally, if you want to check what features are available. -## REGULAR EXPRESSIONS +## Regular expressions A regular expression (or _regex_) is a pattern that describes a set of strings that matches it. @@ -174,15 +165,7 @@ that matches it. special characters that would be recognize by your shell. -## DEPENDENCIES - -Recommended packages: - -* `pygments` for the source code syntax coloring -* `babel` for a locale-aware number parsing - - -## LIMITATIONS +## Limitations Don't use nested groups or colout will duplicate the corresponding input text with each matching colors. @@ -192,7 +175,7 @@ Using a default colormap that is incompatible with the special colormap's mode Color pairs ("foreground.background") work in 8-colors mode for simple coloring, but may fail with `--colormap`. -## EXAMPLES +## Examples ### Simple From b77d79e596c54083c3ad7d25e77291b30d891f3e Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 21:38:23 +0100 Subject: [PATCH 13/17] Mark python version support --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index d844893..0be5ef3 100644 --- a/setup.py +++ b/setup.py @@ -46,6 +46,7 @@ setup( packages=packages, package_data={'': ['LICENSE', 'README.md']}, package_dir={'colout': 'colout'}, + python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*', setup_requires=setup_requires, include_package_data=True, install_requires=requires, From 741665553596ed71dcb0763c4e034ed46c8cba5f Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 21:40:00 +0100 Subject: [PATCH 14/17] Remove references to pygments as optional --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5fb788e..cce451b 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,8 @@ Before interpreting the matched string as a number, colout will remove any character not supposed to be used to write down numbers. This permits to apply this special color on a large group, while interpreting only its numerical part. -If the python3-pygments library is installed, you can use the name of a -syntax-coloring "lexer" as a color (for example: "Cpp", "ruby", "xml+django", etc.). +You can use the name of a syntax-coloring ["lexer"](http://pygments.org/docs/lexers/) +as a color (for example: "Cpp", "ruby", "xml+django", etc.). If GIMP palettes files (\*.gpl) are available, you can also use their names as a colormap (see the `-P` switch below). @@ -68,8 +68,7 @@ When not specified, a *COLOR* defaults to _red_ and a *STYLE* defaults to _bold_ `colout` comes with some predefined themes to rapidly color well-known outputs (see the `-t` switch below). -If the python3-pygments library is available, `colout` can be used as an interface -to it (see also the `-s` switch below). +`colout` can be used as an interface to pygments (see also the `--source` switch below). To have a list of all colors, styles, special colormaps, themes, palettes and lexers, use the `-r` switch (see below). From 3644dc06191844ed1687aefd176d1ddb1c04742e Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 21:54:44 +0100 Subject: [PATCH 15/17] Fix error_codes scope + more informative lexer error --- colout/colout.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index 052d77e..63ef9ac 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -42,6 +42,8 @@ context["styles"] = { "reverse": 7, "conceal": 8 } +error_codes = {"UnknownColor": 1, "DuplicatedPalette": 2, "MixedModes": 3, "UnknownLexer": 4} + # Available color names in 8-colors mode. eight_colors = ["black","red","green","yellow","blue","magenta","cyan","white"] # Given in that order, the ASCII code is the index. @@ -929,8 +931,6 @@ def write_all( as_all, stream_in, stream_out, function, *args ): def main(): global context - error_codes = {"UnknownColor":1, "DuplicatedPalette":2, "MixedModes":3} - usage = "A regular expression based formatter that color up an arbitrary text stream." ##################### @@ -1075,7 +1075,12 @@ def main(): # if pygments elif as_source: logging.debug("asked for lexer: %s" % pattern.lower()) - assert(pattern.lower() in context["lexers"]) + if pattern.lower() not in context["lexers"]: + logging.error("Lexer %r is not one of %s" % ( + pattern, + ", ".join(repr(lexer) for lexer in context["lexers"]), + )) + sys.exit(error_codes["UnknownLexer"]) lexer = get_lexer_by_name(pattern.lower()) # Python => 256 colors, python => 8 colors ask_256 = pattern[0].isupper() From 161777d679add5d92e82140e1a0a78f49a8b92a8 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 22:02:05 +0100 Subject: [PATCH 16/17] Remove references to pygments+babel being optional --- colout/colout.py | 80 ++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 53 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index 63ef9ac..6b5d619 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -20,6 +20,7 @@ import logging import argparse import importlib import functools +import babel.numbers as bn # set the SIGPIPE handler to kill the program instead of # ending in a write error when a broken pipe occurs @@ -292,35 +293,31 @@ def load_lexers(): global context # load available pygments lexers lexers = [] + global get_lexer_by_name + from pygments.lexers import get_lexer_by_name + + global highlight + from pygments import highlight + + global Terminal256Formatter + from pygments.formatters import Terminal256Formatter + + global TerminalFormatter + from pygments.formatters import TerminalFormatter + + from pygments.lexers import get_all_lexers try: - global get_lexer_by_name - from pygments.lexers import get_lexer_by_name + for lexer in get_all_lexers(): + try: + lexers.append(lexer[1][0]) + except IndexError: + logging.warning("cannot load lexer: %s" % lexer[1][0]) + pass + except: + logging.warning("error while executing the pygment module, syntax coloring is not available") - global highlight - from pygments import highlight - - global Terminal256Formatter - from pygments.formatters import Terminal256Formatter - - global TerminalFormatter - from pygments.formatters import TerminalFormatter - - from pygments.lexers import get_all_lexers - except ImportError: - logging.warning("the pygments module has not been found, syntax coloring is not available") - else: - try: - for lexer in get_all_lexers(): - try: - lexers.append(lexer[1][0]) - except IndexError: - logging.warning("cannot load lexer: %s" % lexer[1][0]) - pass - except: - logging.warning("error while executing the pygment module, syntax coloring is not available") - - lexers.sort() - logging.debug("loaded %i lexers: %s" % (len(lexers), ", ".join(lexers))) + lexers.sort() + logging.debug("loaded %i lexers: %s" % (len(lexers), ", ".join(lexers))) context["lexers"] = lexers @@ -411,23 +408,11 @@ def color_scale( name, text ): nb = "".join([i for i in filter(allowed.__contains__, text)]) # interpret as decimal - # First, try with the babel module, if available - # if not, use python itself, - # if thoses fails, try to `eval` the string - # (this allow strings like "1/2+0.9*2") f = None try: - # babel is a specialized module - import babel.numbers as bn - try: - f = float(bn.parse_decimal(nb)) - except bn.NumberFormatError: - pass - except ImportError: - try: - f = float(nb) - except ValueError: - pass + f = float(bn.parse_decimal(nb)) + except bn.NumberFormatError: + pass if f is not None: # normalize with scale if it's a number f = (f - context["scale"][0]) / (context["scale"][1]-context["scale"][0]) @@ -832,11 +817,6 @@ def _args_parse(argv, usage=""): help="A regular expression") pygments_warn=" You can use a language name to activate syntax coloring (see `-r all` for a list)." - try: - import pygments - except ImportError: - pygments_warn=" (WARNING: python3-pygments is not available, \ - install it if you want to be able to use syntax coloring)" parser.add_argument("color", metavar="COLOR", type=str, nargs='?', default="red", @@ -856,12 +836,6 @@ def _args_parse(argv, usage=""): (cycle the colors at each match)") babel_warn=" (numbers will be parsed according to your locale)" - try: - # babel is a specialized module - import babel.numbers - except ImportError: - babel_warn=" (WARNING: python3-babel is not available, install it \ - if you want to be able to parse numbers according to your locale)" parser.add_argument("-l", "--scale", metavar="SCALE", help="When using the 'scale' colormap, parse matches as decimal numbers \ From b8e259add3d596036a6365343c7d680a5b0b8040 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Sat, 4 Aug 2018 22:08:23 +0100 Subject: [PATCH 17/17] Make missing lexer error less verbose --- colout/colout.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index 6b5d619..3cfaa61 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -1050,10 +1050,7 @@ def main(): elif as_source: logging.debug("asked for lexer: %s" % pattern.lower()) if pattern.lower() not in context["lexers"]: - logging.error("Lexer %r is not one of %s" % ( - pattern, - ", ".join(repr(lexer) for lexer in context["lexers"]), - )) + logging.error("Lexer %r is not available. Run with \"--resources all\" to see the options.") sys.exit(error_codes["UnknownLexer"]) lexer = get_lexer_by_name(pattern.lower()) # Python => 256 colors, python => 8 colors