Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
lib2to3.fixes.fix_urllib.FixUrllib Class Reference
Inheritance diagram for lib2to3.fixes.fix_urllib.FixUrllib:
Inheritance graph
[legend]
Collaboration diagram for lib2to3.fixes.fix_urllib.FixUrllib:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def build_pattern
def transform_import
def transform_member
def transform_dot
def transform
def compile_pattern
def match
def start_tree
def set_filename
def new_name
def log_message
def cannot_convert
def warning
def finish_tree

Public Attributes

 PATTERN
 replace
 numbers
 used_names
 log
 first_log
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 BM_compatible = True
 keep_line_order = True
 mapping = MAPPING
int run_order = 6
 pattern = None
 pattern_tree = None
 options = None
 filename = None
 logger = None
tuple numbers = itertools.count(1)
tuple used_names = set()
string order = "post"
 explicit = False
 syms = pygram.python_symbols

Detailed Description

Definition at line 72 of file fix_urllib.py.


Member Function Documentation

Reimplemented from lib2to3.fixes.fix_imports.FixImports.

Definition at line 74 of file fix_urllib.py.

00074 
00075     def build_pattern(self):
00076         return "|".join(build_pattern())

def lib2to3.fixer_base.BaseFix.cannot_convert (   self,
  node,
  reason = None 
) [inherited]
Warn the user that a given chunk of code is not valid Python 3,
but that it cannot be converted automatically.

First argument is the top-level node for the code in question.
Optional second argument is why it can't be converted.

Definition at line 125 of file fixer_base.py.

00125 
00126     def cannot_convert(self, node, reason=None):
00127         """Warn the user that a given chunk of code is not valid Python 3,
00128         but that it cannot be converted automatically.
00129 
00130         First argument is the top-level node for the code in question.
00131         Optional second argument is why it can't be converted.
00132         """
00133         lineno = node.get_lineno()
00134         for_output = node.clone()
00135         for_output.prefix = ""
00136         msg = "Line %d: could not convert: %s"
00137         self.log_message(msg % (lineno, for_output))
00138         if reason:
00139             self.log_message(reason)

Here is the call graph for this function:

Here is the caller graph for this function:

Compiles self.PATTERN into self.pattern.

Subclass may override if it doesn't want to use
self.{pattern,PATTERN} in .match().

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 99 of file fix_imports.py.

00099 
00100     def compile_pattern(self):
00101         # We override this, so MAPPING can be pragmatically altered and the
00102         # changes will be reflected in PATTERN.
00103         self.PATTERN = self.build_pattern()
00104         super(FixImports, self).compile_pattern()

def lib2to3.fixer_base.BaseFix.finish_tree (   self,
  tree,
  filename 
) [inherited]
Some fixers need to maintain tree-wide state.
This method is called once, at the conclusion of tree fix-up.

tree - the root node of the tree to be processed.
filename - the name of the file the tree came from.

Reimplemented in lib2to3.fixes.fix_xrange.FixXrange.

Definition at line 162 of file fixer_base.py.

00162 
00163     def finish_tree(self, tree, filename):
00164         """Some fixers need to maintain tree-wide state.
00165         This method is called once, at the conclusion of tree fix-up.
00166 
00167         tree - the root node of the tree to be processed.
00168         filename - the name of the file the tree came from.
00169         """
00170         pass
00171 

def lib2to3.fixer_base.BaseFix.log_message (   self,
  message 
) [inherited]

Definition at line 119 of file fixer_base.py.

00119 
00120     def log_message(self, message):
00121         if self.first_log:
00122             self.first_log = False
00123             self.log.append("### In file %s ###" % self.filename)
00124         self.log.append(message)

Here is the caller graph for this function:

def lib2to3.fixes.fix_imports.FixImports.match (   self,
  node 
) [inherited]
Returns match for a given parse tree node.

Should return a true or false object (not necessarily a bool).
It may return a non-empty dict of matching sub-nodes as
returned by a matching pattern.

Subclass may override.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 106 of file fix_imports.py.

00106 
00107     def match(self, node):
00108         match = super(FixImports, self).match
00109         results = match(node)
00110         if results:
00111             # Module usage could be in the trailer of an attribute lookup, so we
00112             # might have nested matches when "bare_with_attr" is present.
00113             if "bare_with_attr" not in results and \
00114                     any(match(obj) for obj in attr_chain(node, "parent")):
00115                 return False
00116             return results
00117         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.new_name (   self,
  template = "xxx_todo_changeme" 
) [inherited]
Return a string suitable for use as an identifier

The new name is guaranteed not to conflict with other identifiers.

Definition at line 108 of file fixer_base.py.

