Skip to content
Closed
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
62 changes: 46 additions & 16 deletions django_coverage_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,51 @@
import os.path
import re

import django
import django.template
from django.template.base import Lexer, NodeList, Template, TextNode
from django.template.defaulttags import VerbatimNode
from django.templatetags.i18n import BlockTranslateNode
from six.moves import range

import coverage.plugin

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

def get_token_type(token):
"""Compatibility wrapper for getting token_type

Mirrors function created to handle Django before 2.1
Remove this function (with direct attr access) once
support for Django<2.1 is dropped.

"""
return token.token_type

except ImportError:
# Django 1.4 didn't have VerbatimNode
VerbatimNode = None
# TODO: remove ImportError once Django<2.1 support removed
from django.template.base import (
TOKEN_BLOCK, TOKEN_TEXT, TOKEN_VAR, TOKEN_COMMENT,
)
from enum import Enum

class TokenType(Enum):
"""Replicate the TokenType class from Django 2.1"""

TEXT = TOKEN_TEXT
VAR = TOKEN_VAR
BLOCK = TOKEN_BLOCK
COMMENT = TOKEN_COMMENT

def get_token_type(token):
"""Compatibility wrapper for getting token_type

Ensures an Enum is always used, even when older versions of
Django's template lexer return an integer

"""
return TokenType(token.token_type)


class DjangoTemplatePluginException(Exception):
Expand Down Expand Up @@ -168,7 +197,7 @@ def file_reporter(self, filename):
return FileReporter(filename)

def find_executable_files(self, src_dir):
for (dirpath, dirnames, filenames) in os.walk(src_dir):
for (dirpath, _, filenames) in os.walk(src_dir):
for filename in filenames:
# We're only interested in files that look like reasonable HTML
# files: Must end with .htm or .html, and must not have certain
Expand Down Expand Up @@ -295,23 +324,24 @@ def lines(self):
inblock = False

for token in tokens:
token_type = get_token_type(token)
if SHOW_PARSING:
print(
"%10s %2d: %r" % (
TOKEN_MAPPING[token.token_type],
token_type.name.capitalize(),
token.lineno,
token.contents,
)
)
if token.token_type == TOKEN_BLOCK:
if token_type == TokenType.BLOCK:
if token.contents == "endcomment":
comment = False
continue

if comment:
continue

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

source_lines.add(token.lineno)

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

elif token.token_type == TOKEN_TEXT:
elif token_type == TokenType.TEXT:
if extends and not inblock:
continue
# Text nodes often start with newlines, but we don't want to
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
url='https://github.com/nedbat/django_coverage_plugin',
packages=['django_coverage_plugin'],
install_requires=[
'enum34;python_version<"3.4"',
'coverage >= 4.0',
'six >= 1.4.0',
],
Expand Down
7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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{111,111tip,20,21,tip},
check,doc

[testenv]
Expand All @@ -29,7 +29,8 @@ deps =
django110: Django >=1.10, <1.11
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
django20: Django>=2.0,<2.1
django21: Django>=2.1rc1,<2.2
djangotip: https://github.com/django/django/archive/master.tar.gz

commands =
Expand Down