|
6 | 6 | import os.path |
7 | 7 | import re |
8 | 8 |
|
9 | | -try: |
10 | | - from coverage.exceptions import NoSource |
11 | | -except ImportError: |
12 | | - # for coverage 5.x |
13 | | - from coverage.misc import NoSource |
14 | 9 | import coverage.plugin |
15 | 10 | import django |
16 | 11 | import django.template |
17 | | -from django.template.base import Lexer, NodeList, Template, TextNode |
| 12 | +from coverage.exceptions import NoSource |
| 13 | +from django.template.base import Lexer, NodeList, Template, TextNode, TokenType |
18 | 14 | from django.template.defaulttags import VerbatimNode |
19 | 15 | from django.templatetags.i18n import BlockTranslateNode |
20 | 16 |
|
21 | | -try: |
22 | | - from django.template.base import TokenType |
23 | | - |
24 | | - def _token_name(token_type): |
25 | | - token_type.name.capitalize() |
26 | | - |
27 | | -except ImportError: |
28 | | - # Django <2.1 uses separate constants for token types |
29 | | - from django.template.base import ( |
30 | | - TOKEN_BLOCK, |
31 | | - TOKEN_MAPPING, |
32 | | - TOKEN_TEXT, |
33 | | - TOKEN_VAR, |
34 | | - ) |
35 | | - |
36 | | - class TokenType: |
37 | | - TEXT = TOKEN_TEXT |
38 | | - VAR = TOKEN_VAR |
39 | | - BLOCK = TOKEN_BLOCK |
40 | | - |
41 | | - def _token_name(token_type): |
42 | | - return TOKEN_MAPPING[token_type] |
43 | | - |
44 | 17 |
|
45 | 18 | class DjangoTemplatePluginException(Exception): |
46 | 19 | """Used for any errors from the plugin itself.""" |
@@ -96,8 +69,8 @@ def check_debug(): |
96 | 69 | return True |
97 | 70 |
|
98 | 71 |
|
99 | | -if django.VERSION < (2, 0): |
100 | | - raise RuntimeError("Django Coverage Plugin requires Django 2.x or higher") |
| 72 | +if django.VERSION < (3, 0): |
| 73 | + raise RuntimeError("Django Coverage Plugin requires Django 3.x or higher") |
101 | 74 |
|
102 | 75 |
|
103 | 76 | # Since we are grabbing at internal details, we have to adapt as they |
@@ -129,14 +102,8 @@ def read_template_source(filename): |
129 | 102 | if not settings.configured: |
130 | 103 | settings.configure() |
131 | 104 |
|
132 | | - with open(filename, "rb") as f: |
133 | | - # The FILE_CHARSET setting will be removed in 3.1: |
134 | | - # https://docs.djangoproject.com/en/3.0/ref/settings/#file-charset |
135 | | - if django.VERSION >= (3, 1): |
136 | | - charset = 'utf-8' |
137 | | - else: |
138 | | - charset = settings.FILE_CHARSET |
139 | | - text = f.read().decode(charset) |
| 105 | + with open(filename, "r", encoding="utf-8") as f: |
| 106 | + text = f.read() |
140 | 107 |
|
141 | 108 | return text |
142 | 109 |
|
@@ -326,7 +293,7 @@ def lines(self): |
326 | 293 | if SHOW_PARSING: |
327 | 294 | print( |
328 | 295 | "%10s %2d: %r" % ( |
329 | | - _token_name(token.token_type), |
| 296 | + token.token_type.capitalize(), |
330 | 297 | token.lineno, |
331 | 298 | token.contents, |
332 | 299 | ) |
|
0 commit comments