mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-15 00:44:44 +00:00
Reorganize methods alphabetically
Add methods to report errors to the style guide A single file works fine now but not a directory
This commit is contained in:
parent
3b830366b6
commit
189faf68ba
1 changed files with 100 additions and 57 deletions
|
|
@ -116,68 +116,38 @@ class Manager(object):
|
||||||
# it to an integer
|
# it to an integer
|
||||||
return int(jobs)
|
return int(jobs)
|
||||||
|
|
||||||
def start(self):
|
def _report_after_parallel(self):
|
||||||
"""Start checking files."""
|
style_guide = self.style_guide
|
||||||
LOG.info('Making checkers')
|
for (filename, results) in iter(self.results_queue.get, 'DONE'):
|
||||||
self.make_checkers()
|
results = sorted(results, key=lambda tup: (tup[2], tup[3]))
|
||||||
if not self.using_multiprocessing:
|
for (error_code, line_number, column, text) in results:
|
||||||
return
|
style_guide.handle_error(
|
||||||
|
code=error_code,
|
||||||
|
filename=filename,
|
||||||
|
line_number=line_number,
|
||||||
|
column_number=column,
|
||||||
|
text=text
|
||||||
|
)
|
||||||
|
|
||||||
LOG.info('Populating process queue')
|
def _report_after_serial(self):
|
||||||
|
style_guide = self.style_guide
|
||||||
for checker in self.checkers:
|
for checker in self.checkers:
|
||||||
self.process_queue.put(checker)
|
results = sorted(checker.results, key=lambda tup: (tup[2], tup[3]))
|
||||||
|
filename = checker.filename
|
||||||
def stop(self):
|
for (error_code, line_number, column, text) in results:
|
||||||
"""Stop checking files."""
|
style_guide.handle_error(
|
||||||
if not self.using_multiprocessing:
|
code=error_code,
|
||||||
return
|
filename=filename,
|
||||||
|
line_number=line_number,
|
||||||
LOG.info('Notifying process workers of completion')
|
column_number=column,
|
||||||
for i in range(self.jobs or 0):
|
text=text
|
||||||
self.process_queue.put('DONE')
|
)
|
||||||
|
|
||||||
LOG.info('Joining process workers')
|
|
||||||
for process in self.processes:
|
|
||||||
process.join()
|
|
||||||
|
|
||||||
def make_checkers(self, paths=None):
|
|
||||||
# type: (List[str]) -> NoneType
|
|
||||||
"""Create checkers for each file."""
|
|
||||||
if paths is None:
|
|
||||||
paths = self.arguments
|
|
||||||
filename_patterns = self.options.filename
|
|
||||||
self.checkers = [
|
|
||||||
FileChecker(filename, self.checks, self.style_guide)
|
|
||||||
for argument in paths
|
|
||||||
for filename in utils.filenames_from(argument,
|
|
||||||
self.is_path_excluded)
|
|
||||||
if utils.fnmatch(filename, filename_patterns)
|
|
||||||
]
|
|
||||||
|
|
||||||
def _run_checks_from_queue(self):
|
def _run_checks_from_queue(self):
|
||||||
LOG.info('Running checks in parallel')
|
LOG.info('Running checks in parallel')
|
||||||
for checker in iter(self.process_queue.get, 'DONE'):
|
for checker in iter(self.process_queue.get, 'DONE'):
|
||||||
LOG.debug('Running checker for file "%s"', checker.filename)
|
LOG.debug('Running checker for file "%s"', checker.filename)
|
||||||
checker.run_checks()
|
checker.run_checks(self.results_queue)
|
||||||
|
|
||||||
def run(self):
|
|
||||||
"""Run all the checkers.
|
|
||||||
|
|
||||||
This handles starting the process workers or just simply running all
|
|
||||||
of the checks in serial.
|
|
||||||
"""
|
|
||||||
if self.using_multiprocessing:
|
|
||||||
LOG.info('Starting process workers')
|
|
||||||
for i in range(self.jobs or 0):
|
|
||||||
proc = multiprocessing.Process(
|
|
||||||
target=self._run_checks_from_queue
|
|
||||||
)
|
|
||||||
proc.daemon = True
|
|
||||||
proc.start()
|
|
||||||
self.processes.append(proc)
|
|
||||||
else:
|
|
||||||
for checker in self.checkers:
|
|
||||||
checker.run_checks()
|
|
||||||
|
|
||||||
def is_path_excluded(self, path):
|
def is_path_excluded(self, path):
|
||||||
# type: (str) -> bool
|
# type: (str) -> bool
|
||||||
|
|
@ -205,6 +175,76 @@ class Manager(object):
|
||||||
'' if match else 'not ')
|
'' if match else 'not ')
|
||||||
return match
|
return match
|
||||||
|
|
||||||
|
def make_checkers(self, paths=None):
|
||||||
|
# type: (List[str]) -> NoneType
|
||||||
|
"""Create checkers for each file."""
|
||||||
|
if paths is None:
|
||||||
|
paths = self.arguments
|
||||||
|
filename_patterns = self.options.filename
|
||||||
|
self.checkers = [
|
||||||
|
FileChecker(filename, self.checks, self.style_guide)
|
||||||
|
for argument in paths
|
||||||
|
for filename in utils.filenames_from(argument,
|
||||||
|
self.is_path_excluded)
|
||||||
|
if utils.fnmatch(filename, filename_patterns)
|
||||||
|
]
|
||||||
|
|
||||||
|
def report(self):
|
||||||
|
"""Report all of the errors found in the managed file checkers.
|
||||||
|
|
||||||
|
This iterates over each of the checkers and reports the errors sorted
|
||||||
|
by line number.
|
||||||
|
"""
|
||||||
|
if self.using_multiprocessing:
|
||||||
|
self._report_after_parallel()
|
||||||
|
else:
|
||||||
|
self._report_after_serial()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
"""Run all the checkers.
|
||||||
|
|
||||||
|
This handles starting the process workers or just simply running all
|
||||||
|
of the checks in serial.
|
||||||
|
"""
|
||||||
|
if self.using_multiprocessing:
|
||||||
|
LOG.info('Starting process workers')
|
||||||
|
for i in range(self.jobs or 0):
|
||||||
|
proc = multiprocessing.Process(
|
||||||
|
target=self._run_checks_from_queue
|
||||||
|
)
|
||||||
|
proc.daemon = True
|
||||||
|
proc.start()
|
||||||
|
self.processes.append(proc)
|
||||||
|
else:
|
||||||
|
for checker in self.checkers:
|
||||||
|
checker.run_checks()
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""Start checking files."""
|
||||||
|
LOG.info('Making checkers')
|
||||||
|
self.make_checkers()
|
||||||
|
if not self.using_multiprocessing:
|
||||||
|
return
|
||||||
|
|
||||||
|
LOG.info('Populating process queue')
|
||||||
|
for checker in self.checkers:
|
||||||
|
self.process_queue.put(checker)
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""Stop checking files."""
|
||||||
|
if not self.using_multiprocessing:
|
||||||
|
return
|
||||||
|
|
||||||
|
LOG.info('Notifying process workers of completion')
|
||||||
|
for i in range(self.jobs or 0):
|
||||||
|
self.process_queue.put('DONE')
|
||||||
|
|
||||||
|
LOG.info('Joining process workers')
|
||||||
|
for process in self.processes:
|
||||||
|
process.join()
|
||||||
|
LOG.info('Processes joined')
|
||||||
|
self.results_queue.put('DONE')
|
||||||
|
|
||||||
|
|
||||||
class FileChecker(object):
|
class FileChecker(object):
|
||||||
"""Manage running checks for a file and aggregate the results."""
|
"""Manage running checks for a file and aggregate the results."""
|
||||||
|
|
@ -246,7 +286,7 @@ class FileChecker(object):
|
||||||
"""Report an error by storing it in the results list."""
|
"""Report an error by storing it in the results list."""
|
||||||
if error_code is None:
|
if error_code is None:
|
||||||
error_code, text = text.split(' ', 1)
|
error_code, text = text.split(' ', 1)
|
||||||
error = (error_code, self.filename, line_number, column, text)
|
error = (error_code, line_number, column, text)
|
||||||
self.results.append(error)
|
self.results.append(error)
|
||||||
return error_code
|
return error_code
|
||||||
|
|
||||||
|
|
@ -323,7 +363,7 @@ class FileChecker(object):
|
||||||
self.run_physical_checks(file_processor.lines[-1])
|
self.run_physical_checks(file_processor.lines[-1])
|
||||||
self.run_logical_checks()
|
self.run_logical_checks()
|
||||||
|
|
||||||
def run_checks(self):
|
def run_checks(self, results_queue):
|
||||||
"""Run checks against the file."""
|
"""Run checks against the file."""
|
||||||
if self.processor.should_ignore_file():
|
if self.processor.should_ignore_file():
|
||||||
return
|
return
|
||||||
|
|
@ -334,6 +374,9 @@ class FileChecker(object):
|
||||||
self.report(exc.error_code, exc.line_number, exc.column_number,
|
self.report(exc.error_code, exc.line_number, exc.column_number,
|
||||||
exc.error_message)
|
exc.error_message)
|
||||||
|
|
||||||
|
if results_queue is not None:
|
||||||
|
results_queue.put_nowait((self.filename, self.results))
|
||||||
|
|
||||||
def handle_comment(self, token, token_text):
|
def handle_comment(self, token, token_text):
|
||||||
"""Handle the logic when encountering a comment token."""
|
"""Handle the logic when encountering a comment token."""
|
||||||
# The comment also ends a physical line
|
# The comment also ends a physical line
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue