From a20422b737d1f0da6125961044df2b2b946a5b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20Morini=C3=A8re?= Date: Tue, 5 Jul 2022 10:43:13 +0200 Subject: [PATCH] Add black, isort and flake8 for dev --- .gitignore | 1 + Makefile | 10 ++++++++++ chopper/css/extractor.py | 12 +++++------ chopper/html/extractor.py | 5 +++-- chopper/tests.py | 3 +-- requirements.txt | 3 +++ setup.cfg | 22 ++++++++++++++++++++ setup.py | 42 +++++++++++++++++++-------------------- 8 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 setup.cfg diff --git a/.gitignore b/.gitignore index de603e1..e644bcd 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,6 @@ !requirements-dev.txt !requirements-docs.txt !setup.py +!setup.cfg __pycache__/ *.py[cod] diff --git a/Makefile b/Makefile index f5671d9..f3cdefb 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,12 @@ tests: nosetests --with-coverage --cover-package=chopper + +styles: + isort chopper + black . + flake8 --config setup.cfg + +styles.check: + black . --check + isort --ws jurismarches --check-only + flake8 --config setup.cfg diff --git a/chopper/css/extractor.py b/chopper/css/extractor.py index f6e11a4..5258cf1 100644 --- a/chopper/css/extractor.py +++ b/chopper/css/extractor.py @@ -1,15 +1,15 @@ import re -import cssselect from urllib.parse import urljoin -from tinycss.css21 import RuleSet, ImportRule, MediaRule, PageRule + +import cssselect +from tinycss.css21 import ImportRule, MediaRule, PageRule, RuleSet from tinycss.parsing import split_on_comma, strip_whitespace +from ..mixins import TreeBuilderMixin from .parser import CSSParser from .rules import FontFaceRule from .translator import XpathTranslator -from ..mixins import TreeBuilderMixin - class CSSExtractor(TreeBuilderMixin): """ @@ -103,7 +103,7 @@ def _clean_css(self): if cleaned_rule is not None: css_rules.append(cleaned_rule) - except: + except Exception: # On error, assume the rule matched the tree css_rules.append(rule) @@ -165,7 +165,7 @@ def _token_list_matches_tree(self, token_list): return bool( self.tree.xpath( self.xpath_translator.selector_to_xpath(parsed_selector))) - except: + except Exception: # On error, assume the selector matches the tree return True diff --git a/chopper/html/extractor.py b/chopper/html/extractor.py index 58d5189..2f89b79 100644 --- a/chopper/html/extractor.py +++ b/chopper/html/extractor.py @@ -1,9 +1,10 @@ import re -from lxml import html from itertools import chain -from lxml.etree import strip_attributes from urllib.parse import urljoin +from lxml import html +from lxml.etree import strip_attributes + from ..mixins import TreeBuilderMixin diff --git a/chopper/tests.py b/chopper/tests.py index 92b06cb..88160d8 100644 --- a/chopper/tests.py +++ b/chopper/tests.py @@ -3,7 +3,6 @@ from .extractor import Extractor - TEST_HTML = """ @@ -53,7 +52,7 @@ def __init__(self, *args, **kwargs): self.assertIsNone = lambda v: self.assertEqual(v, None) def format_output(self, output): - return ''.join(l.strip() for l in output.splitlines()) + return ''.join(line.strip() for line in output.splitlines()) def test_no_rules(self): """ diff --git a/requirements.txt b/requirements.txt index adec9a7..104a835 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,6 @@ # Dev requirements Sphinx==1.2.2 nose==1.3.3 +black +isort +flake8 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..8d091ca --- /dev/null +++ b/setup.cfg @@ -0,0 +1,22 @@ +[flake8] +max-complexity = 11 +max-line-length = 99 +# ignore = E203,W503,W504 +exclude = + .git, + docs, + **/site-packages/** + .venv +filename = + # complete + **.py +per-file-ignores = + chopper/tests.py:E501 + +[isort] +combine_as_imports = true +multi_line_output = 3 +include_trailing_comma = true +sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER +line_length=99 +profile=black diff --git a/setup.py b/setup.py index f1a58e6..f5ed94a 100644 --- a/setup.py +++ b/setup.py @@ -4,37 +4,37 @@ from chopper import __version__ -with open('README.rst', 'r') as f: +with open("README.rst", "r") as f: long_description = f.read() setup( - name='chopper', + name="chopper", version=__version__, description="Lib to extract html elements by preserving ancestors and cleaning CSS", long_description=long_description, - author='Jurismarches', - author_email='contact@jurismarches.com', - url='https://github.com/jurismarches/chopper', + author="Jurismarches", + author_email="contact@jurismarches.com", + url="https://github.com/jurismarches/chopper", packages=[ - 'chopper', - 'chopper.css', - 'chopper.html', + "chopper", + "chopper.css", + "chopper.html", ], install_requires=[ - 'cssselect==1.1.0', - 'tinycss==0.4', - 'lxml==4.9.1', + "cssselect==1.1.0", + "tinycss==0.4", + "lxml==4.9.1", ], classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'License :: OSI Approved :: MIT License', - 'Intended Audience :: Developers', - 'Programming Language :: Python', - 'Programming Language :: Python :: 3.6' - 'Programming Language :: Python :: 3.7' - 'Programming Language :: Python :: 3.8' - 'Programming Language :: Python :: 3.9' - 'Programming Language :: Python :: 3.10' + "Development Status :: 5 - Production/Stable", + "License :: OSI Approved :: MIT License", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3.6" + "Programming Language :: Python :: 3.7" + "Programming Language :: Python :: 3.8" + "Programming Language :: Python :: 3.9" + "Programming Language :: Python :: 3.10", ], - test_suite='chopper.tests' + test_suite="chopper.tests", )