Port suspicious markup builder and patchlevel.py so that they can be used with Python 2 and 3 without conversion.

This commit is contained in:
Georg Brandl 2010-10-29 05:30:17 +00:00
parent b98273fff9
commit a17fd1f3b5
2 changed files with 37 additions and 17 deletions

View File

@ -68,4 +68,4 @@ def get_version_info():
return version, release
if __name__ == '__main__':
print get_header_version_info('.')[1]
print(get_header_version_info('.')[1])

View File

@ -49,13 +49,15 @@ import sys
from docutils import nodes
from sphinx.builders import Builder
detect_all = re.compile(ur'''
detect_all = re.compile(r'''
::(?=[^=])| # two :: (but NOT ::=)
:[a-zA-Z][a-zA-Z0-9]+| # :foo
`| # ` (seldom used by itself)
(?<!\.)\.\.[ \t]*\w+: # .. foo: (but NOT ... else:)
''', re.UNICODE | re.VERBOSE).finditer
py3 = sys.version_info >= (3, 0)
class Rule:
def __init__(self, docname, lineno, issue, line):
@ -136,7 +138,11 @@ class CheckSuspiciousMarkupBuilder(Builder):
if not self.any_issue: self.info()
self.any_issue = True
self.write_log_entry(lineno, issue, text)
self.warn('[%s:%d] "%s" found in "%-.120s"' % (
if py3:
self.warn('[%s:%d] "%s" found in "%-.120s"' %
(self.docname, lineno, issue, text))
else:
self.warn('[%s:%d] "%s" found in "%-.120s"' % (
self.docname.encode(sys.getdefaultencoding(),'replace'),
lineno,
issue.encode(sys.getdefaultencoding(),'replace'),
@ -144,13 +150,19 @@ class CheckSuspiciousMarkupBuilder(Builder):
self.app.statuscode = 1
def write_log_entry(self, lineno, issue, text):
f = open(self.log_file_name, 'ab')
writer = csv.writer(f, dialect)
writer.writerow([self.docname.encode('utf-8'),
lineno,
issue.encode('utf-8'),
text.strip().encode('utf-8')])
f.close()
if py3:
f = open(self.log_file_name, 'a')
writer = csv.writer(f, dialect)
writer.writerow([self.docname, lineno, issue, text.strip()])
f.close()
else:
f = open(self.log_file_name, 'ab')
writer = csv.writer(f, dialect)
writer.writerow([self.docname.encode('utf-8'),
lineno,
issue.encode('utf-8'),
text.strip().encode('utf-8')])
f.close()
def load_rules(self, filename):
"""Load database of previously ignored issues.
@ -160,18 +172,26 @@ class CheckSuspiciousMarkupBuilder(Builder):
"""
self.info("loading ignore rules... ", nonl=1)
self.rules = rules = []
try: f = open(filename, 'rb')
except IOError: return
try:
if py3:
f = open(filename, 'r')
else:
f = open(filename, 'rb')
except IOError:
return
for i, row in enumerate(csv.reader(f)):
if len(row) != 4:
raise ValueError(
"wrong format in %s, line %d: %s" % (filename, i+1, row))
docname, lineno, issue, text = row
docname = docname.decode('utf-8')
if lineno: lineno = int(lineno)
else: lineno = None
issue = issue.decode('utf-8')
text = text.decode('utf-8')
if lineno:
lineno = int(lineno)
else:
lineno = None
if not py3:
docname = docname.decode('utf-8')
issue = issue.decode('utf-8')
text = text.decode('utf-8')
rule = Rule(docname, lineno, issue, text)
rules.append(rule)
f.close()