Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions django_coverage_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,35 @@

import django
import django.template
from django.template.base import (
Lexer, TextNode, NodeList, Template,
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR,
)
from django.template.base import Lexer, TextNode, NodeList, Template
from django.templatetags.i18n import BlockTranslateNode

try:
from django.template.defaulttags import VerbatimNode
except ImportError:
# Django 1.4 didn't have VerbatimNode
VerbatimNode = None

try:
from django.template.base import TokenType

def _token_name(token_type):
token_type.name.capitalize()

except ImportError:
# Django <2.1 uses separate constants for token types
from django.template.base import (
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR
)

class TokenType:
TEXT = TOKEN_TEXT
VAR = TOKEN_VAR
BLOCK = TOKEN_BLOCK

def _token_name(token_type):
return TOKEN_MAPPING[token_type]


class DjangoTemplatePluginException(Exception):
"""Used for any errors from the plugin itself."""
Expand Down Expand Up @@ -298,20 +316,20 @@ def lines(self):
if SHOW_PARSING:
print(
"%10s %2d: %r" % (
TOKEN_MAPPING[token.token_type],
_token_name(token.token_type),
token.lineno,
token.contents,
)
)
if token.token_type == TOKEN_BLOCK:
if token.token_type == TokenType.BLOCK:
if token.contents == "endcomment":
comment = False
continue

if comment:
continue

if token.token_type == TOKEN_BLOCK:
if token.token_type == TokenType.BLOCK:
if token.contents.startswith("endblock"):
inblock = False
elif token.contents.startswith("block"):
Expand Down Expand Up @@ -340,10 +358,10 @@ def lines(self):

source_lines.add(token.lineno)

elif token.token_type == TOKEN_VAR:
elif token.token_type == TokenType.VAR:
source_lines.add(token.lineno)

elif token.token_type == TOKEN_TEXT:
elif token.token_type == TokenType.TEXT:
if extends and not inblock:
continue
# Text nodes often start with newlines, but we don't want to
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
envlist =
py27-django{18,19,110,111,111tip},
py34-django{18,19,110,111,111tip,20},
py35-django{18,19,110,111,111tip,20,tip},
py36-django{18,19,110,111,111tip,20,tip},
py35-django{18,19,110,111,111tip,20,21,tip},
py36-django{18,19,110,111,111tip,20,21,tip},
py37-django{20,21,tip},
check,doc

[testenv]
Expand All @@ -30,6 +31,7 @@ deps =
django111: Django >=1.11, <2.0
django111tip: https://github.com/django/django/archive/stable/1.11.x.tar.gz
django20: Django>=2.0b1,<2.1
django21: Django>=2.1, <2.2
djangotip: https://github.com/django/django/archive/master.tar.gz

commands =
Expand Down