Back to index

python3.2  3.2.2
Classes | Functions
lib2to3.main Namespace Reference

Classes

class  StdoutRefactoringTool

Functions

def diff_texts
def warn
def main

Detailed Description

Main program for 2to3.

Function Documentation

def lib2to3.main.diff_texts (   a,
  b,
  filename 
)
Return a unified diff of two strings.

Definition at line 17 of file main.py.

00017 
00018 def diff_texts(a, b, filename):
00019     """Return a unified diff of two strings."""
00020     a = a.splitlines()
00021     b = b.splitlines()
00022     return difflib.unified_diff(a, b, filename, filename,
00023                                 "(original)", "(refactored)",
00024                                 lineterm="")
00025 

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.main.main (   fixer_pkg,
  args = None 
)
Main program.

Args:
    fixer_pkg: the name of a package where the fixers are located.
    args: optional; a list of command line arguments. If omitted,
          sys.argv[1:] is used.

Returns a suggested exit status (0, 1, 2).

Definition at line 84 of file main.py.

00084 
00085 def main(fixer_pkg, args=None):
00086     """Main program.
00087 
00088     Args:
00089         fixer_pkg: the name of a package where the fixers are located.
00090         args: optional; a list of command line arguments. If omitted,
00091               sys.argv[1:] is used.
00092 
00093     Returns a suggested exit status (0, 1, 2).
00094     """
00095     # Set up option parser
00096     parser = optparse.OptionParser(usage="2to3 [options] file|dir ...")
00097     parser.add_option("-d", "--doctests_only", action="store_true",
00098                       help="Fix up doctests only")
00099     parser.add_option("-f", "--fix", action="append", default=[],
00100                       help="Each FIX specifies a transformation; default: all")
00101     parser.add_option("-j", "--processes", action="store", default=1,
00102                       type="int", help="Run 2to3 concurrently")
00103     parser.add_option("-x", "--nofix", action="append", default=[],
00104                       help="Prevent a transformation from being run")
00105     parser.add_option("-l", "--list-fixes", action="store_true",
00106                       help="List available transformations")
00107     parser.add_option("-p", "--print-function", action="store_true",
00108                       help="Modify the grammar so that print() is a function")
00109     parser.add_option("-v", "--verbose", action="store_true",
00110                       help="More verbose logging")
00111     parser.add_option("--no-diffs", action="store_true",
00112                       help="Don't show diffs of the refactoring")
00113     parser.add_option("-w", "--write", action="store_true",
00114                       help="Write back modified files")
00115     parser.add_option("-n", "--nobackups", action="store_true", default=False,
00116                       help="Don't write backups for modified files")
00117 
00118     # Parse command line arguments
00119     refactor_stdin = False
00120     flags = {}
00121     options, args = parser.parse_args(args)
00122     if not options.write and options.no_diffs:
00123         warn("not writing files and not printing diffs; that's not very useful")
00124     if not options.write and options.nobackups:
00125         parser.error("Can't use -n without -w")
00126     if options.list_fixes:
00127         print("Available transformations for the -f/--fix option:")
00128         for fixname in refactor.get_all_fix_names(fixer_pkg):
00129             print(fixname)
00130         if not args:
00131             return 0
00132     if not args:
00133         print("At least one file or directory argument required.", file=sys.stderr)
00134         print("Use --help to show usage.", file=sys.stderr)
00135         return 2
00136     if "-" in args:
00137         refactor_stdin = True
00138         if options.write:
00139             print("Can't write to stdin.", file=sys.stderr)
00140             return 2
00141     if options.print_function:
00142         flags["print_function"] = True
00143 
00144     # Set up logging handler
00145     level = logging.DEBUG if options.verbose else logging.INFO
00146     logging.basicConfig(format='%(name)s: %(message)s', level=level)
00147 
00148     # Initialize the refactoring tool
00149     avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
00150     unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
00151     explicit = set()
00152     if options.fix:
00153         all_present = False
00154         for fix in options.fix:
00155             if fix == "all":
00156                 all_present = True
00157             else:
00158                 explicit.add(fixer_pkg + ".fix_" + fix)
00159         requested = avail_fixes.union(explicit) if all_present else explicit
00160     else:
00161         requested = avail_fixes.union(explicit)
00162     fixer_names = requested.difference(unwanted_fixes)
00163     rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
00164                                options.nobackups, not options.no_diffs)
00165 
00166     # Refactor all files and directories passed as arguments
00167     if not rt.errors:
00168         if refactor_stdin:
00169             rt.refactor_stdin()
00170         else:
00171             try:
00172                 rt.refactor(args, options.write, options.doctests_only,
00173                             options.processes)
00174             except refactor.MultiprocessingUnsupported:
00175                 assert options.processes > 1
00176                 print("Sorry, -j isn't supported on this platform.",
00177                       file=sys.stderr)
00178                 return 1
00179         rt.summarize()
00180 
00181     # Return error status (0 if rt.errors is zero)
00182     return int(bool(rt.errors))

Here is the call graph for this function:

def lib2to3.main.warn (   msg)

Definition at line 80 of file main.py.

00080 
00081 def warn(msg):
00082     print("WARNING: %s" % (msg,), file=sys.stderr)
00083 

Here is the caller graph for this function: