diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2020-04-26 13:29:46 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2020-04-26 15:18:01 +0200 |
commit | 8c13c4dc5d9d2a3aa712f88c2b2981e348dda70b (patch) | |
tree | b2bac6a524decdc0baa4c3d8cc58a837e29ce72b | |
parent | a1ba9f06c027a31df218233abc710f2c8bf3251e (diff) | |
download | pylint-git-8c13c4dc5d9d2a3aa712f88c2b2981e348dda70b.tar.gz |
[lint package refactor] Create a file for the utility functions
-rw-r--r-- | pylint/lint/__init__.py | 78 | ||||
-rw-r--r-- | pylint/lint/utils.py | 73 |
2 files changed, 79 insertions, 72 deletions
diff --git a/pylint/lint/__init__.py b/pylint/lint/__init__.py index b9bb104b1..e7001cdc6 100644 --- a/pylint/lint/__init__.py +++ b/pylint/lint/__init__.py @@ -89,6 +89,12 @@ from pylint.lint.report_functions import ( report_messages_stats, report_total_messages_stats, ) +from pylint.lint.utils import ( + ArgumentPreprocessingError, + _patch_sys_path, + fix_import_path, + preprocess_options, +) from pylint.message import Message, MessageDefinitionStore, MessagesHandlerMixIn from pylint.reporters.ureports import nodes as report_nodes from pylint.utils import ASTWalker, FileState, utils @@ -1356,78 +1362,6 @@ def _worker_check_single_file(file_item): ) -# utilities ################################################################### - - -class ArgumentPreprocessingError(Exception): - """Raised if an error occurs during argument preprocessing.""" - - -def preprocess_options(args, search_for): - """look for some options (keys of <search_for>) which have to be processed - before others - - values of <search_for> are callback functions to call when the option is - found - """ - i = 0 - while i < len(args): - arg = args[i] - if arg.startswith("--"): - try: - option, val = arg[2:].split("=", 1) - except ValueError: - option, val = arg[2:], None - try: - cb, takearg = search_for[option] - except KeyError: - i += 1 - else: - del args[i] - if takearg and val is None: - if i >= len(args) or args[i].startswith("-"): - msg = "Option %s expects a value" % option - raise ArgumentPreprocessingError(msg) - val = args[i] - del args[i] - elif not takearg and val is not None: - msg = "Option %s doesn't expects a value" % option - raise ArgumentPreprocessingError(msg) - cb(option, val) - else: - i += 1 - - -def _patch_sys_path(args): - original = list(sys.path) - changes = [] - seen = set() - for arg in args: - path = utils.get_python_path(arg) - if path not in seen: - changes.append(path) - seen.add(path) - - sys.path[:] = changes + sys.path - return original - - -@contextlib.contextmanager -def fix_import_path(args): - """Prepare sys.path for running the linter checks. - - Within this context, each of the given arguments is importable. - Paths are added to sys.path in corresponding order to the arguments. - We avoid adding duplicate directories to sys.path. - `sys.path` is reset to its original value upon exiting this context. - """ - original = _patch_sys_path(args) - try: - yield - finally: - sys.path[:] = original - - class Run: """helper class to use as main for pylint : diff --git a/pylint/lint/utils.py b/pylint/lint/utils.py new file mode 100644 index 000000000..07163e044 --- /dev/null +++ b/pylint/lint/utils.py @@ -0,0 +1,73 @@ +import contextlib +import sys + +from pylint.utils import utils + + +class ArgumentPreprocessingError(Exception): + """Raised if an error occurs during argument preprocessing.""" + + +def preprocess_options(args, search_for): + """look for some options (keys of <search_for>) which have to be processed + before others + + values of <search_for> are callback functions to call when the option is + found + """ + i = 0 + while i < len(args): + arg = args[i] + if arg.startswith("--"): + try: + option, val = arg[2:].split("=", 1) + except ValueError: + option, val = arg[2:], None + try: + cb, takearg = search_for[option] + except KeyError: + i += 1 + else: + del args[i] + if takearg and val is None: + if i >= len(args) or args[i].startswith("-"): + msg = "Option %s expects a value" % option + raise ArgumentPreprocessingError(msg) + val = args[i] + del args[i] + elif not takearg and val is not None: + msg = "Option %s doesn't expects a value" % option + raise ArgumentPreprocessingError(msg) + cb(option, val) + else: + i += 1 + + +def _patch_sys_path(args): + original = list(sys.path) + changes = [] + seen = set() + for arg in args: + path = utils.get_python_path(arg) + if path not in seen: + changes.append(path) + seen.add(path) + + sys.path[:] = changes + sys.path + return original + + +@contextlib.contextmanager +def fix_import_path(args): + """Prepare sys.path for running the linter checks. + + Within this context, each of the given arguments is importable. + Paths are added to sys.path in corresponding order to the arguments. + We avoid adding duplicate directories to sys.path. + `sys.path` is reset to its original value upon exiting this context. + """ + original = _patch_sys_path(args) + try: + yield + finally: + sys.path[:] = original |