lint: Pass args to LinterConfig when initializing it.

This makes it easy to use args inside LinterConfig.
This commit is contained in:
Aman Agrawal 2019-06-21 22:06:41 +05:30 committed by Tim Abbott
parent 0b12ec1417
commit 7b7e887386
2 changed files with 18 additions and 16 deletions

View File

@ -66,7 +66,7 @@ def run():
# Invoke the appropriate lint checker for each language,
# and also check files for extra whitespace.
linter_config = LinterConfig(by_lang)
linter_config = LinterConfig(args, by_lang)
linter_config.external_linter('add_class', ['tools/find-add-class'], ['js'])
linter_config.external_linter('css', ['node', 'node_modules/.bin/stylelint'], ['css', 'scss'])
linter_config.external_linter('eslint', ['node', 'node_modules/.bin/eslint',
@ -117,7 +117,8 @@ def run():
# type: () -> int
failed = check_pep8(list(python_part2))
return 1 if failed else 0
linter_config.do_lint(args)
linter_config.do_lint()
if __name__ == '__main__':
run()

View File

@ -66,9 +66,10 @@ def run_parallel(lint_functions):
class LinterConfig:
lint_functions = {} # type: Dict[str, Callable[[], int]]
def __init__(self, by_lang):
# type: (Any) -> None
self.by_lang = by_lang
def __init__(self, args, by_lang):
# type: (argparse.Namespace, Any) -> None
self.args = args
self.by_lang = by_lang # type: Dict[str, List[str]]
def lint(self, func):
# type: (Callable[[], int]) -> Callable[[], int]
@ -114,26 +115,26 @@ class LinterConfig:
self.lint_functions[name] = run_linter
def set_logger(self, verbose_timing):
# type: (bool) -> None
def set_logger(self):
# type: () -> None
logging.basicConfig(format="%(asctime)s %(message)s")
logger = logging.getLogger()
if verbose_timing:
if self.args.verbose_timing:
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.WARNING)
def do_lint(self, args):
# type: (argparse.Namespace) -> None
assert not args.only or not args.skip, "Only one of --only or --skip can be used at once."
if args.only:
self.lint_functions = {linter: self.lint_functions[linter] for linter in args.only}
for linter in args.skip:
def do_lint(self):
# type: () -> None
assert not self.args.only or not self.args.skip, "Only one of --only or --skip can be used at once."
if self.args.only:
self.lint_functions = {linter: self.lint_functions[linter] for linter in self.args.only}
for linter in self.args.skip:
del self.lint_functions[linter]
if args.list:
if self.args.list:
print("\n".join(self.lint_functions.keys()))
sys.exit()
self.set_logger(args.verbose_timing)
self.set_logger()
failed = run_parallel(self.lint_functions)
sys.exit(1 if failed else 0)