Changeset 391 for python/trunk/Doc/tools/sphinxext/suspicious.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/tools/sphinxext/suspicious.py
r2 r391 42 42 """ 43 43 44 import os, sys 44 import os 45 import re 45 46 import csv 46 import re 47 import sys 48 47 49 from docutils import nodes 48 49 try: 50 from sphinx.builders import Builder 51 except ImportError: 52 from sphinx.builder import Builder 53 50 from sphinx.builders import Builder 54 51 55 52 detect_all = re.compile(ur''' … … 60 57 ''', re.UNICODE | re.VERBOSE).finditer 61 58 59 62 60 class Rule: 63 61 def __init__(self, docname, lineno, issue, line): 64 " A rule for ignoring issues"62 """A rule for ignoring issues""" 65 63 self.docname = docname # document to which this rule applies 66 64 self.lineno = lineno # line number in the original source; … … 69 67 self.issue = issue # the markup fragment that triggered this rule 70 68 self.line = line # text of the container element (single line only) 69 self.used = False 70 71 def __repr__(self): 72 return '{0.docname},,{0.issue},{0.line}'.format(self) 73 74 75 76 class dialect(csv.excel): 77 """Our dialect: uses only linefeed as newline.""" 78 lineterminator = '\n' 71 79 72 80 73 81 class CheckSuspiciousMarkupBuilder(Builder): 74 82 """ 75 Checks for possibly invalid markup that may leak into the output 83 Checks for possibly invalid markup that may leak into the output. 76 84 """ 77 85 name = 'suspicious' … … 82 90 open(self.log_file_name, 'w').close() 83 91 # load database of previously ignored issues 84 self.load_rules(os.path.join(os.path.dirname(__file__), 'susp-ignored.csv')) 92 self.load_rules(os.path.join(os.path.dirname(__file__), 93 'susp-ignored.csv')) 85 94 86 95 def get_outdated_docs(self): … … 91 100 92 101 def prepare_writing(self, docnames): 93 ### PYTHON PROJECT SPECIFIC ### 94 for name in set(docnames): 95 if name.split('/', 1)[0] == 'documenting': 96 docnames.remove(name) 97 ### PYTHON PROJECT SPECIFIC ### 102 pass 98 103 99 104 def write_doc(self, docname, doctree): 100 self.any_issue = False # set when any issue is encountered in this document 105 # set when any issue is encountered in this document 106 self.any_issue = False 101 107 self.docname = docname 102 108 visitor = SuspiciousVisitor(doctree, self) … … 104 110 105 111 def finish(self): 112 unused_rules = [rule for rule in self.rules if not rule.used] 113 if unused_rules: 114 self.warn('Found %s/%s unused rules:' % 115 (len(unused_rules), len(self.rules))) 116 for rule in unused_rules: 117 self.info(repr(rule)) 106 118 return 107 119 … … 111 123 112 124 def is_ignored(self, line, lineno, issue): 113 """Determine whether this issue should be ignored. 114 """ 125 """Determine whether this issue should be ignored.""" 115 126 docname = self.docname 116 127 for rule in self.rules: … … 129 140 abs(rule.lineno - lineno) > 5: continue 130 141 # if it came this far, the rule matched 142 rule.used = True 131 143 return True 132 144 return False … … 145 157 def write_log_entry(self, lineno, issue, text): 146 158 f = open(self.log_file_name, 'ab') 147 writer = csv.writer(f )159 writer = csv.writer(f, dialect) 148 160 writer.writerow([self.docname.encode('utf-8'), 149 lineno, 150 issue.encode('utf-8'), 151 text.strip().encode('utf-8')]) 152 del writer 161 lineno, 162 issue.encode('utf-8'), 163 text.strip().encode('utf-8')]) 153 164 f.close() 154 165 … … 165 176 for i, row in enumerate(csv.reader(f)): 166 177 if len(row) != 4: 167 raise ValueError("wrong format in %s, line %d: %s" % (filename, i+1, row)) 178 raise ValueError( 179 "wrong format in %s, line %d: %s" % (filename, i+1, row)) 168 180 docname, lineno, issue, text = row 169 181 docname = docname.decode('utf-8') … … 179 191 180 192 def get_lineno(node): 181 " Obtain line number information for a node"193 """Obtain line number information for a node.""" 182 194 lineno = None 183 195 while lineno is None and node: … … 204 216 p = text.rfind('\n', 0, index) + 1 205 217 q = text.find('\n', index) 206 if q<0: q = len(text) 218 if q < 0: 219 q = len(text) 207 220 return text[p:q] 208 221 … … 223 236 seen = set() # don't report the same issue more than only once per line 224 237 for match in detect_all(text): 225 #import pdb; pdb.set_trace()226 238 issue = match.group() 227 239 line = extract_line(text, match.start())
Note:
See TracChangeset
for help on using the changeset viewer.