B ‡ f¤- ã @ 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( | ¡ } | ¡ }tj| |||ddddS )z%Return a unified diff of two strings.z (original)z(refactored)Ú )Zlineterm)Ú splitlinesÚdifflibZunified_diff)ÚaÚbÚfilename© r ú1/opt/alt/python37/lib64/python3.7/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 sP || _ || _|r&| tj¡s&|tj7 }|| _|| _|| _tt | ƒ |||¡ 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 |||f¡ | jj|f|ž|Ž d S )N)ÚerrorsÚappendÚloggerÚerror)r ÚmsgÚargsÚkwargsr r r Ú log_errorA s zStdoutRefactoringTool.log_errorc sz |}| j rH| | j¡r6tj | j |t| jƒd … ¡}ntd|| jf ƒ‚| jrX|| j7 }||kr”tj |¡}tj |¡s†|r†t |¡ | d||¡ | j s2|d }tj |¡rîyt |¡ W n. tk rì } z| d|¡ W d d }~X Y nX yt ||¡ W n2 tk r0 } z| d||¡ W d d }~X Y nX tt| ƒj} | ||||ƒ | j s`t ||¡ ||krvt ||¡ 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| d|¡ nš| d|¡ | jr¬t|||ƒ}yX| jd k rp| j& x|D ]}t|ƒ qJW tj ¡ 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<