
    "eN.                         d Z ddlmZmZ ddlZddlZddlZddlZddlZddl	Z	ddl
mZ d Z G d dej                        Zd	 Zdd
Zy)z
Main program for 2to3.
    )with_statementprint_functionN   )refactorc           	      z    | j                         } |j                         }t        j                  | |||ddd      S )z%Return a unified diff of two strings.z
(original)z(refactored) )lineterm)
splitlinesdifflibunified_diff)abfilenames      lib/python3.12/lib2to3/main.py
diff_textsr      s;    	A	A1h ,n)+- -    c                   <     e Zd ZdZ	 d fd	Zd Z fdZd Z xZS )StdoutRefactoringToola2  
    A refactoring tool that can avoid overwriting its input files.
    Prints output to stdout.

    Output files can optionally be written to a different directory and or
    have an extra file suffix appended to their name for use in situations
    where you do not want to replace the input files.
    c	                     || _         || _        |r2|j                  t        j                        s|t        j                  z  }|| _        || _        || _        t        t        | +  |||       y)aF  
        Args:
            fixers: A list of fixers to import.
            options: A dict with RefactoringTool configuration.
            explicit: A list of fixers to run even if they are explicit.
            nobackups: If true no backup '.bak' files will be created for those
                files that are being refactored.
            show_diffs: Should diffs of the refactoring be printed to stdout?
            input_base_dir: The base directory for all input files.  This class
                will strip this path prefix off of filenames before substituting
                it with output_dir.  Only meaningful if output_dir is supplied.
                All files processed by refactor() must start with this path.
            output_dir: If supplied, all converted files will be written into
                this directory tree instead of input_base_dir.
            append_suffix: If supplied, all files output by this tool will have
                this appended to their filename.  Useful for changing .py to
                .py3 for example by passing append_suffix='3'.
        N)	nobackups
show_diffsendswithossep_input_base_dir_output_dir_append_suffixsuperr   __init__)
selffixersoptionsexplicitr   r   input_base_dir
output_dirappend_suffix	__class__s
            r   r   zStdoutRefactoringTool.__init__$   sa    ( #$."9"9"&&"Abff$N-%+#T3FGXNr   c                     | j                   j                  |||f        | j                  j                  |g|i | y N)errorsappendloggererror)r    msgargskwargss       r   	log_errorzStdoutRefactoringTool.log_errorA   s9    Cv./#///r   c                    |}| j                   rw|j                  | j                        rAt        j                  j                  | j                   |t        | j                        d        }nt        d|d| j                        | j                  r|| j                  z  }||k7  rht        j                  j                  |      }t        j                  j                  |      s|rt        j                  |       | j                  d||       | j                  sQ|dz   }t        j                  j                  |      r	 t        j                  |       	 t        j"                  ||       t$        t&        | R  } |||||       | j                  st+        j,                  |       ||k7  rt+        j,                  ||       y y # t         $ r | j                  d|       Y w xY w# t         $ r | j                  d||       Y w xY w)Nz	filename z( does not start with the input_base_dir zWriting converted %s to %s.z.bakzCan't remove backup %szCan't rename %s to %s)r   
startswithr   r   pathjoinlen
ValueErrorr   dirnameisdirmakedirslog_messager   lexistsremoveOSErrorrenamer   r   
write_fileshutilcopymode)
r    new_textr   old_textencodingorig_filenamer%   backupwriter'   s
            r   r@   z StdoutRefactoringTool.write_fileE   s    ""4#7#7877<<(8(8(0T5I5I1J1K(LN !)143G3G"I J J +++HH$2J77==,J':M%'~~&Fwwv&GIIf%L		(F+ +T=h(H5~~OOFH-H$OOM84 %  G$$%=vFG  L  !8(FKLs$    G G% G"!G"%HHc                    |r| j                  d|       y | j                  d|       | j                  rst        |||      }	 | j                  F| j                  5  |D ]  }t	        |        t
        j                  j                          d d d        y |D ]  }t	        |        y y # 1 sw Y   y xY w# t        $ r t        d|d       Y y w xY w)NzNo changes to %szRefactored %szcouldn't encode z's diff for your terminal)
