Skip to content

Commit

Permalink
Fix issue where parent is module level
Browse files Browse the repository at this point in the history
* don't repeat print elements
* catch valueerror for linenos
* catch keyboard interupt
  • Loading branch information
Smirl committed Jul 24, 2018
1 parent a73eb31 commit a1c0ec2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 23 additions & 3 deletions baroness/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


from __future__ import print_function
from itertools import repeat
from multiprocessing import Pool
import os
import signal
import sys

from redbaron import RedBaron
Expand All @@ -29,8 +29,9 @@ def search(
options = dict(locals())
options.pop('files')

LOGGER.debug('DEBUG Using options: %s', options)
LOGGER.debug('DEBUG Using %s processes', processes)
pool = Pool(processes)
pool = Pool(processes, _init_worker)
tasks = ((filename, pattern, options) for filename in filenames(files))
try:
for result in pool.imap_unordered(_safe_search_file, tasks):
Expand All @@ -48,6 +49,11 @@ def search(
pool.join()


def _init_worker():
"""Ensure that KeyboardInterrupt is ignored."""
signal.signal(signal.SIGINT, signal.SIG_IGN)


def _safe_search_file(args):
"""A wrapper for multiprocessing around _search_file."""
try:
Expand All @@ -74,12 +80,26 @@ def _search_file(filename, pattern, options):

results = _search(root)
output = []
seen = set()
if results:
output.append(filename)
for result in results:
# Get the correct number of parents
for _ in range(options['parents']):
result = result.parent if result.parent else result
output.append(format_node(result, no_color=options['no_color'], no_linenos=options['no_linenos']))

# Ensure that we don't print the same code twice
if result in seen:
continue
else:
seen.add(result)

# format the output
output.append(format_node(
result,
no_color=options['no_color'],
no_linenos=options['no_linenos']
))
output.append(u'--')
output.append(u'')
return (filename, u'\n'.join(output))
2 changes: 2 additions & 0 deletions baroness/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def format_lineno(lineno):
linenos = count(node.absolute_bounding_box.top_left.line)
except AttributeError:
linenos = repeat(u'**')
except ValueError:
linenos = count()
lines = [
u'{}-{}'.format(format_lineno(lineno).strip(), line)
for lineno, line in zip(linenos, lines)
Expand Down

0 comments on commit a1c0ec2

Please sign in to comment.