Back to index

python3.2  3.2.2
Functions | Variables
rstlint Namespace Reference

Functions

def checker
def check_syntax
def check_suspicious_constructs
def check_whitespace
def check_line_length
def check_leaked_markup
def main

Variables

list directives
string all_directives = '('
tuple seems_directive_re = re.compile(r'\.\. %s([^a-z:]|:(?!:))' % all_directives)
tuple default_role_re = re.compile(r'(^| )`\w([^`]*?\w)?`($| )')
tuple leaked_markup_re = re.compile(r'[a-z]::[^=]|:[a-z]+:|`|\.\.\s*\w+:')
dictionary checkers = {}
dictionary checker_props = {'severity': 1, 'falsepositives': False}

Function Documentation

def rstlint.check_leaked_markup (   fn,
  lines 
)
Check HTML files for leaked reST markup; this only works if
the HTML files have been built.

Definition at line 121 of file rstlint.py.

00121 
00122 def check_leaked_markup(fn, lines):
00123     """Check HTML files for leaked reST markup; this only works if
00124     the HTML files have been built.
00125     """
00126     for lno, line in enumerate(lines):
00127         if leaked_markup_re.search(line):
00128             yield lno+1, 'possibly leaked markup: %r' % line
00129 

Here is the call graph for this function:

def rstlint.check_line_length (   fn,
  lines 
)
Check for line length; this checker is not run by default.

Definition at line 107 of file rstlint.py.

00107 
00108 def check_line_length(fn, lines):
00109     """Check for line length; this checker is not run by default."""
00110     for lno, line in enumerate(lines):
00111         if len(line) > 81:
00112             # don't complain about tables, links and function signatures
00113             if line.lstrip()[0] not in '+|' and \
00114                'http://' not in line and \
00115                not line.lstrip().startswith(('.. function',
00116                                              '.. method',
00117                                              '.. cfunction')):
00118                 yield lno+1, "line too long"
00119 
00120 
@checker('.html', severity=2, falsepositives=True)

Here is the call graph for this function:

def rstlint.check_suspicious_constructs (   fn,
  lines 
)
Check for suspicious reST constructs.

Definition at line 80 of file rstlint.py.

00080 
00081 def check_suspicious_constructs(fn, lines):
00082     """Check for suspicious reST constructs."""
00083     inprod = False
00084     for lno, line in enumerate(lines):
00085         if seems_directive_re.match(line):
00086             yield lno+1, 'comment seems to be intended as a directive'
00087         if '.. productionlist::' in line:
00088             inprod = True
00089         elif not inprod and default_role_re.search(line):
00090             yield lno+1, 'default role used'
00091         elif inprod and not line.strip():
00092             inprod = False
00093 
00094 
@checker('.py', '.rst')

Here is the call graph for this function:

def rstlint.check_syntax (   fn,
  lines 
)
Check Python examples for valid syntax.

Definition at line 66 of file rstlint.py.

00066 
00067 def check_syntax(fn, lines):
00068     """Check Python examples for valid syntax."""
00069     code = ''.join(lines)
00070     if '\r' in code:
00071         if os.name != 'nt':
00072             yield 0, '\\r in code file'
00073         code = code.replace('\r', '')
00074     try:
00075         compile(code, fn, 'exec')
00076     except SyntaxError as err:
00077         yield err.lineno, 'not compilable: %s' % err
00078 
00079 
@checker('.rst', severity=2)

Here is the call graph for this function:

def rstlint.check_whitespace (   fn,
  lines 
)
Check for whitespace and line length issues.

Definition at line 95 of file rstlint.py.

00095 
00096 def check_whitespace(fn, lines):
00097     """Check for whitespace and line length issues."""
00098     for lno, line in enumerate(lines):
00099         if '\r' in line:
00100             yield lno+1, '\\r in line'
00101         if '\t' in line:
00102             yield lno+1, 'OMG TABS!!!1'
00103         if line[:-1].rstrip(' \t') != line[:-1]:
00104             yield lno+1, 'trailing whitespace'
00105 
00106 
@checker('.rst', severity=0)

Here is the call graph for this function:

def rstlint.checker (   suffixes,
  kwds 
)
Decorator to register a function as a checker.

Definition at line 54 of file rstlint.py.

00054 
00055 def checker(*suffixes, **kwds):
00056     """Decorator to register a function as a checker."""
00057     def deco(func):
00058         for suffix in suffixes:
00059             checkers.setdefault(suffix, []).append(func)
00060         for prop in checker_props:
00061             setattr(func, prop, kwds.get(prop, checker_props[prop]))
00062         return func
00063     return deco
00064 
00065 
@checker('.py', severity=4)

Here is the call graph for this function:

Here is the caller graph for this function:

def rstlint.main (   argv)

Definition at line 130 of file rstlint.py.

