The new woob repository is here: https://gitlab.com/woob/woob. This gitlab will be removed soon.

Commit 9c714c91 authored by Vincent Ardisson's avatar Vincent Ardisson Committed by Vincent A

tools/hooks/run-flake8.py: use flake8-import-order plugin

But it's pep8 rule keeps application-imports in the same group as
relative-imports. Changing it is not a matter of configuration but defining
our own style, which is tedious as it would require a setuptools entrypoint
just for that, so we inject manually the style.
parent 20e437bb
......@@ -10,3 +10,4 @@ Sphinx==3.0.3
xunitparser==1.3.3
asttokens
virtualenv
flake8-import-order
......@@ -2,12 +2,39 @@
from pathlib import Path
import runpy
import subprocess
import sys
from types import ModuleType
from flake8.main.cli import main
from flake8_import_order.styles import PEP8
import pkg_resources
mod = runpy.run_path(str(Path(__file__).with_name('checkerlib.py')))
args = mod['parser'].parse_args()
# flake8-import-order's PEP8 style merges app imports and relative imports
class AllSeparateStyle(PEP8):
@staticmethod
def same_section(previous, current):
return current.type == previous.type
# flake8-import-order is almost only configurable through entry_points
# ugly hack to avoid having to create a separate package with entry_point, setup, etc.
style_mod = ModuleType('fake_module_all_separate')
style_mod.AllSeparateStyle = AllSeparateStyle
sys.modules['fake_module_all_separate'] = style_mod
d = pkg_resources.Distribution('/lol.py')
ep = pkg_resources.EntryPoint.parse('fake_module_all_separate = fake_module_all_separate:AllSeparateStyle')
ep.dist = d
d._ep_map = {'flake8_import_order.styles': {'fake_module_all_separate': ep}}
pkg_resources.working_set.add(d, 'fake_module_all_separate')
# E501: Line too long
# Disabled because it doesn't allow exceptions, for example URLs or log
# messages shouldn't be split, less readable or searchable.
......@@ -15,11 +42,9 @@
# Disabling it follows pep8 (see W504).
# E266: Too many leading '#' for block comment
# But it's a nice visual separator sometimes.
args = mod['parser'].parse_args()
execution = subprocess.run([
'flake8', '--ignore=E501,W503,E266',
main([
'--ignore=E501,W503,E266',
'--application-import-names=weboob',
'--import-order-style=fake_module_all_separate',
*map(str, mod['files_to_check'](args)),
])
sys.exit(execution.returncode)
......@@ -10,6 +10,7 @@ die () {
}
$PYTHON3 -c 'import flake8' || die "Please install flake8 (e.g. apt install flake8)"
$PYTHON3 -c 'import flake8_import_order' || die "Please install flake8-import-order (e.g. pip3 install flake8-import-order)"
$PYTHON3 -c 'import bugbear' || die "Please install flake8-bugbear (e.g. pip3 install flake8-bugbear)"
$PYTHON3 -c 'import asttokens' || die "Please install asttokens (e.g. apt install python3-asttokens)"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment