B ÍAÒ[¥5ã@s’dZddlmZmZmZddlZddlmZddlZddl Z ddl Z ddl Z ddl m Z mZddlmZddlmZmZmZmZdZd d d „ZdS) a¶ futurize: automatic conversion to clean 2/3 code using ``python-future`` ====================================================================== Like Armin Ronacher's modernize.py, ``futurize`` attempts to produce clean standard Python 3 code that runs on both Py2 and Py3. One pass -------- Use it like this on Python 2 code: $ futurize --verbose mypython2script.py This will attempt to port the code to standard Py3 code that also provides Py2 compatibility with the help of the right imports from ``future``. To write changes to the files, use the -w flag. Two stages ---------- The ``futurize`` script can also be called in two separate stages. First: $ futurize --stage1 mypython2script.py This produces more modern Python 2 code that is not yet compatible with Python 3. The tests should still run and the diff should be uncontroversial to apply to most Python projects that are willing to drop support for Python 2.5 and lower. After this, the recommended approach is to explicitly mark all strings that must be byte-strings with a b'' prefix and all text (unicode) strings with a u'' prefix, and then invoke the second stage of Python 2 to 2/3 conversion with:: $ futurize --stage2 mypython2script.py Stage 2 adds a dependency on ``future``. It converts most remaining Python 2-specific code to Python 3 code and adds appropriate imports from ``future`` to restore Py2 support. The command above leaves all unadorned string literals as native strings (byte-strings on Py2, unicode strings on Py3). If instead you would like all unadorned string literals to be promoted to unicode, you can also pass this flag: $ futurize --stage2 --unicode-literals mypython2script.py This adds the declaration ``from __future__ import unicode_literals`` to the top of each file, which implicitly declares all unadorned string literals to be unicode strings (``unicode`` on Py2). All imports ----------- The --all-imports option forces adding all ``__future__`` imports, ``builtins`` imports, and standard library aliases, even if they don't seem necessary for the current state of each module. (This can simplify testing, and can reduce the need to think about Py2 compatibility when editing the code further.) é)Úabsolute_importÚprint_functionÚunicode_literalsN)Ú __version__)ÚwarnÚStdoutRefactoringTool)Úrefactor)Úlib2to3_fix_names_stage1Úlib2to3_fix_names_stage2Úlibfuturize_fix_names_stage1Úlibfuturize_fix_names_stage2zlibfuturize.fixescs tjdd}|jddddd|jdd dd d|jd d dd d|jddddd|jddddd|jddddd|jdddgdd|jddddd d!d"|jd#d$dgd%d|jd&d'dd(d|jd)d*dd+d|jd,d-dd.d|jd/dd0d|jd1d2dd3d|jd4d5dd6d7d|jd8d9dd:d;dd?dd@d|jdAdd:d;dBd=i}d6}| |¡\}}|jršdC|dD<|js”tdEƒdC|_|jr´|js´|  dF¡|j rÎ|jsÎ|  dG¡|jsæ|j rætdHƒ|js|jr|  dI¡dJ|kr(dC}|jr(t dKt jdLdMS|jr8dC|dN<|jrFtjntj}tjdO|dPt dQ¡}|jst|jrŒ|jdRks„t‚d6|_ndC|_tƒ}|js¨|jr¼| t¡| t¡|jsÌ|jrà| t¡| t¡|j rò| !dS¡|j"rt t#ƒdTS|j$r¡t?t@|j9ƒƒS)nzþ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). zfuturize [options] file|dir ...)Úusagez-Vz --versionÚ store_truez%Report the version number of futurize)ÚactionÚhelpz-az --all-importsz4Add all __future__ and future imports to each modulez-1z--stage1zZModernize Python 2 code only; no compatibility with Python 3 (or dependency on ``future``)z-2z--stage2z^Take modernized (stage1) code and add a dependency on ``future`` to provide Py3 compatibility.z-0z --both-stageszApply both stages 1 and 2z-uz--unicode-literalsz{Add ``from __future__ import unicode_literals`` to implicitly convert all unadorned string literals '' into unicode stringsz-fz--fixÚappendzÊEach FIX specifies a transformation; default: all. Either use '-f division -f metaclass' etc. or use the fully-qualified module name: '-f lib2to3.fixes.fix_types -f libfuturize.fixes.fix_unicode_keep_u')rÚdefaultrz-jz --processesÚstoreéÚintzRun 2to3 concurrently)rrÚtyperz-xz--nofixzPrevent a fixer from being run.z-lz --list-fixeszList available transformationsz-pz--print-functionz0Modify the grammar so that print() is a functionz-vz --verbosezMore verbose loggingz --no-diffsz#Don't show diffs of the refactoringz-wz--writezWrite back modified filesz-nz --nobackupsFz'Don't write backups for modified files.z-oz --output-dirÚstrÚzpPut output files in this directory instead of overwriting the input files. Requires -n. For Python >= 2.7 only.)rrrrz-Wz--write-unchanged-fileszYAlso write files even if no changes were required (useful with --output-dir); implies -w.z --add-suffixz‹Append this string to all output filenames. Requires -n if non-empty. For Python >= 2.7 only.ex: --add-suffix='3' will generate .py3 files.TÚwrite_unchanged_filesz&--write-unchanged-files/-W implies -w.z%Can't use --output-dir/-o without -n.z"Can't use --add-suffix without -n.z@not writing files and not printing diffs; that's not very usefulzCan't use -n without -wú-zCan't write to stdin.)Úfileérz%(name)s: %(message)s)ÚformatÚlevelzlibfuturize.mainNz-libfuturize.fixes.fix_unicode_literals_importrz2Available transformations for the -f/--fix option:z1At least one file or directory argument required.zUse --help to show usage.z.fix_cs g|]}| d ˆ¡¡r|‘qS)zfix_{0})Úendswithr)Ú.0Úf)Úfix©úA/opt/alt/python37/lib/python3.7/site-packages/libfuturize/main.pyú Øszmain..zOAmbiguous fixer name. Choose a fully qualified module name instead from these: Ú css|]}d|VqdS)z Nr#)r Úmyfr#r#r$ú Ýszmain..z1Unknown fixer. Use --list-fixes or -l for a list.zlibfuturize.fixes.Z0fix_add__future__imports_except_unicode_literalszlibpasteurize.fixes.Zfix_add_all__future__importsZ&fix_add_future_standard_library_importZfix_add_all_future_builtinsÚallcs g|]}| d ˆ¡¡r|‘qS)zfix_{0})rr)r r!)r"r#r$r%ÿscss|]}d|VqdS)z Nr#)r r'r#r#r$r(sz[Conflicting usage: the following fixers have been simultaneously requested and disallowed: css|]}d|VqdS)z Nr#)r r'r#r#r$r(sz7Output in %r will mirror the input directory %r layout.)Z append_suffixÚ output_dirÚinput_base_dirz+Sorry, -j isn't supported on this platform.)AÚoptparseÚ OptionParserÚ add_optionÚ parse_argsrÚwriterr*Z nobackupsÚerrorZ add_suffixZno_diffsÚprintÚsysÚstderrrÚverboseÚloggingÚDEBUGÚINFOÚ basicConfigÚ getLoggerZstage1Zstage2Z both_stagesÚAssertionErrorÚsetÚupdater r r r rÚaddÚversionrZ list_fixesÚsortedZnofixÚlenÚjoinZ all_importsr"ÚunionÚosÚpathÚ commonprefixrÚsepÚisdirÚdirnameÚrstripÚinfoÚfutureÚutilsZPY26rÚerrorsÚrefactor_stdinrZ processesZMultiprocessingUnsupportedZ summarizerÚbool)ÚargsÚparserÚflagsrOÚoptionsrÚloggerZ avail_fixesZfixnameZunwanted_fixesÚfoundZ extra_fixesÚprefixÚexplicitZ all_presentZ requestedÚ fixer_namesr+Z extra_kwargsÚrtr#)r"r$ÚmainTsX                                                        r[)N)Ú__doc__Ú __future__rrrZ future.utilsrLrr3r6r,rDZ lib2to3.mainrrZlib2to3rZlibfuturize.fixesr r r r Z fixer_pkgr[r#r#r#r$Ú>s