r;   r   r   output_lockprintsysstdoutflushUnicodeEncodeErrorwarn)r    oldnewr   equal
diff_lineslines          r   print_outputz"StdoutRefactoringTool.print_outputl   s    /:_h7'S(;
''3!-- /(2 , %d,JJ,,./ /
 %/ (D!$K( / / * "% &s6   B4 1B(B4 B4 (B1-B4 1B4 4CC)r   r   r   )	__name__
__module____qualname____doc__r   r1   r@   rV   __classcell__)r'   s   @r   r   r      s%     BDO:0%5Nr   r   c                 @    t        d| t        j                         y )Nz	WARNING: file)rK   rL   stderr)r.   s    r   rP   rP      s    	3
 szz2r   c           
          t        j                  d      }|j                  dddd       |j                  dd	d
g d       |j                  dddddd       |j                  ddd
g d       |j                  dddd       |j                  dddd       |j                  dddd       |j                  d d!dd"       |j                  d#dd$       |j                  d%d&dd'       |j                  d(d)dd*d+       |j                  d,d-dd.d/d01       |j                  d2d3dd4       |j                  d5dd.d/d61       d*}i }|j                  |      \  }}|j                  r#d7|d8<   |j
                  st        d9       d7|_        |j                  r|j                  s|j                  d:       |j                  r|j                  s|j                  d;       |j
                  s|j                  rt        d<       |j
                  s|j                  r|j                  d=       |j                  r3t        d>       t        j                         D ]  }t        |        |sy?|s7t        d@t         j"                  A       t        dBt         j"                  A       yCdD|v r*d7}|j
                  rt        dEt         j"                  A       yC|j$                  rd7|dF<   |j&                  rd7|dG<   |j(                  rt*        j,                  nt*        j.                  }t+        j0                  dH|I       t+        j2                  dJ      }t5        t        j6                               }	t5         fdK|j8                  D              }
t5               }|j:                  rHd*}|j:                  D ]!  }|dLk(  rd7}|j=                   dMz   |z          # |r|	j?                  |      n|}n|	j?                  |      }|jA                  |
      }tB        jD                  jG                  |      }|r]|jI                  tB        jJ                        s>tB        jD                  jM                  |      stB        jD                  jO                  |      }|j                  r<|jQ                  tB        jJ                        }|jS                  dN|j                  |       tU        tW        |      |tW        |      |j                  |j                   ||j                  |j                  O      }|jX                  sV|r|j[                          n3	 |j                  ||j
                  |j\                  |j^                         |jc                          te        tg        |jX                              S # t        j`                  $ r/ |j^                  dkD  sJ t        dPt         j"                  A       Y yw xY w)QzMain 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2to3 [options] file|dir ...)usagez-dz--doctests_only
store_truezFix up doctests only)actionhelpz-fz--fixr+   z1Each FIX specifies a transformation; default: all)rc   defaultrd   z-jz--processesstorer   intzRun 2to3 concurrently)rc   re   typerd   z-xz--nofixz'Prevent a transformation from being runz-lz--list-fixeszList available transformationsz-pz--print-functionz0Modify the grammar so that print() is a functionz-ez--exec-functionz/Modify the grammar so that exec() 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 filesz-oz--output-dirstrr   zXPut output files in this directory instead of overwriting the input files.  Requires -n.)rc   rh   re   rd   z-Wz--write-unchanged-fileszYAlso write files even if no changes were required (useful with --output-dir); implies -w.z--add-suffixzuAppend this string to all output filenames. Requires -n if non-empty.  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 -wz2Available transformations for the -f/--fix option:r   z1At least one file or directory argument required.r]   zUse --help to show usage.   -zCan't write to stdin.r   exec_functionz%(name)s: %(message)s)formatlevelzlib2to3.mainc              3   .   K   | ]  }d z   |z     yw).fix_N ).0fix	fixer_pkgs     r   	<genexpr>zmain.<locals>.<genexpr>   s     LsW,s2Ls   allrq   z7Output in %r will mirror the input directory %r layout.)r$   r%   r&   z+Sorry, -j isn't supported on this platform.)4optparseOptionParser
add_option
parse_argsrj   rH   rP   r%   r   r-   
add_suffixno_diffs
list_fixesrK   r   get_all_fix_namesrL   r_   r   rm   verboseloggingDEBUGINFObasicConfig	getLoggersetget_fixers_from_packagenofixrt   addunion
differencer   r4   commonprefixr   r   r9   r8   rstripinfor   sortedr*   refactor_stdindoctests_only	processesMultiprocessingUnsupported	summarizerg   bool)ru   r/   parserr   flagsr"   fixnamero   r,   avail_fixesunwanted_fixesr#   all_presentrt   	requestedfixer_namesr$   rts   `                 r   mainr      sv    "")FGF
d-l1  3
dGHbN  P
dM'1 '>  @
dIhD  F
dN<;  =
d.|M  O
d-lL  N
dK1  3
l<@  B
dIl6  8
dM,C  E
dN7  (N  O d5lA  B nW5"G  H NE%%d+MGT$$)-%&}}9: '"3"3<='"3"39:==W--OP==W.../BC11)< 	G'N	A

S)

;
d{==)

;"&!%o %__GMM',,E6eD~.F h66yABKLgmmLLNuH{{;; 	8Ce|"Y0367		8
 4?K%%h/H	%%h/	&&~6KWW))$/N~66rvv>n- 8'..rvv6M&&	8	;x(87#3#33)))!,,
.B 99D'--1F1F#--/ 	 tBII 66 ((1,,C::'	s   :2V ?WWr)   )rZ   
__future__r   r   rL   r   r   r   rA   rx   r   r   r   MultiprocessRefactoringToolr   rP   r   rr   r   r   <module>r      sI    6 
 	     -eH@@ eN3L r   