a
    W%b5                     @   s   d Z ddlmZmZmZ ddlZddlmZ ddlZddl	Z	ddl
Z
ddlZddlmZmZ ddlmZ ddlmZmZmZmZ dZdd	d
ZdS )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_stage2zlibfuturize.fixesc                    s  t jdd}|jddddd |jdd	dd
d |jddddd |jddddd |jddddd |jddddd |jdddg dd |jddddd d!d" |jd#d$dg d%d |jd&d'dd(d |jd)d*dd+d |jd,d-dd.d |jd/dd0d |jd1d2dd3d |jd4d5dd6d7d |jd8d9dd:d;d<d= |jd>d?dd@d |jdAdd:d;dBd= i }d6}|| \}} |jrdC|dD< |jstdE dC|_|jr|js|	dF |j
r|js|	dG |js|jrtdH |js |jr |	dI dJ| v r(dC}|jr(tdKtjdL dMS |jr8dC|dN< |jrFtjntj}tjdO|dP tdQ}|jst|jr|jdRu sJ d6|_ndC|_t }|js|jr|t |t |js|jr|t |t |jr| dS |j!rtt" dTS |j#r8tdU t$|D ]}t| q| s8dTS | s^tdVtjdL tdWtjdL dMS t }	|j%D ] dX v r|	   nx fdYdZ|D }
t&|
dkrtd[d\'d]d^ |
D  tjdL  dMS t&|
dTkrtd_tjdL  dMS |	 |
dT  qjt }|j(rX|jr*d`}| |da  n.db}| |dc  | |dd  | |de  t }|j)rhd6}|j)D ]  dfkrdC}ndX v r|   nx fdgdZ|D }
t&|
dkrtd[d\'dhd^ |
D  tjdL  dMS t&|
dTkrtd_tjdL  dMS | |
dT  qpt&||	@ dTkrRtdid\'djd^ ||	@ D  tjdL dMS |rb|*|n|}n
|*|}||B |	 }t+j,-| }|r|.t+j/st+j,0|st+j,1|}|jr|2t+j/}|3dk|j| t4j5j6ri }n|j
|j|dl}t7t$||t$||j|j fi |}|j8s|r:|9  nPz|:| |jdR|j; W n6 t:j<y   |j;dkstJ tdmtjdL Y dS 0 |=  t>t?|j8S )nzMain 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).
    zfuturize [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-stageszApply 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appendzEach 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   defaultr   z-jz--processesstore   intzRun 2to3 concurrently)r   r   typer   z-xz--nofixzPrevent a fixer from being run.z-lz--list-fixeszList available transformationsz-pz--print-functionz0Modify the grammar so that print() is a functionz-vz	--verbosezMore verbose loggingz
--no-diffsz#Don't show diffs of the refactoringz-wz--writezWrite 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.)r   r   r   r   z-Wz--write-unchanged-fileszYAlso write files even if no changes were required (useful with --output-dir); implies -w.z--add-suffixzAppend 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 usefulzCan't use -n without -w-zCan't write to stdin.)file   r   z%(name)s: %(message)s)formatlevelzlibfuturize.mainNz-libfuturize.fixes.fix_unicode_literals_importr   z2Available transformations for the -f/--fix option:z1At least one file or directory argument required.zUse --help to show usage.z.fix_c                    s    g | ]}| d  r|qS zfix_{0}endswithr   .0ffix `/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/libfuturize/main.py
<listcomp>   s   zmain.<locals>.<listcomp>zOAmbiguous fixer name. Choose a fully qualified module name instead from these:

c                 s   s   | ]}d | V  qdS z  Nr'   r#   Zmyfr'   r'   r(   	<genexpr>       zmain.<locals>.<genexpr>z1Unknown fixer. Use --list-fixes or -l for a list.zlibfuturize.fixes.Z0fix_add__future__imports_except_unicode_literalszlibpasteurize.fixes.Zfix_add_all__future__importsZ&fix_add_future_standard_library_importZfix_add_all_future_builtinsallc                    s    g | ]}| d  r|qS r   r    r"   r%   r'   r(   r)      s   c                 s   s   | ]}d | V  qdS r+   r'   r,   r'   r'   r(   r-     r.   z[Conflicting usage: the following fixers have been simultaneously requested and disallowed:
c                 s   s   | ]}d | V  qdS r+   r'   r,   r'   r'   r(   r-     r.   z7Output 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.)@optparseOptionParser
add_option
parse_argsr   writer   r0   Z	nobackupserrorZ
add_suffixZno_diffsprintsysstderrr   verboseloggingDEBUGINFObasicConfig	getLoggerZstage1Zstage2Zboth_stagessetupdater	   r   r
   r   r   addversionr   Z
list_fixessortedZnofixlenjoinZall_importsr&   unionospathcommonprefixr!   sepisdirdirnamerstripinfofutureutilsZPY26r   errorsrefactor_stdinr   Z	processesZMultiprocessingUnsupportedZ	summarizer   bool)argsparserflagsrT   optionsr   loggerZavail_fixesZfixnameZunwanted_fixesfoundZextra_fixesprefixexplicitZall_present	requestedfixer_namesr1   extra_kwargsrtr'   r%   r(   mainT   s   






























rb   )N)__doc__
__future__r   r   r   Zfuture.utilsrQ   r   r9   r<   r2   rI   Zlib2to3.mainr   r   Zlib2to3r   Zlibfuturize.fixesr	   r
   r   r   Z	fixer_pkgrb   r'   r'   r'   r(   <module>   s   ?