00108 
00109     def new_name(self, template="xxx_todo_changeme"):
00110         """Return a string suitable for use as an identifier
00111 
00112         The new name is guaranteed not to conflict with other identifiers.
00113         """
00114         name = template
00115         while name in self.used_names:
00116             name = template + str(next(self.numbers))
00117         self.used_names.add(name)
00118         return name

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.set_filename (   self,
  filename 
) [inherited]
Set the filename, and a logger derived from it.

The main refactoring tool should call this.

Definition at line 72 of file fixer_base.py.

00072 
00073     def set_filename(self, filename):
00074         """Set the filename, and a logger derived from it.
00075 
00076         The main refactoring tool should call this.
00077         """
00078         self.filename = filename
00079         self.logger = logging.getLogger(filename)

def lib2to3.fixes.fix_imports.FixImports.start_tree (   self,
  tree,
  filename 
) [inherited]
Some fixers need to maintain tree-wide state.
This method is called once, at the start of tree fix-up.

tree - the root node of the tree to be processed.
filename - the name of the file the tree came from.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 118 of file fix_imports.py.

00118 
00119     def start_tree(self, tree, filename):
00120         super(FixImports, self).start_tree(tree, filename)
00121         self.replace = {}

def lib2to3.fixes.fix_urllib.FixUrllib.transform (   self,
  node,
  results 
)
Returns the transformation for a given parse tree node.

Args:
  node: the root of the parse tree that matched the fixer.
  results: a dict mapping symbolic names to part of the match.

Returns:
  None, or a node that is a modified copy of the
  argument node.  The node argument may also be modified in-place to
  effect the same change.

Subclass *must* override.

Reimplemented from lib2to3.fixes.fix_imports.FixImports.

Definition at line 186 of file fix_urllib.py.

00186 
00187     def transform(self, node, results):
00188         if results.get("module"):
00189             self.transform_import(node, results)
00190         elif results.get("mod_member"):
00191             self.transform_member(node, results)
00192         elif results.get("bare_with_attr"):
00193             self.transform_dot(node, results)
00194         # Renaming and star imports are not supported for these modules.
00195         elif results.get("module_star"):
00196             self.cannot_convert(node, "Cannot handle star imports.")
00197         elif results.get("module_as"):
00198             self.cannot_convert(node, "This module is now multiple modules")

Here is the call graph for this function:

def lib2to3.fixes.fix_urllib.FixUrllib.transform_dot (   self,
  node,
  results 
)
Transform for calls to module members in code.

Definition at line 169 of file fix_urllib.py.

00169 
00170     def transform_dot(self, node, results):
00171         """Transform for calls to module members in code."""
00172         module_dot = results.get("bare_with_attr")
00173         member = results.get("member")
00174         new_name = None
00175         if isinstance(member, list):
00176             member = member[0]
00177         for change in MAPPING[module_dot.value]:
00178             if member.value in change[1]:
00179                 new_name = change[0]
00180                 break
00181         if new_name:
00182             module_dot.replace(Name(new_name,
00183                                     prefix=module_dot.prefix))
00184         else:
00185             self.cannot_convert(node, "This is an invalid module element")

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixes.fix_urllib.FixUrllib.transform_import (   self,
  node,
  results 
)
Transform for the basic import case. Replaces the old
   import name with a comma separated list of its
   replacements.

Definition at line 77 of file fix_urllib.py.

00077 
00078     def transform_import(self, node, results):
00079         """Transform for the basic import case. Replaces the old
00080            import name with a comma separated list of its
00081            replacements.
00082         """
00083         import_mod = results.get("module")
00084         pref = import_mod.prefix
00085 
00086         names = []
00087 
00088         # create a Node list of the replacement modules
00089         for name in MAPPING[import_mod.value][:-1]:
00090             names.extend([Name(name[0], prefix=pref), Comma()])
00091         names.append(Name(MAPPING[import_mod.value][-1][0], prefix=pref))
00092         import_mod.replace(names)

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixes.fix_urllib.FixUrllib.transform_member (   self,
  node,
  results 
)
Transform for imports of specific module elements. Replaces
   the module to be imported from with the appropriate new
   module.

Definition at line 93 of file fix_urllib.py.

