29
29
30
30
In order to avoid one-off dependencies for this task, this script uses
31
31
a reasonably working HTML parser and the existing XPath implementation
32
- from Python 2 's standard library. Hopefully we won't render
32
+ from Python's standard library. Hopefully we won't render
33
33
non-well-formed HTML.
34
34
35
35
# Commands
110
110
import re
111
111
import shlex
112
112
from collections import namedtuple
113
- from HTMLParser import HTMLParser
113
+ try :
114
+ from html .parser import HTMLParser
115
+ except ImportError :
116
+ from HTMLParser import HTMLParser
114
117
from xml .etree import cElementTree as ET
115
118
116
119
# ⇤/⇥ are not in HTML 4 but are in HTML 5
117
- from htmlentitydefs import entitydefs
120
+ try :
121
+ from html .entities import entitydefs
122
+ except ImportError :
123
+ from htmlentitydefs import entitydefs
118
124
entitydefs ['larrb' ] = u'\u21e4 '
119
125
entitydefs ['rarrb' ] = u'\u21e5 '
120
126
entitydefs ['nbsp' ] = ' '
123
129
VOID_ELEMENTS = set (['area' , 'base' , 'br' , 'col' , 'embed' , 'hr' , 'img' , 'input' , 'keygen' ,
124
130
'link' , 'menuitem' , 'meta' , 'param' , 'source' , 'track' , 'wbr' ])
125
131
132
+ # Python 2 -> 3 compatibility
133
+ try :
134
+ unichr
135
+ except NameError :
136
+ unichr = chr
126
137
127
138
class CustomHTMLParser (HTMLParser ):
128
139
"""simplified HTML parser.
@@ -184,12 +195,8 @@ def concat_multi_lines(f):
184
195
185
196
# strip the common prefix from the current line if needed
186
197
if lastline is not None :
187
- maxprefix = 0
188
- for i in xrange (min (len (line ), len (lastline ))):
189
- if line [i ] != lastline [i ]:
190
- break
191
- maxprefix += 1
192
- line = line [maxprefix :].lstrip ()
198
+ common_prefix = os .path .commonprefix ([line , lastline ])
199
+ line = line [len (common_prefix ):].lstrip ()
193
200
194
201
firstlineno = firstlineno or lineno
195
202
if line .endswith ('\\ ' ):
@@ -213,7 +220,7 @@ def concat_multi_lines(f):
213
220
214
221
215
222
def get_commands (template ):
216
- with open (template , 'rUb ' ) as f :
223
+ with open (template , 'rU ' ) as f :
217
224
for lineno , line in concat_multi_lines (f ):
218
225
m = LINE_PATTERN .search (line )
219
226
if not m :
@@ -372,7 +379,7 @@ def check_command(c, cache):
372
379
cache .get_file (c .args [0 ])
373
380
ret = True
374
381
except FailedCheck as err :
375
- cerr = err . message
382
+ cerr = str ( err )
376
383
ret = False
377
384
elif len (c .args ) == 2 : # @has/matches <path> <pat> = string test
378
385
cerr = "`PATTERN` did not match"
@@ -413,9 +420,9 @@ def check_command(c, cache):
413
420
414
421
except FailedCheck as err :
415
422
message = '@{}{} check failed' .format ('!' if c .negated else '' , c .cmd )
416
- print_err (c .lineno , c .context , err . message , message )
423
+ print_err (c .lineno , c .context , str ( err ) , message )
417
424
except InvalidCheck as err :
418
- print_err (c .lineno , c .context , err . message )
425
+ print_err (c .lineno , c .context , str ( err ) )
419
426
420
427
def check (target , commands ):
421
428
cache = CachedFiles (target )
0 commit comments