diff --git a/hawkcatcher/__init__.py b/hawkcatcher/__init__.py index 2c86bd2..f07f10f 100644 --- a/hawkcatcher/__init__.py +++ b/hawkcatcher/__init__.py @@ -120,54 +120,57 @@ def get_near_filelines(self, filepath, line, margin=5): :return trace: tuple """ # Read file and strip right spaces - with open(filepath, 'r') as file: - content = file.readlines() - content = [x.rstrip() for x in content] - - # Calculate upper and lower strings for code fragment - - # Error in 7th line in file <=> 6th element in array - error_line_in_array = line - 1 - - # Start and end are being counted for lines array - # Start from 0 or error_line_in_array - margin - start = max(0, error_line_in_array - margin) - - # file | array - # line | key - # 1 | 0 - # /¯¯ 2 | 1 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\ # start from 1 - # | 3 | 2 | - # | 4 | 3 | - # | 5 | 4 | - # | 6 | 5 | - # | [ 7 | 6 raise Error() ] | - # | 8 | 7 | - # | 9 | 8 | - # | 10 | 9 | - # | 11 | 10 | - # \_ 12 | 11 __________________/ # end is 12 because python doesn't get element on the right side of range - # 13 | 12 - # 14 | 13 - # - # End is the last line of code fragment in array plus 1 - # or the number of code lines in file - end = min(len(content), error_line_in_array + margin + 1) - - # Trace tuple to be returned - trace = [] - - # Cycle index for line number - index = 1 - - # Get cut of original file - lines = content[start:end] - for array_line in range(start, end): - trace.append({ - # +1 because lines in array start from 0 - 'line': array_line + 1, - # Get item from lines array - 'content': lines[array_line - start] - }) - - return trace + try: + with open(filepath, 'r') as file: + content = file.readlines() + content = [x.rstrip() for x in content] + + # Calculate upper and lower strings for code fragment + + # Error in 7th line in file <=> 6th element in array + error_line_in_array = line - 1 + + # Start and end are being counted for lines array + # Start from 0 or error_line_in_array - margin + start = max(0, error_line_in_array - margin) + + # file | array + # line | key + # 1 | 0 + # /¯¯ 2 | 1 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\ # start from 1 + # | 3 | 2 | + # | 4 | 3 | + # | 5 | 4 | + # | 6 | 5 | + # | [ 7 | 6 raise Error() ] | + # | 8 | 7 | + # | 9 | 8 | + # | 10 | 9 | + # | 11 | 10 | + # \_ 12 | 11 __________________/ # end is 12 because python doesn't get element on the right side of range + # 13 | 12 + # 14 | 13 + # + # End is the last line of code fragment in array plus 1 + # or the number of code lines in file + end = min(len(content), error_line_in_array + margin + 1) + + # Trace tuple to be returned + trace = [] + + # Cycle index for line number + index = 1 + + # Get cut of original file + lines = content[start:end] + for array_line in range(start, end): + trace.append({ + # +1 because lines in array start from 0 + 'line': array_line + 1, + # Get item from lines array + 'content': lines[array_line - start] + }) + return trace + # If cannot read module source code + except Exception as e: + return []