Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
cleanfuture.FutureFinder Class Reference

List of all members.

Public Member Functions

def __init__
def getline
def run
def gettherest
def write

Public Attributes

 f
 fname
 ateof
 lines
 changed
 therest

Detailed Description

Definition at line 134 of file cleanfuture.py.


Constructor & Destructor Documentation

def cleanfuture.FutureFinder.__init__ (   self,
  f,
  fname 
)

Definition at line 136 of file cleanfuture.py.

00136 
00137     def __init__(self, f, fname):
00138         self.f = f
00139         self.fname = fname
00140         self.ateof = 0
00141         self.lines = [] # raw file lines
00142 
00143         # List of (start_index, end_index, new_line) triples.
00144         self.changed = []

Here is the caller graph for this function:


Member Function Documentation

Definition at line 146 of file cleanfuture.py.

00146 
00147     def getline(self):
00148         if self.ateof:
00149             return ""
00150         line = self.f.readline()
00151         if line == "":
00152             self.ateof = 1
00153         else:
00154             self.lines.append(line)
00155         return line

Here is the caller graph for this function:

Definition at line 251 of file cleanfuture.py.

00251 
00252     def gettherest(self):
00253         if self.ateof:
00254             self.therest = ''
00255         else:
00256             self.therest = self.f.read()

Definition at line 156 of file cleanfuture.py.

00156 
00157     def run(self):
00158         STRING = tokenize.STRING
00159         NL = tokenize.NL
00160         NEWLINE = tokenize.NEWLINE
00161         COMMENT = tokenize.COMMENT
00162         NAME = tokenize.NAME
00163         OP = tokenize.OP
00164 
00165         changed = self.changed
00166         get = tokenize.generate_tokens(self.getline).__next__
00167         type, token, (srow, scol), (erow, ecol), line = get()
00168 
00169         # Chew up initial comments and blank lines (if any).
00170         while type in (COMMENT, NL, NEWLINE):
00171             type, token, (srow, scol), (erow, ecol), line = get()
00172 
00173         # Chew up docstring (if any -- and it may be implicitly catenated!).
00174         while type is STRING:
00175             type, token, (srow, scol), (erow, ecol), line = get()
00176 
00177         # Analyze the future stmts.
00178         while 1:
00179             # Chew up comments and blank lines (if any).
00180             while type in (COMMENT, NL, NEWLINE):
00181                 type, token, (srow, scol), (erow, ecol), line = get()
00182 
00183             if not (type is NAME and token == "from"):
00184                 break
00185             startline = srow - 1    # tokenize is one-based
00186             type, token, (srow, scol), (erow, ecol), line = get()
00187 
00188             if not (type is NAME and token == "__future__"):
00189                 break
00190             type, token, (srow, scol), (erow, ecol), line = get()
00191 
00192             if not (type is NAME and token == "import"):
00193                 break
00194             type, token, (srow, scol), (erow, ecol), line = get()
00195 
00196             # Get the list of features.
00197             features = []
00198             while type is NAME:
00199                 features.append(token)
00200                 type, token, (srow, scol), (erow, ecol), line = get()
00201 
00202                 if not (type is OP and token == ','):
00203                     break
00204                 type, token, (srow, scol), (erow, ecol), line = get()
00205 
00206             # A trailing comment?
00207             comment = None
00208             if type is COMMENT:
00209                 comment = token
00210                 type, token, (srow, scol), (erow, ecol), line = get()
00211 
00212             if type is not NEWLINE:
00213                 errprint("Skipping file %r; can't parse line %d:\n%s" %
00214                          (self.fname, srow, line))
00215                 return []
00216 
00217             endline = srow - 1
00218 
00219             # Check for obsolete features.
00220             okfeatures = []
00221             for f in features:
00222                 object = getattr(__future__, f, None)
00223                 if object is None:
00224                     # A feature we don't know about yet -- leave it in.
00225                     # They'll get a compile-time error when they compile
00226                     # this program, but that's not our job to sort out.
00227                     okfeatures.append(f)
00228                 else:
00229                     released = object.getMandatoryRelease()
00230                     if released is None or released <= sys.version_info:
00231                         # Withdrawn or obsolete.
00232                         pass
00233                     else:
00234                         okfeatures.append(f)
00235 
00236             # Rewrite the line if at least one future-feature is obsolete.
00237             if len(okfeatures) < len(features):
00238                 if len(okfeatures) == 0:
00239                     line = None
00240                 else:
00241                     line = "from __future__ import "
00242                     line += ', '.join(okfeatures)
00243                     if comment is not None:
00244                         line += ' ' + comment
00245                     line += '\n'
00246                 changed.append((startline, endline, line))
00247 
00248             # Loop back for more future statements.
00249 
00250         return changed

Here is the call graph for this function:

def cleanfuture.FutureFinder.write (   self,
  f 
)

Definition at line 257 of file cleanfuture.py.

00257 
00258     def write(self, f):
00259         changed = self.changed
00260         assert changed
00261         # Prevent calling this again.
00262         self.changed = []
00263         # Apply changes in reverse order.
00264         changed.reverse()
00265         for s, e, line in changed:
00266             if line is None:
00267                 # pure deletion
00268                 del self.lines[s:e+1]
00269             else:
00270                 self.lines[s:e+1] = [line]
00271         f.writelines(self.lines)
00272         # Copy over the remainder of the file.
00273         if self.therest:
00274             f.write(self.therest)

Here is the call graph for this function:


Member Data Documentation

Definition at line 139 of file cleanfuture.py.

Definition at line 143 of file cleanfuture.py.

Definition at line 137 of file cleanfuture.py.

Definition at line 138 of file cleanfuture.py.

Definition at line 140 of file cleanfuture.py.

Definition at line 253 of file cleanfuture.py.


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