Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes
MoinMoin._tests.pep8.Checker Class Reference

List of all members.

Public Member Functions

def __init__
def readline
def readline_check_physical
def run_check
def check_physical
def build_tokens_line
def check_logical
def check_all
def report_error

Public Attributes

 filename
 lines
 physical_checks
 logical_checks
 physical_line
 indent_char
 mapping
 logical_line
 previous_indent_level
 indent_level
 previous_logical
 file_errors
 line_number
 blank_lines
 tokens

Detailed Description

Load a Python source file, tokenize it, check coding style.

Definition at line 544 of file pep8.py.


Constructor & Destructor Documentation

def MoinMoin._tests.pep8.Checker.__init__ (   self,
  filename 
)

Definition at line 549 of file pep8.py.

00549 
00550     def __init__(self, filename):
00551         self.filename = filename
00552         self.lines = file(filename, 'rb').readlines()
00553         self.physical_checks = find_checks('physical_line')
00554         self.logical_checks = find_checks('logical_line')
00555         options.counters['physical lines'] = \
00556             options.counters.get('physical lines', 0) + len(self.lines)


Member Function Documentation

Build a logical line from tokens.

Definition at line 598 of file pep8.py.

00598 
00599     def build_tokens_line(self):
00600         """
00601         Build a logical line from tokens.
00602         """
00603         self.mapping = []
00604         logical = []
00605         length = 0
00606         previous = None
00607         for token in self.tokens:
00608             token_type, text = token[0:2]
00609             if token_type in (tokenize.COMMENT, tokenize.NL,
00610                               tokenize.INDENT, tokenize.DEDENT,
00611                               tokenize.NEWLINE):
00612                 continue
00613             if token_type == tokenize.STRING:
00614                 text = mute_string(text)
00615             if previous:
00616                 end_line, end = previous[3]
00617                 start_line, start = token[2]
00618                 if end_line != start_line: # different row
00619                     if self.lines[end_line - 1][end - 1] not in '{[(':
00620                         logical.append(' ')
00621                         length += 1
00622                 elif end != start: # different column
00623                     fill = self.lines[end_line - 1][end:start]
00624                     logical.append(fill)
00625                     length += len(fill)
00626             self.mapping.append((length, token))
00627             logical.append(text)
00628             length += len(text)
00629             previous = token
00630         self.logical_line = ''.join(logical)
00631         assert self.logical_line.lstrip() == self.logical_line
00632         assert self.logical_line.rstrip() == self.logical_line

Run all checks on the input file.

Definition at line 664 of file pep8.py.

00664 
00665     def check_all(self):
00666         """
00667         Run all checks on the input file.
00668         """
00669         self.file_errors = 0
00670         self.line_number = 0
00671         self.indent_char = None
00672         self.indent_level = 0
00673         self.previous_logical = ''
00674         self.blank_lines = 0
00675         self.tokens = []
00676         parens = 0
00677         for token in tokenize.generate_tokens(self.readline_check_physical):
00678             # print tokenize.tok_name[token[0]], repr(token)
00679             self.tokens.append(token)
00680             token_type, text = token[0:2]
00681             if token_type == tokenize.OP and text in '([{':
00682                 parens += 1
00683             if token_type == tokenize.OP and text in '}])':
00684                 parens -= 1
00685             if token_type == tokenize.NEWLINE and not parens:
00686                 self.check_logical()
00687                 self.blank_lines = 0
00688                 self.tokens = []
00689             if token_type == tokenize.NL and not parens:
00690                 self.blank_lines += 1
00691                 self.tokens = []
00692             if token_type == tokenize.COMMENT:
00693                 self.blank_lines = 0
00694         return self.file_errors

Build a line from tokens and run all logical checks on it.

Definition at line 633 of file pep8.py.

00633 
00634     def check_logical(self):
00635         """
00636         Build a line from tokens and run all logical checks on it.
00637         """
00638         options.counters['logical lines'] = \
00639             options.counters.get('logical lines', 0) + 1
00640         self.build_tokens_line()
00641         first_line = self.lines[self.mapping[0][1][2][0] - 1]
00642         indent = first_line[:self.mapping[0][1][2][1]]
00643         self.previous_indent_level = self.indent_level
00644         self.indent_level = expand_indent(indent)
00645         if options.verbose >= 2:
00646             print self.logical_line[:80].rstrip()
00647         for name, check, argument_names in self.logical_checks:
00648             if options.verbose >= 3:
00649                 print '   ', name
00650             result = self.run_check(check, argument_names)
00651             if result is not None:
00652                 offset, text = result
00653                 if type(offset) is tuple:
00654                     original_number, original_offset = offset
00655                 else:
00656                     for token_offset, token in self.mapping:
00657                         if offset >= token_offset:
00658                             original_number = token[2][0]
00659                             original_offset = (token[2][1]
00660                                                + offset - token_offset)
00661                 self.report_error(original_number, original_offset,
00662                                   text, check)
00663         self.previous_logical = self.logical_line

