Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
reindent.Reindenter Class Reference

List of all members.

Public Member Functions

def __init__
def run
def write
def getline
def tokeneater

Public Attributes

 find_stmt
 level
 raw
 lines
 index
 stats
 newlines
 after

Detailed Description

Definition at line 162 of file reindent.py.


Constructor & Destructor Documentation

def reindent.Reindenter.__init__ (   self,
  f 
)

Definition at line 164 of file reindent.py.

00164 
00165     def __init__(self, f):
00166         self.find_stmt = 1  # next token begins a fresh stmt?
00167         self.level = 0      # current indent level
00168 
00169         # Raw file lines.
00170         self.raw = f.readlines()
00171 
00172         # File lines, rstripped & tab-expanded.  Dummy at start is so
00173         # that we can use tokenize's 1-based line numbering easily.
00174         # Note that a line is all-blank iff it's "\n".
00175         self.lines = [_rstrip(line).expandtabs() + "\n"
00176                       for line in self.raw]
00177         self.lines.insert(0, None)
00178         self.index = 1  # index into self.lines of next line
00179 
00180         # List of (lineno, indentlevel) pairs, one for each stmt and
00181         # comment line.  indentlevel is -1 for comment lines, as a
00182         # signal that tokenize doesn't know what to do about them;
00183         # indeed, they're our headache!
00184         self.stats = []
00185 
00186         # Save the newlines found in the file so they can be used to
00187         #  create output without mutating the newlines.
00188         self.newlines = f.newlines


Member Function Documentation

Definition at line 264 of file reindent.py.

00264 
00265     def getline(self):
00266         if self.index >= len(self.lines):
00267             line = ""
00268         else:
00269             line = self.lines[self.index]
00270             self.index += 1
00271         return line

Here is the call graph for this function:

Here is the caller graph for this function:

def reindent.Reindenter.run (   self)

Definition at line 189 of file reindent.py.

00189 
00190     def run(self):
00191         tokens = tokenize.generate_tokens(self.getline)
00192         for _token in tokens:
00193             self.tokeneater(*_token)
00194         # Remove trailing empty lines.
00195         lines = self.lines
00196         while lines and lines[-1] == "\n":
00197             lines.pop()
00198         # Sentinel.
00199         stats = self.stats
00200         stats.append((len(lines), 0))
00201         # Map count of leading spaces to # we want.
00202         have2want = {}
00203         # Program after transformation.
00204         after = self.after = []
00205         # Copy over initial empty lines -- there's nothing to do until
00206         # we see a line with *something* on it.
00207         i = stats[0][0]
00208         after.extend(lines[1:i])
00209         for i in range(len(stats) - 1):
00210             thisstmt, thislevel = stats[i]
00211             nextstmt = stats[i + 1][0]
00212             have = getlspace(lines[thisstmt])
00213             want = thislevel * 4
00214             if want < 0:
00215                 # A comment line.
00216                 if have:
00217                     # An indented comment line.  If we saw the same
00218                     # indentation before, reuse what it most recently
00219                     # mapped to.
00220                     want = have2want.get(have, -1)
00221                     if want < 0:
00222                         # Then it probably belongs to the next real stmt.
00223                         for j in range(i + 1, len(stats) - 1):
00224                             jline, jlevel = stats[j]
00225                             if jlevel >= 0:
00226                                 if have == getlspace(lines[jline]):
00227                                     want = jlevel * 4
00228                                 break
00229                     if want < 0:           # Maybe it's a hanging
00230                                            # comment like this one,
00231                         # in which case we should shift it like its base
00232                         # line got shifted.
00233                         for j in range(i - 1, -1, -1):
00234                             jline, jlevel = stats[j]
00235                             if jlevel >= 0:
00236                                 want = have + (getlspace(after[jline - 1]) -
00237                                                getlspace(lines[jline]))
00238                                 break
00239                     if want < 0:
00240                         # Still no luck -- leave it alone.
00241                         want = have
00242                 else:
00243                     want = 0
00244             assert want >= 0
00245             have2want[have] = want
00246             diff = want - have
00247             if diff == 0 or have == 0:
00248                 after.extend(lines[thisstmt:nextstmt])
00249             else:
00250                 for line in lines[thisstmt:nextstmt]:
00251                     if diff > 0:
00252                         if line == "\n":
00253                             after.append(line)
00254                         else:
00255                             after.append(" " * diff + line)
00256                     else:
00257                         remove = min(getlspace(line), -diff)
00258                         after.append(line[remove:])
00259         return self.raw != self.after

Here is the call graph for this function:

def reindent.Reindenter.tokeneater (   self,
  type,
  token,
  slinecol,
  end,
  line,
  INDENT = tokenize.INDENT,
  DEDENT = tokenize.DEDENT,
  NEWLINE = tokenize.NEWLINE,
  COMMENT = tokenize.COMMENT,
  NL = tokenize.NL 
)

Definition at line 278 of file reindent.py.

00278 
00279                    NL=tokenize.NL):
00280 
00281         if type == NEWLINE:
00282             # A program statement, or ENDMARKER, will eventually follow,
00283             # after some (possibly empty) run of tokens of the form
00284             #     (NL | COMMENT)* (INDENT | DEDENT+)?
00285             self.find_stmt = 1
00286 
00287         elif type == INDENT:
00288             self.find_stmt = 1
00289             self.level += 1
00290 
00291         elif type == DEDENT:
00292             self.find_stmt = 1
00293             self.level -= 1
00294 
00295         elif type == COMMENT:
00296             if self.find_stmt:
00297                 self.stats.append((slinecol[0], -1))
00298                 # but we're still looking for a new stmt, so leave
00299                 # find_stmt alone
00300 
00301         elif type == NL:
00302             pass
00303 
00304         elif self.find_stmt:
00305             # This is the first "real token" following a NEWLINE, so it
00306             # must be the first token of the next program statement, or an
00307             # ENDMARKER.
00308             self.find_stmt = 0
00309             if line:   # not endmarker
00310                 self.stats.append((slinecol[0], self.level))
00311 
00312 
# Count number of leading blanks.

Here is the caller graph for this function:

def reindent.Reindenter.write (   self,
  f 
)

Definition at line 260 of file reindent.py.

00260 
00261     def write(self, f):
00262         f.writelines(self.after)


Member Data Documentation

Definition at line 203 of file reindent.py.

Definition at line 165 of file reindent.py.

Definition at line 177 of file reindent.py.

Definition at line 166 of file reindent.py.

Definition at line 174 of file reindent.py.

Definition at line 187 of file reindent.py.

Definition at line 169 of file reindent.py.

Definition at line 183 of file reindent.py.


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