00130 
00131 def main(argv):
00132     usage = '''\
00133 Usage: %s [-v] [-f] [-s sev] [-i path]* [path]
00134 
00135 Options:  -v       verbose (print all checked file names)
00136           -f       enable checkers that yield many false positives
00137           -s sev   only show problems with severity >= sev
00138           -i path  ignore subdir or file path
00139 ''' % argv[0]
00140     try:
00141         gopts, args = getopt.getopt(argv[1:], 'vfs:i:')
00142     except getopt.GetoptError:
00143         print(usage)
00144         return 2
00145 
00146     verbose = False
00147     severity = 1
00148     ignore = []
00149     falsepos = False
00150     for opt, val in gopts:
00151         if opt == '-v':
00152             verbose = True
00153         elif opt == '-f':
00154             falsepos = True
00155         elif opt == '-s':
00156             severity = int(val)
00157         elif opt == '-i':
00158             ignore.append(abspath(val))
00159 
00160     if len(args) == 0:
00161         path = '.'
00162     elif len(args) == 1:
00163         path = args[0]
00164     else:
00165         print(usage)
00166         return 2
00167 
00168     if not exists(path):
00169         print('Error: path %s does not exist' % path)
00170         return 2
00171 
00172     count = defaultdict(int)
00173 
00174     for root, dirs, files in os.walk(path):
00175         # ignore subdirs controlled by svn
00176         if '.svn' in dirs:
00177             dirs.remove('.svn')
00178 
00179         # ignore subdirs in ignore list
00180         if abspath(root) in ignore:
00181             del dirs[:]
00182             continue
00183 
00184         for fn in files:
00185             fn = join(root, fn)
00186             if fn[:2] == './':
00187                 fn = fn[2:]
00188 
00189             # ignore files in ignore list
00190             if abspath(fn) in ignore:
00191                 continue
00192 
00193             ext = splitext(fn)[1]
00194             checkerlist = checkers.get(ext, None)
00195             if not checkerlist:
00196                 continue
00197 
00198             if verbose:
00199                 print('Checking %s...' % fn)
00200 
00201             try:
00202                 with open(fn, 'r') as f:
00203                     lines = list(f)
00204             except (IOError, OSError) as err:
00205                 print('%s: cannot open: %s' % (fn, err))
00206                 count[4] += 1
00207                 continue
00208 
00209             for checker in checkerlist:
00210                 if checker.falsepositives and not falsepos:
00211                     continue
00212                 csev = checker.severity
00213                 if csev >= severity:
00214                     for lno, msg in checker(fn, lines):
00215                         print('[%d] %s:%d: %s' % (csev, fn, lno, msg))
00216                         count[csev] += 1
00217     if verbose:
00218         print()
00219     if not count:
00220         if severity > 1:
00221             print('No problems with severity >= %d found.' % severity)
00222         else:
00223             print('No problems found.')
00224     else:
00225         for severity in sorted(count):
00226             number = count[severity]
00227             print('%d problem%s with severity %d found.' %
00228                   (number, number > 1 and 's' or '', severity))
00229     return int(bool(count))
00230 

Here is the call graph for this function:


Variable Documentation

Definition at line 44 of file rstlint.py.

dictionary rstlint.checker_props = {'severity': 1, 'falsepositives': False}

Definition at line 52 of file rstlint.py.

Definition at line 50 of file rstlint.py.

tuple rstlint.default_role_re = re.compile(r'(^| )`\w([^`]*?\w)?`($| )')

Definition at line 46 of file rstlint.py.

Initial value:
00001 [
00002     # standard docutils ones
00003     'admonition', 'attention', 'caution', 'class', 'compound', 'container',
00004     'contents', 'csv-table', 'danger', 'date', 'default-role', 'epigraph',
00005     'error', 'figure', 'footer', 'header', 'highlights', 'hint', 'image',
00006     'important', 'include', 'line-block', 'list-table', 'meta', 'note',
00007     'parsed-literal', 'pull-quote', 'raw', 'replace',
00008     'restructuredtext-test-directive', 'role', 'rubric', 'sectnum', 'sidebar',
00009     'table', 'target-notes', 'tip', 'title', 'topic', 'unicode', 'warning',
00010     # Sphinx custom ones
00011     'acks', 'attribute', 'autoattribute', 'autoclass', 'autodata',
00012     'autoexception', 'autofunction', 'automethod', 'automodule', 'centered',
00013     'cfunction', 'class', 'classmethod', 'cmacro', 'cmdoption', 'cmember',
00014     'code-block', 'confval', 'cssclass', 'ctype', 'currentmodule', 'cvar',
00015     'data', 'deprecated', 'describe', 'directive', 'doctest', 'envvar', 'event',
00016     'exception', 'function', 'glossary', 'highlight', 'highlightlang', 'index',
00017     'literalinclude', 'method', 'module', 'moduleauthor', 'productionlist',
00018     'program', 'role', 'sectionauthor', 'seealso', 'sourcecode', 'staticmethod',
00019     'tabularcolumns', 'testcode', 'testoutput', 'testsetup', 'toctree', 'todo',
00020     'todolist', 'versionadded', 'versionchanged'
00021 ]

Definition at line 22 of file rstlint.py.

tuple rstlint.leaked_markup_re = re.compile(r'[a-z]::[^=]|:[a-z]+:|`|\.\.\s*\w+:')

Definition at line 47 of file rstlint.py.

tuple rstlint.seems_directive_re = re.compile(r'\.\. %s([^a-z:]|:(?!:))' % all_directives)

Definition at line 45 of file rstlint.py.