Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Attributes
pindent.PythonIndenter Class Reference

List of all members.

Public Member Functions

def __init__
def write
def readline
def error
def getline
def putline
def reformat
def delete
def complete

Public Attributes

 fpi
 fpo
 indentsize
 tabsize
 lineno
 expandtabs
 kwprog
 endprog
 wsprog

Private Attributes

 _write

Detailed Description

Definition at line 96 of file pindent.py.


Constructor & Destructor Documentation

def pindent.PythonIndenter.__init__ (   self,
  fpi = sys.stdin,
  fpo = sys.stdout,
  indentsize = STEPSIZE,
  tabsize = TABSIZE,
  expandtabs = EXPANDTABS 
)

Definition at line 99 of file pindent.py.

00099 
00100                  indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
00101         self.fpi = fpi
00102         self.fpo = fpo
00103         self.indentsize = indentsize
00104         self.tabsize = tabsize
00105         self.lineno = 0
00106         self.expandtabs = expandtabs
00107         self._write = fpo.write
00108         self.kwprog = re.compile(
00109                 r'^\s*(?P<kw>[a-z]+)'
00110                 r'(\s+(?P<id>[a-zA-Z_]\w*))?'
00111                 r'[^\w]')
00112         self.endprog = re.compile(
00113                 r'^\s*#?\s*end\s+(?P<kw>[a-z]+)'
00114                 r'(\s+(?P<id>[a-zA-Z_]\w*))?'
00115                 r'[^\w]')
        self.wsprog = re.compile(r'^[ \t]*')

Member Function Documentation

Definition at line 236 of file pindent.py.

00236 
00237     def complete(self):
00238         self.indentsize = 1
00239         stack = []
00240         todo = []
00241         thisid = ''
00242         current, firstkw, lastkw, topid = 0, '', '', ''
00243         while 1:
00244             line = self.getline()
00245             i = 0
00246             m = self.wsprog.match(line)
00247             if m: i = m.end()
00248             # end if
00249             m = self.endprog.match(line)
00250             if m:
00251                 thiskw = 'end'
00252                 endkw = m.group('kw')
00253                 thisid = m.group('id')
00254             else:
00255                 m = self.kwprog.match(line)
00256                 if m:
00257                     thiskw = m.group('kw')
00258                     if thiskw not in next:
00259                         thiskw = ''
00260                     # end if
00261                     if thiskw in ('def', 'class'):
00262                         thisid = m.group('id')
00263                     else:
00264                         thisid = ''
00265                     # end if
00266                 elif line[i:i+1] in ('\n', '#'):
00267                     todo.append(line)
00268                     continue
00269                 else:
00270                     thiskw = ''
00271                 # end if
00272             # end if
00273             indent = len(line[:i].expandtabs(self.tabsize))
00274             while indent < current:
00275                 if firstkw:
00276                     if topid:
00277                         s = '# end %s %s\n' % (
00278                                 firstkw, topid)
00279                     else:
00280                         s = '# end %s\n' % firstkw
00281                     # end if
00282                     self.putline(s, current)
00283                     firstkw = lastkw = ''
00284                 # end if
00285                 current, firstkw, lastkw, topid = stack[-1]
00286                 del stack[-1]
00287             # end while
00288             if indent == current and firstkw:
00289                 if thiskw == 'end':
00290                     if endkw != firstkw:
00291                         self.error('mismatched end')
00292                     # end if
00293                     firstkw = lastkw = ''
00294                 elif not thiskw or thiskw in start:
00295                     if topid:
00296                         s = '# end %s %s\n' % (
00297                                 firstkw, topid)
00298                     else:
00299                         s = '# end %s\n' % firstkw
00300                     # end if
00301                     self.putline(s, current)
00302                     firstkw = lastkw = topid = ''
00303                 # end if
00304             # end if
00305             if indent > current:
00306                 stack.append((current, firstkw, lastkw, topid))
00307                 if thiskw and thiskw not in start:
00308                     # error
00309                     thiskw = ''
00310                 # end if
00311                 current, firstkw, lastkw, topid = \
00312                          indent, thiskw, thiskw, thisid
00313             # end if
00314             if thiskw:
00315                 if thiskw in start:
00316                     firstkw = lastkw = thiskw
00317                     topid = thisid
00318                 else:
00319                     lastkw = thiskw
00320                 # end if
00321             # end if
00322             for l in todo: self.write(l)
00323             # end for
00324             todo = []
00325             if not line: break
00326             # end if
00327             self.write(line)
        # end while

