3 \…- ã @ s€ d Z ddlmZmZ ddlZddlZddlZddlZddlZddl Z ddl mZ dd„ ZG dd „ d ej ƒZd d„ Zddd „ZdS )z Main program for 2to3. é )Úwith_statementÚprint_functionNé )Úrefactorc C s( | j ƒ } |j ƒ }tj| |||dddd�S )z%Return a unified diff of two strings.z (original)z(refactored)Ú )Zlineterm)Ú splitlinesÚdifflibZunified_diff)ÚaÚbÚfilename© r ú$/usr/lib64/python3.6/lib2to3/main.pyÚ diff_texts s r c s> e Zd ZdZd‡ fdd„ Zdd„ Z‡ fdd„Zd d „ Z‡ ZS )Ú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. r c sR || _ || _|r(|jtjƒ r(|tj7 }|| _|| _|| _tt | ƒj |||ƒ dS )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__) ÚselfZfixersÚoptionsÚexplicitr r Úinput_base_dirÚ output_dirÚ append_suffix)Ú __class__r r r $ s zStdoutRefactoringTool.__init__c O s* | j j|||fƒ | jj|f|ž|Ž d S )N)ÚerrorsÚappendÚloggerÚerror)r ÚmsgÚargsÚkwargsr r r Ú log_errorA s zStdoutRefactoringTool.log_errorc ! s| |}| j rH|j| jƒr6tjj| j |t| jƒd … ƒ}ntd|| jf ƒ‚| jrX|| j7 }||kr–tjj |ƒ}tjj |ƒ rˆ|rˆtj|ƒ | jd||ƒ | j �s4|d }tjj|ƒrðytj|ƒ W n. tk rî } z| jd|ƒ W Y d d }~X nX ytj||ƒ W n2 tk �r2 } z| jd||ƒ W Y d d }~X nX tt| ƒj} | ||||ƒ | j �sbtj||ƒ ||k�rxtj||ƒ d S )Nz5filename %s does not start with the input_base_dir %szWriting converted %s to %s.z.bakzCan't remove backup %szCan't rename %s to %s)r Ú startswithr r ÚpathÚjoinÚlenÚ ValueErrorr ÚdirnameÚisdirÚmakedirsÚlog_messager ÚlexistsÚremoveÚOSErrorÚrenamer r Ú write_fileÚshutilZcopymode) r Znew_textr Zold_textÚencodingZ orig_filenamer ZbackupÚerrÚwrite)r r r r6 E s@ z StdoutRefactoringTool.write_filec C s° |r| j d|ƒ nš| j d|ƒ | jr¬t|||ƒ}yX| jd k rp| j�& x|D ]}t|ƒ qJW tjjƒ W d Q R X nx|D ]}t|ƒ qvW W n" tk rª t d|f ƒ d S X d S )NzNo changes to %sz Refactored %sz+couldn't encode %s's diff for your terminal) r1 r r Zoutput_lockÚprintÚsysÚstdoutÚflushÚUnicodeEncodeErrorÚwarn)r ÚoldÚnewr ZequalZ diff_linesÚliner r r Úprint_outputl s" z"StdoutRefactoringTool.print_output)r r r ) Ú__name__Ú __module__Ú__qualname__Ú__doc__r r( r6 rD Ú __classcell__r r )r r r s 'r c C s t d| f tjd� d S )NzWARNING: %s)Úfile)r; r<