00093 
00094     def transform_member(self, node, results):
00095         """Transform for imports of specific module elements. Replaces
00096            the module to be imported from with the appropriate new
00097            module.
00098         """
00099         mod_member = results.get("mod_member")
00100         pref = mod_member.prefix
00101         member = results.get("member")
00102 
00103         # Simple case with only a single member being imported
00104         if member:
00105             # this may be a list of length one, or just a node
00106             if isinstance(member, list):
00107                 member = member[0]
00108             new_name = None
00109             for change in MAPPING[mod_member.value]:
00110                 if member.value in change[1]:
00111                     new_name = change[0]
00112                     break
00113             if new_name:
00114                 mod_member.replace(Name(new_name, prefix=pref))
00115             else:
00116                 self.cannot_convert(node, "This is an invalid module element")
00117 
00118         # Multiple members being imported
00119         else:
00120             # a dictionary for replacements, order matters
00121             modules = []
00122             mod_dict = {}
00123             members = results["members"]
00124             for member in members:
00125                 # we only care about the actual members
00126                 if member.type == syms.import_as_name:
00127                     as_name = member.children[2].value
00128                     member_name = member.children[0].value
00129                 else:
00130                     member_name = member.value
00131                     as_name = None
00132                 if member_name != ",":
00133                     for change in MAPPING[mod_member.value]:
00134                         if member_name in change[1]:
00135                             if change[0] not in mod_dict:
00136                                 modules.append(change[0])
00137                             mod_dict.setdefault(change[0], []).append(member)
00138 
00139             new_nodes = []
00140             indentation = find_indentation(node)
00141             first = True
00142             def handle_name(name, prefix):
00143                 if name.type == syms.import_as_name:
00144                     kids = [Name(name.children[0].value, prefix=prefix),
00145                             name.children[1].clone(),
00146                             name.children[2].clone()]
00147                     return [Node(syms.import_as_name, kids)]
00148                 return [Name(name.value, prefix=prefix)]
00149             for module in modules:
00150                 elts = mod_dict[module]
00151                 names = []
00152                 for elt in elts[:-1]:
00153                     names.extend(handle_name(elt, pref))
00154                     names.append(Comma())
00155                 names.extend(handle_name(elts[-1], pref))
00156                 new = FromImport(module, names)
00157                 if not first or node.parent.prefix.endswith(indentation):
00158                     new.prefix = indentation
00159                 new_nodes.append(new)
00160                 first = False
00161             if new_nodes:
00162                 nodes = []
00163                 for new_node in new_nodes[:-1]:
00164                     nodes.extend([new_node, Newline()])
00165                 nodes.append(new_nodes[-1])
00166                 node.replace(nodes)
00167             else:
00168                 self.cannot_convert(node, "All module elements are invalid")

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_base.BaseFix.warning (   self,
  node,
  reason 
) [inherited]
Used for warning the user about possible uncertainty in the
translation.

First argument is the top-level node for the code in question.
Optional second argument is why it can't be converted.

Definition at line 140 of file fixer_base.py.

00140 
00141     def warning(self, node, reason):
00142         """Used for warning the user about possible uncertainty in the
00143         translation.
00144 
00145         First argument is the top-level node for the code in question.
00146         Optional second argument is why it can't be converted.
00147         """
00148         lineno = node.get_lineno()
00149         self.log_message("Line %d: %s" % (lineno, reason))

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 87 of file fix_imports.py.

lib2to3.fixer_base.BaseFix.explicit = False [static, inherited]
lib2to3.fixer_base.BaseFix.filename = None [static, inherited]

Definition at line 29 of file fixer_base.py.

Definition at line 121 of file fixer_base.py.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 88 of file fix_imports.py.

Definition at line 58 of file fixer_base.py.

lib2to3.fixer_base.BaseFix.logger = None [static, inherited]

Definition at line 30 of file fixer_base.py.

Reimplemented in lib2to3.fixes.fix_imports2.FixImports2.

Definition at line 90 of file fix_imports.py.

tuple lib2to3.fixer_base.BaseFix.numbers = itertools.count(1) [static, inherited]

Definition at line 31 of file fixer_base.py.

Definition at line 159 of file fixer_base.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

lib2to3.fixer_base.BaseFix.options = None [static, inherited]

Definition at line 28 of file fixer_base.py.

string lib2to3.fixer_base.BaseFix.order = "post" [static, inherited]
lib2to3.fixer_base.BaseFix.pattern = None [static, inherited]

Definition at line 26 of file fixer_base.py.

Reimplemented from lib2to3.fixer_base.BaseFix.

Definition at line 102 of file fix_imports.py.

lib2to3.fixer_base.BaseFix.pattern_tree = None [static, inherited]

Definition at line 27 of file fixer_base.py.

Definition at line 120 of file fix_imports.py.

Reimplemented from lib2to3.fixer_base.BaseFix.

Reimplemented in lib2to3.fixes.fix_imports2.FixImports2.

Definition at line 94 of file fix_imports.py.

Definition at line 47 of file fixer_base.py.

tuple lib2to3.fixer_base.BaseFix.used_names = set() [static, inherited]

Definition at line 32 of file fixer_base.py.

Definition at line 157 of file fixer_base.py.


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