Definition at line 208 of file pindent.py.

00208 
00209     def delete(self):
00210         begin_counter = 0
00211         end_counter = 0
00212         while 1:
00213             line = self.getline()
00214             if not line: break      # EOF
00215             # end if
00216             m = self.endprog.match(line)
00217             if m:
00218                 end_counter = end_counter + 1
00219                 continue
00220             # end if
00221             m = self.kwprog.match(line)
00222             if m:
00223                 kw = m.group('kw')
00224                 if kw in start:
00225                     begin_counter = begin_counter + 1
00226                 # end if
00227             # end if
00228             self.putline(line)
00229         # end while
00230         if begin_counter - end_counter < 0:
00231             sys.stderr.write('Warning: input contained more end tags than expected\n')
00232         elif begin_counter - end_counter > 0:
00233             sys.stderr.write('Warning: input contained less end tags than expected\n')
        # end if

Here is the call graph for this function:

def pindent.PythonIndenter.error (   self,
  fmt,
  args 
)

Definition at line 133 of file pindent.py.

00133 
00134     def error(self, fmt, *args):
00135         if args: fmt = fmt % args
00136         # end if
00137         sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt))
        self.write('### %s ###\n' % fmt)

Here is the caller graph for this function:

Definition at line 140 of file pindent.py.

00140 
00141     def getline(self):
00142         line = self.readline()
00143         while line[-2:] == '\\\n':
00144             line2 = self.readline()
00145             if not line2: break
00146             # end if
00147             line = line + line2
00148         # end while
        return line

Here is the call graph for this function:

Here is the caller graph for this function:

def pindent.PythonIndenter.putline (   self,
  line,
  indent = None 
)

Definition at line 151 of file pindent.py.

00151 
00152     def putline(self, line, indent = None):
00153         if indent is None:
00154             self.write(line)
00155             return
00156         # end if
00157         tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
00158         i = 0
00159         m = self.wsprog.match(line)
00160         if m: i = m.end()
00161         # end if
        self.write('\t'*tabs + ' '*spaces + line[i:])

Here is the caller graph for this function:

Definition at line 126 of file pindent.py.

00126 
00127     def readline(self):
00128         line = self.fpi.readline()
00129         if line: self.lineno = self.lineno + 1
00130         # end if
        return line

Here is the caller graph for this function:

Definition at line 164 of file pindent.py.

00164 
00165     def reformat(self):
00166         stack = []
00167         while 1:
00168             line = self.getline()
00169             if not line: break      # EOF
00170             # end if
00171             m = self.endprog.match(line)
00172             if m:
00173                 kw = 'end'
00174                 kw2 = m.group('kw')
00175                 if not stack:
00176                     self.error('unexpected end')
00177                 elif stack[-1][0] != kw2:
00178                     self.error('unmatched end')
00179                 # end if
00180                 del stack[-1:]
00181                 self.putline(line, len(stack))
00182                 continue
00183             # end if
00184             m = self.kwprog.match(line)
00185             if m:
00186                 kw = m.group('kw')
00187                 if kw in start:
00188                     self.putline(line, len(stack))
00189                     stack.append((kw, kw))
00190                     continue
00191                 # end if
00192                 if kw in next and stack:
00193                     self.putline(line, len(stack)-1)
00194                     kwa, kwb = stack[-1]
00195                     stack[-1] = kwa, kw
00196                     continue
00197                 # end if
00198             # end if
00199             self.putline(line, len(stack))
00200         # end while
00201         if stack:
00202             self.error('unterminated keywords')
00203             for kwa, kwb in stack:
00204                 self.write('\t%s\n' % kwa)
00205             # end for
        # end if
def pindent.PythonIndenter.write (   self,
  line 
)

Definition at line 118 of file pindent.py.

00118 
00119     def write(self, line):
00120         if self.expandtabs:
00121             self._write(line.expandtabs(self.tabsize))
00122         else:
00123             self._write(line)
        # end if

Member Data Documentation

Definition at line 106 of file pindent.py.

Definition at line 111 of file pindent.py.

Definition at line 105 of file pindent.py.

Definition at line 100 of file pindent.py.

Definition at line 101 of file pindent.py.

Definition at line 102 of file pindent.py.

Definition at line 107 of file pindent.py.

Definition at line 104 of file pindent.py.

Definition at line 103 of file pindent.py.

Definition at line 115 of file pindent.py.


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