Back to index

python3.2  3.2.2
Classes | Functions | Variables
lib2to3.refactor Namespace Reference

Classes

class  _EveryNode
class  FixerError
class  RefactoringTool
class  MultiprocessingUnsupported
class  MultiprocessRefactoringTool

Functions

def get_all_fix_names
def _get_head_types
def _get_headnode_dict
def get_fixers_from_package
def _identity
def _from_system_newlines
def _to_system_newlines
def _detect_future_features

Variables

string __author__ = "Guido van Rossum <guido@python.org>"
 _open_with_encoding = codecs.open
 _from_system_newlines = _identity
 _to_system_newlines = _identity

Function Documentation

def lib2to3.refactor._detect_future_features (   source) [private]

Definition at line 130 of file refactor.py.

00130 
00131 def _detect_future_features(source):
00132     have_docstring = False
00133     gen = tokenize.generate_tokens(io.StringIO(source).readline)
00134     def advance():
00135         tok = next(gen)
00136         return tok[0], tok[1]
00137     ignore = frozenset((token.NEWLINE, tokenize.NL, token.COMMENT))
00138     features = set()
00139     try:
00140         while True:
00141             tp, value = advance()
00142             if tp in ignore:
00143                 continue
00144             elif tp == token.STRING:
00145                 if have_docstring:
00146                     break
00147                 have_docstring = True
00148             elif tp == token.NAME and value == "from":
00149                 tp, value = advance()
00150                 if tp != token.NAME or value != "__future__":
00151                     break
00152                 tp, value = advance()
00153                 if tp != token.NAME or value != "import":
00154                     break
00155                 tp, value = advance()
00156                 if tp == token.OP and value == "(":
00157                     tp, value = advance()
00158                 while tp == token.NAME:
00159                     features.add(value)
00160                     tp, value = advance()
00161                     if tp != token.OP or value != ",":
00162                         break
00163                     tp, value = advance()
00164             else:
00165                 break
00166     except StopIteration:
00167         pass
00168     return frozenset(features)
00169 

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.refactor._from_system_newlines (   input) [private]

Definition at line 117 of file refactor.py.

00117 
00118     def _from_system_newlines(input):
        return input.replace("\r\n", "\n")
def lib2to3.refactor._get_head_types (   pat) [private]
Accepts a pytree Pattern Node and returns a set
    of the pattern types which will match first. 

Definition at line 50 of file refactor.py.

00050 
00051 def _get_head_types(pat):
00052     """ Accepts a pytree Pattern Node and returns a set
00053         of the pattern types which will match first. """
00054 
00055     if isinstance(pat, (pytree.NodePattern, pytree.LeafPattern)):
00056         # NodePatters must either have no type and no content
00057         #   or a type and content -- so they don't get any farther
00058         # Always return leafs
00059         if pat.type is None:
00060             raise _EveryNode
00061         return set([pat.type])
00062 
00063     if isinstance(pat, pytree.NegatedPattern):
00064         if pat.content:
00065             return _get_head_types(pat.content)
00066         raise _EveryNode # Negated Patterns don't have a type
00067 
00068     if isinstance(pat, pytree.WildcardPattern):
00069         # Recurse on each node in content
00070         r = set()
00071         for p in pat.content:
00072             for x in p:
00073                 r.update(_get_head_types(x))
00074         return r
00075 
00076     raise Exception("Oh no! I don't understand pattern %s" %(pat))
00077 

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.refactor._get_headnode_dict (   fixer_list) [private]
Accepts a list of fixers and returns a dictionary
    of head node type --> fixer list.  

Definition at line 78 of file refactor.py.

00078 
00079 def _get_headnode_dict(fixer_list):
00080     """ Accepts a list of fixers and returns a dictionary
00081         of head node type --> fixer list.  """
00082     head_nodes = collections.defaultdict(list)
00083     every = []
00084     for fixer in fixer_list:
00085         if fixer.pattern:
00086             try:
00087                 heads = _get_head_types(fixer.pattern)
00088             except _EveryNode:
00089                 every.append(fixer)
00090             else:
00091                 for node_type in heads:
00092                     head_nodes[node_type].append(fixer)
00093         else:
00094             if fixer._accept_type is not None:
00095                 head_nodes[fixer._accept_type].append(fixer)
00096             else:
00097                 every.append(fixer)
00098     for node_type in chain(pygram.python_grammar.symbol2number.values(),
00099                            pygram.python_grammar.tokens):
00100         head_nodes[node_type].extend(every)
00101     return dict(head_nodes)
00102 

Here is the call graph for this function:

def lib2to3.refactor._identity (   obj) [private]

Definition at line 110 of file refactor.py.

00110 
00111 def _identity(obj):
00112     return obj

def lib2to3.refactor._to_system_newlines (   input) [private]

Definition at line 119 of file refactor.py.

00119 
00120     def _to_system_newlines(input):
00121         if os.linesep != "\n":
00122             return input.replace("\n", os.linesep)
00123         else:
00124             return input
else:
def lib2to3.refactor.get_all_fix_names (   fixer_pkg,
  remove_prefix = True 
)
Return a sorted list of all available fix names in the given package.

Definition at line 33 of file refactor.py.

00033 
00034 def get_all_fix_names(fixer_pkg, remove_prefix=True):
00035     """Return a sorted list of all available fix names in the given package."""
00036     pkg = __import__(fixer_pkg, [], [], ["*"])
00037     fixer_dir = os.path.dirname(pkg.__file__)
00038     fix_names = []
00039     for name in sorted(os.listdir(fixer_dir)):
00040         if name.startswith("fix_") and name.endswith(".py"):
00041             if remove_prefix:
00042                 name = name[4:]
00043             fix_names.append(name[:-3])
00044     return fix_names
00045 

Here is the call graph for this function:

Here is the caller graph for this function:

Return the fully qualified names for fixers in the package pkg_name.

Definition at line 103 of file refactor.py.

00103 
00104 def get_fixers_from_package(pkg_name):
00105     """
00106     Return the fully qualified names for fixers in the package pkg_name.
00107     """
00108     return [pkg_name + "." + fix_name
00109             for fix_name in get_all_fix_names(pkg_name, False)]

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

string lib2to3.refactor.__author__ = "Guido van Rossum <guido@python.org>"

Definition at line 13 of file refactor.py.

Definition at line 126 of file refactor.py.

Definition at line 115 of file refactor.py.

Definition at line 127 of file refactor.py.