From 1b7c5d26c63dbd088f28ad757570c4d97db0c5a0 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Sat, 13 Jul 2019 21:50:53 +0200 Subject: [PATCH] Document "known-modules" option --- LICENSE.txt | 2 +- README.rst | 23 +++++++++++++++++++++++ src/flake8_requirements/checker.py | 6 +++--- src/flake8_requirements/modules.py | 2 +- test/test_checker.py | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/LICENSE.txt b/LICENSE.txt index d8a7c3a..e5aea70 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License -Copyright (c) 2017-2018 Arkadiusz Bokowy +Copyright (c) 2017-2019 Arkadiusz Bokowy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rst b/README.rst index 5e654a9..c026cb5 100644 --- a/README.rst +++ b/README.rst @@ -37,3 +37,26 @@ You can install, upgrade, or uninstall ``flake8-requirements`` with these comman $ pip install flake8-requirements $ pip install --upgrade flake8-requirements $ pip uninstall flake8-requirements + +Customization +------------- + +For projects with custom (private) dependencies, one can provide mapping between project name and +provided modules. Such a mapping can be set on the command line during the flake8 invocation with +the ``--known-modules`` option or alternatively in the ``[flake8]`` section of the configuration +file, e.g. ``setup.cfg``. The syntax of the custom mapping looks like follows:: + + 1st-project-name:[module1,module2,...],2nd-project-name:[moduleA,moduleB,...],... + +If some local project lacks "name" attribute in the ``setup.py`` file (it is highly discouraged +not to provide the "name" attribute, though), one can omit the project name in the mapping and do +as follows:: + + :[localmodule1,localmodule2,...],1st-local-library:[moduleA,moduleB,...],... + +Real life example:: + + $ cat setup.cfg + [flake8] + max-line-length = 100 + known-modules = my-lib:[mylib.drm,mylib.encryption] diff --git a/src/flake8_requirements/checker.py b/src/flake8_requirements/checker.py index 77a99a5..31babce 100644 --- a/src/flake8_requirements/checker.py +++ b/src/flake8_requirements/checker.py @@ -14,10 +14,10 @@ from .modules import STDLIB_PY3 # NOTE: Changing this number will alter package version as well. -__version__ = "1.1.1" +__version__ = "1.1.2" __license__ = "MIT" -LOG = getLogger('flake8.plugin.requires') +LOG = getLogger('flake8.plugin.requirements') ERRORS = { 'I900': "I900 '{pkg}' not listed as a requirement", @@ -246,7 +246,7 @@ def visit_Call(self, node): class Flake8Checker(object): """Package requirements checker.""" - name = "flake8-requires" + name = "flake8-requirements" version = __version__ # User defined project->modules mapping. diff --git a/src/flake8_requirements/modules.py b/src/flake8_requirements/modules.py index 1121349..1de4276 100644 --- a/src/flake8_requirements/modules.py +++ b/src/flake8_requirements/modules.py @@ -492,7 +492,7 @@ # or the name of the module is different than the project name itself. KNOWN_3RD_PARTIES = { "awesome_slugify": ["slugify"], - "beautifulsoup4": ["bs4"] + "beautifulsoup4": ["bs4"], "cx_oracle": ["cx_Oracle"], "enum34": ["enum"], "factory_boy": ["factory"], diff --git a/test/test_checker.py b/test/test_checker.py index ac317f5..e67f183 100644 --- a/test/test_checker.py +++ b/test/test_checker.py @@ -102,6 +102,22 @@ def test_relative(self): errors = check("from ..local import local") self.assertEqual(len(errors), 0) + def test_custom_mapping_parser(self): + class Flake8Options: + known_modules = ":[pydrmcodec],mylib:[mylib.drm,mylib.ex]" + Flake8Checker.parse_options(Flake8Options) + self.assertEqual( + Flake8Checker.known_modules, + {"": ["pydrmcodec"], "mylib": ["mylib.drm", "mylib.ex"]}, + ) + + def test_custom_mapping(self): + class Flake8Options: + known_modules = "flake8-requires:[flake8req]" + Flake8Checker.parse_options(Flake8Options) + errors = check("from flake8req import mymodule") + self.assertEqual(len(errors), 0) + def test_setup_py(self): errors = check("from setuptools import setup", "setup.py") self.assertEqual(len(errors), 0)