Here is the call graph for this function:

def MoinMoin._tests.pep8.Checker.check_physical (   self,
  line 
)
Run all physical checks on a raw input line.

Definition at line 585 of file pep8.py.

00585 
00586     def check_physical(self, line):
00587         """
00588         Run all physical checks on a raw input line.
00589         """
00590         self.physical_line = line
00591         if self.indent_char is None and len(line) and line[0] in ' \t':
00592             self.indent_char = line[0]
00593         for name, check, argument_names in self.physical_checks:
00594             result = self.run_check(check, argument_names)
00595             if result is not None:
00596                 offset, text = result
00597                 self.report_error(self.line_number, offset, text, check)

Get the next line from the input buffer.

Definition at line 557 of file pep8.py.

00557 
00558     def readline(self):
00559         """
00560         Get the next line from the input buffer.
00561         """
00562         self.line_number += 1
00563         if self.line_number > len(self.lines):
00564             return ''
00565         return self.lines[self.line_number - 1]

Here is the caller graph for this function:

Check and return the next physical line. This method can be
used to feed tokenize.generate_tokens.

Definition at line 566 of file pep8.py.

00566 
00567     def readline_check_physical(self):
00568         """
00569         Check and return the next physical line. This method can be
00570         used to feed tokenize.generate_tokens.
00571         """
00572         line = self.readline()
00573         if line:
00574             self.check_physical(line)
00575         return line

Here is the call graph for this function:

def MoinMoin._tests.pep8.Checker.report_error (   self,
  line_number,
  offset,
  text,
  check 
)
Report an error, according to options.

Definition at line 695 of file pep8.py.

00695 
00696     def report_error(self, line_number, offset, text, check):
00697         """
00698         Report an error, according to options.
00699         """
00700         if options.quiet == 1 and not self.file_errors:
00701             message(self.filename)
00702         code = text[:4]
00703         if ignore_code(code):
00704             return
00705         self.file_errors += 1
00706         options.counters[code] = options.counters.get(code, 0) + 1
00707         options.messages[code] = text[5:]
00708         if options.quiet:
00709             return
00710         if options.testsuite:
00711             base = os.path.basename(self.filename)[:4]
00712             if base == code:
00713                 return
00714             if base[0] == 'E' and code[0] == 'W':
00715                 return
00716         if options.counters[code] == 1 or options.repeat:
00717             message("%s:%s:%d: %s" %
00718                     (self.filename, line_number, offset + 1, text))
00719             if options.show_source:
00720                 line = self.lines[line_number - 1]
00721                 message(line.rstrip())
00722                 message(' ' * offset + '^')
00723             if options.show_pep8:
00724                 message(check.__doc__.lstrip('\n').rstrip())
00725 

Here is the call graph for this function:

def MoinMoin._tests.pep8.Checker.run_check (   self,
  check,
  argument_names 
)
Run a check plugin.

Definition at line 576 of file pep8.py.

00576 
00577     def run_check(self, check, argument_names):
00578         """
00579         Run a check plugin.
00580         """
00581         arguments = []
00582         for name in argument_names:
00583             arguments.append(getattr(self, name))
00584         return check(*arguments)


Member Data Documentation

Definition at line 673 of file pep8.py.

Definition at line 668 of file pep8.py.

Definition at line 550 of file pep8.py.

Definition at line 591 of file pep8.py.

Definition at line 643 of file pep8.py.

Definition at line 669 of file pep8.py.

Definition at line 551 of file pep8.py.

Definition at line 553 of file pep8.py.

Definition at line 629 of file pep8.py.

Definition at line 602 of file pep8.py.

Definition at line 552 of file pep8.py.

Definition at line 589 of file pep8.py.

Definition at line 642 of file pep8.py.

Definition at line 662 of file pep8.py.

Definition at line 674 of file pep8.py.


The documentation for this class was generated from the following file: