def get_template_info(source, context_lines=3): line = 0 upto = 0 source_lines = [] before = during = after = "" origin, (start, end) = source template_source = origin.reload() for num, next in enumerate(linebreak_iter(template_source)): if start >= upto and end <= next: line = num before = template_source[upto:start] during = template_source[start:end] after = template_source[end:next] source_lines.append((num, template_source[upto:next])) upto = next top = max(1, line - context_lines) bottom = min(len(source_lines), line + 1 + context_lines) context = [] for num, content in source_lines[top:bottom]: context.append({ 'num': num, 'content': content, 'highlight': (num == line), }) return { 'name': origin.name, 'context': context, }
def get_template_exception_info(self): template_source, start, end, name = self._template context_lines = 10 line = 0 upto = 0 source_lines = [] before = during = after = "" for num, next in enumerate(linebreak_iter(template_source)): if start >= upto and end <= next: line = num before = escape(template_source[upto:start]) during = escape(template_source[start:end]) after = escape(template_source[end:next]) source_lines.append( (num, escape(template_source[upto:next])) ) upto = next total = len(source_lines) top = max(1, line - context_lines) bottom = min(total, line + 1 + context_lines) self.template_info = { 'message': self.exc_value.args[0], 'source_lines': source_lines[top:bottom], 'before': before, 'during': during, 'after': after, 'top': top, 'bottom': bottom, 'total': total, 'line': line, 'name': name, }
def get_template_info(source, context_lines=3): line = 0 upto = 0 source_lines = [] # before = during = after = "" origin, (start, end) = source template_source = origin.reload() for num, next in enumerate(linebreak_iter(template_source)): if start >= upto and end <= next: line = num # before = template_source[upto:start] # during = template_source[start:end] # after = template_source[end:next] source_lines.append((num, template_source[upto:next])) upto = next top = max(1, line - context_lines) bottom = min(len(source_lines), line + 1 + context_lines) context = [] for num, content in source_lines[top:bottom]: context.append({ 'num': num, 'content': content, 'highlight': (num == line), }) return { 'name': origin.name, 'context': context, }
def get_template_exception_info(self): template_source, start, end, name = self._template context_lines = 10 line = 0 upto = 0 source_lines = [] before = during = after = "" for num, next in enumerate(linebreak_iter(template_source)): if start >= upto and end <= next: line = num before = escape(template_source[upto:start]) during = escape(template_source[start:end]) after = escape(template_source[end:next]) source_lines.append((num, escape(template_source[upto:next]))) upto = next total = len(source_lines) top = max(1, line - context_lines) bottom = min(total, line + 1 + context_lines) self.template_info = { 'message': self.exc_value.args[0], 'source_lines': source_lines[top:bottom], 'before': before, 'during': during, 'after': after, 'top': top, 'bottom': bottom, 'total': total, 'line': line, 'name': name, }
def _get_template_exception_info(self, exception): origin, (start, end) = exception.source template_source = origin.reload() upto = line = 0 for num, next in enumerate(debug.linebreak_iter(template_source)): if start >= upto and end <= next: line = num upto = next return line
def _generate_django_exception(e, source=None): '''Generate a Django exception from a Jinja exception''' from django.views.debug import linebreak_iter import re if source: exception = DjangoTemplateSyntaxError(e.message) exception_dict = e.__dict__ del exception_dict['source'] # Fetch the entire template in a string template_string = source[0].reload() # Get the line number from the error message, if available match = re.match('.* at (\d+)$', e.message) start_index = 0 stop_index = 0 if match: # Convert the position found in the stacktrace to a position # the Django template debug system can use position = int(match.group(1)) + source[1][0] + 1 for index in linebreak_iter(template_string): if index >= position: stop_index = min(index, position + 3) start_index = min(index, position - 2) break start_index = index else: # So there wasn't a matching error message, in that case we # simply have to highlight the entire line instead of the specific # words ignore_lines = -1 for i, index in enumerate(linebreak_iter(template_string)): if source[1][0] > index: ignore_lines += 1 if i - ignore_lines == e.lineno: stop_index = index break start_index = index # Convert the positions to a source that is compatible with the # Django template debugger source = source[0], ( start_index, stop_index, ) else: # No source available so we let Django fetch it for us lineno = e.lineno - 1 template_string, source = django_loader.find_template_source(e.name) exception = DjangoTemplateSyntaxError(e.message) # Find the positions by the line number given in the exception start_index = 0 for i in range(lineno): start_index = template_string.index('\n', start_index + 1) source = source, ( start_index + 1, template_string.index('\n', start_index + 1) + 1, ) # Set our custom source as source for the exception so the Django # template debugger can use it exception.source = source return exception