From 4b036fb93675747d0eada3df53fb722b3f02ad99 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 14 Oct 2020 17:09:05 +0100 Subject: [PATCH 1/2] Enable isort --- .pre-commit-config.yaml | 4 ++++ ansi2html/converter.py | 8 ++++---- pyproject.toml | 6 ++++++ tests/test_ansi2html.py | 26 +++++++++++++------------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a49e3a7..99469da 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,9 @@ --- repos: + - repo: https://github.com/PyCQA/isort + rev: 5.6.4 + hooks: + - id: isort - repo: https://github.com/python/black.git rev: 20.8b1 hooks: diff --git a/ansi2html/converter.py b/ansi2html/converter.py index 0982144..4d8ff5b 100644 --- a/ansi2html/converter.py +++ b/ansi2html/converter.py @@ -20,19 +20,19 @@ # along with this program. If not, see # . +import io +import optparse import re import sys -import optparse + import pkg_resources -import io try: from collections import OrderedDict except ImportError: from ordereddict import OrderedDict -from ansi2html.style import get_styles, SCHEME - +from ansi2html.style import SCHEME, get_styles ANSI_FULL_RESET = 0 ANSI_INTENSITY_INCREASED = 1 diff --git a/pyproject.toml b/pyproject.toml index 490c397..8ad8ada 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,3 +6,9 @@ requires = [ "wheel", ] build-backend = "setuptools.build_meta" + +[tool.isort] +profile = "black" + +[tool.setuptools_scm] +local_scheme = "no-local-version" diff --git a/tests/test_ansi2html.py b/tests/test_ansi2html.py index daf18b4..902af7d 100644 --- a/tests/test_ansi2html.py +++ b/tests/test_ansi2html.py @@ -21,28 +21,28 @@ # along with this program. If not, see # . +import textwrap +import unittest +from io import StringIO from os.path import abspath, dirname, join + +from mock import patch + from ansi2html import Ansi2HTMLConverter from ansi2html.converter import ( - main, - ANSI_VISIBILITY_ON, - ANSI_VISIBILITY_OFF, - ANSI_BLINK_SLOW, ANSI_BLINK_FAST, ANSI_BLINK_OFF, - ANSI_NEGATIVE_ON, - ANSI_NEGATIVE_OFF, + ANSI_BLINK_SLOW, ANSI_INTENSITY_INCREASED, - ANSI_INTENSITY_REDUCED, ANSI_INTENSITY_NORMAL, + ANSI_INTENSITY_REDUCED, + ANSI_NEGATIVE_OFF, + ANSI_NEGATIVE_ON, + ANSI_VISIBILITY_OFF, + ANSI_VISIBILITY_ON, + main, ) from ansi2html.util import read_to_unicode -from io import StringIO - -from mock import patch - -import unittest -import textwrap _here = dirname(abspath(__file__)) From 101e89a1dfa0d0b131fa20cc2db9ad5a1356ce35 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 14 Oct 2020 17:26:48 +0100 Subject: [PATCH 2/2] Enable flake8 and pylint --- .pre-commit-config.yaml | 17 +++++++++++++++++ .pylintrc | 29 +++++++++++++++++++++++++++++ ansi2html/converter.py | 40 +++++++++++++++++++--------------------- ansi2html/style.py | 2 +- setup.cfg | 5 +++++ tests/test_ansi2html.py | 3 ++- 6 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 .pylintrc diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99469da..214d3b5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,3 +20,20 @@ repos: - id: check-merge-conflict - id: debug-statements language_version: python3 + - repo: https://gitlab.com/pycqa/flake8.git + rev: 3.8.4 + hooks: + - id: flake8 + additional_dependencies: + - pydocstyle>=5.1.1 + - flake8-absolute-import + - flake8-black>=0.1.1 + - flake8-docstrings>=1.5.0 + language_version: python3 + - repo: https://github.com/pre-commit/mirrors-pylint + rev: v2.6.0 + hooks: + - id: pylint + additional_dependencies: + - ansible-base + - testinfra diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..455ad61 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,29 @@ +[MESSAGES CONTROL] + +disable = + # TODO(ssbarnea): remove temporary skips adding during initial adoption: + attribute-defined-outside-init, + consider-using-dict-comprehension, + consider-using-enumerate, + deprecated-module, + import-error, + invalid-name, + line-too-long, + missing-class-docstring, + missing-function-docstring, + missing-module-docstring, + no-self-use, + redefined-builtin, + redefined-outer-name, + too-few-public-methods, + too-many-arguments, + too-many-branches, + too-many-instance-attributes, + too-many-locals, + too-many-public-methods, + too-many-statements, + unused-argument, + unused-variable, + +[REPORTS] +output-format = colorized diff --git a/ansi2html/converter.py b/ansi2html/converter.py index 4d8ff5b..5f75796 100644 --- a/ansi2html/converter.py +++ b/ansi2html/converter.py @@ -114,7 +114,7 @@ """ -class _State(object): +class _State: def __init__(self): self.reset() @@ -228,8 +228,7 @@ def linkify(line, latex_mode): ) if latex_mode: return url_matcher.sub(r"\\url{\1}", line) - else: - return url_matcher.sub(r'\1', line) + return url_matcher.sub(r'\1', line) def map_vt100_box_code(char): @@ -243,11 +242,11 @@ def _needs_extra_newline(text): return True -class CursorMoveUp(object): +class CursorMoveUp: pass -class Ansi2HTMLConverter(object): +class Ansi2HTMLConverter: """Convert Ansi color codes to CSS+HTML Example: @@ -508,24 +507,23 @@ def convert(self, ansi, full=True, ensure_trailing_newline=False): attrs = self.prepare(ansi, ensure_trailing_newline=ensure_trailing_newline) if not full: return attrs["body"] + if self.latex: + _template = _latex_template else: - if self.latex: - _template = _latex_template - else: - _template = _html_template - all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme) - backgrounds = all_styles[:6] - used_styles = filter( - lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles - ) + _template = _html_template + all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme) + backgrounds = all_styles[:6] + used_styles = filter( + lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles + ) - return _template % { - "style": "\n".join(list(map(str, backgrounds + list(used_styles)))), - "title": self.title, - "font_size": self.font_size, - "content": attrs["body"], - "output_encoding": self.output_encoding, - } + return _template % { + "style": "\n".join(list(map(str, backgrounds + list(used_styles)))), + "title": self.title, + "font_size": self.font_size, + "content": attrs["body"], + "output_encoding": self.output_encoding, + } def produce_headers(self): return '\n' % { diff --git a/ansi2html/style.py b/ansi2html/style.py index 74ffc11..d8603f9 100644 --- a/ansi2html/style.py +++ b/ansi2html/style.py @@ -17,7 +17,7 @@ # . -class Rule(object): +class Rule: def __init__(self, klass, **kw): self.klass = klass diff --git a/setup.cfg b/setup.cfg index 36cf482..c859710 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,3 +41,8 @@ setup_requires = [options.packages.find] where = . + +[flake8] +format = pylint +# E203: https://github.com/python/black/issues/315 +ignore = E741,W503,W504,H,E501,E203,D diff --git a/tests/test_ansi2html.py b/tests/test_ansi2html.py index 902af7d..7d9d25f 100644 --- a/tests/test_ansi2html.py +++ b/tests/test_ansi2html.py @@ -92,7 +92,8 @@ def test_conversion_as_command(self, mock_stdout, mock_argv): with open(join(_here, "ansicolor.html"), "rb") as output: expected_data = "".join(read_to_unicode(output)) - f = lambda: StringIO(test_data) + def f(): + return StringIO(test_data) with patch("sys.stdin", new_callable=f): main()