B nR@sdZdZddlZddlZddlZddlZdadadada da dddZ dd Z d d Z d d ZdddZGdddZddZedkre dS)areindent [-d][-r][-v] [ path ... ] -d (--dryrun) Dry run. Analyze, but don't make any changes to, files. -r (--recurse) Recurse. Search for all .py files in subdirectories too. -n (--nobackup) No backup. Does not make a ".bak" file before reindenting. -v (--verbose) Verbose. Print informative msgs; else no output. (--newline) Newline. Specify the newline character to use (CRLF, LF). Default is the same as the original file. -h (--help) Help. Print this usage information and exit. Change Python (.py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files. Also ensure the last line ends with a newline. If no paths are given on the command line, reindent operates as a filter, reading a single source file from standard input and writing the transformed source to standard output. In this case, the -d, -r and -v flags are ignored. You can pass one or more file and/or directory paths. When a directory path, all .py files within the directory will be examined, and, if the -r option is given, likewise recursively for subdirectories. If output is not to standard output, reindent overwrites files in place, renaming the originals with a .bak extension. If it finds nothing to change, the file is left alone. If reindent does change a file, the changed file is a fixed-point for future runs (i.e., running reindent on the resulting .py file won't change it again). The hard part of reindenting is figuring out what to do with comment lines. So long as the input files get a clean bill of health from tabnanny.py, reindent should do a good job. The backup file is a copy of the one that is being reindented. The ".bak" file is generated with shutil.copy(), but some corner cases regarding user/group and permissions could leave the backup file more readable than you'd prefer. You can always use the --nobackup option to prevent this. 1NFTcCs|dkr t}t|tjddS)N)file)__doc__printsysstderr)msgr ;/opt/alt/python37/lib64/python3.7/Tools/scripts/reindent.pyusage;sr cGs,tjddd|DtjddS)N css|]}t|VqdS)N)str).0argr r r Bszerrprint.. )rrwritejoin)argsr r r errprintAsrc Cs6ddl}y,|tjddddddddd g\}}Wn,|jk r`}z t|dSd}~XYnXx|D]\}}|d kr~d aqh|d krd aqh|d krdaqh|dkrd aqh|dkr| dkrtdSt ddd| a qh|dkrhtdSqhW|st tj }||tjdSx|D]}t|q WdS)NrZdrnvhdryrunrecurseZnobackupverboseznewline=help)z-dz--dryrunT)z-rz --recurse)z-nz --nobackupF)z-vz --verbose)z --newline)ZCRLFZLFz r)z-hz--help)getoptrargverrorr rr makebackuprupperdict spec_newline Reindenterstdinrunrstdoutcheck)rZoptsrroarrr r r mainEs>    r*c CsDtj|rtj|str&td|t|}xb|D]Z}tj||}trztj|rztj|sztj |d dr| dr6t |q6WdStrtd|dddt|d T}yt|j\}}Wn8tk r}ztd |t|fdSd}~XYnXWdQRXy$t||d }t|}WdQRXWn8tk rl} ztd |t| fdSd} ~ XYnXtrxtn|j} t| trtd |dS|r.trtdtrtdts*|d} trt|| trtd|d| t|d|| d}| |WdQRXtr*td|dStr%s: mixed newlines detected; cannot continue without --newlinezchanged.z+But this is a dry run, so leaving it alone.z.bakz backed uptow)r.newlinez wrote newTz unchanged.F)!ospathisdirislinkrrlistdirrrsplit startswithlowerendswithr&opentokenizedetect_encodingreadline SyntaxErrorrr r"IOErrorr!newlines isinstancetupler$rrshutilZcopyfiler) rnamesnamefullnamefr._Zser)rr1Zbakr r r r&hsb           r& cCs:t|}x$|dkr,||d|kr,|d8}q W|d|S)aReturn line stripped of trailing spaces, tabs, newlines. Note that line.rstrip() instead also strips sundry control characters, but at least one known Emacs user expects to keep junk like that, not mentioning Barry by name or anything . rrN)len)lineZJUNKir r r _rstrips rNc@sJeZdZddZddZddZddZejej ej ej ej fd d Z d S) r"cCsNd|_d|_||_dd|jD|_|jddd|_g|_|j|_dS)NrrcSsg|]}t|dqS)r)rN expandtabs)rrLr r r sz'Reindenter.__init__..) find_stmtlevel readlinesrawlinesinsertindexstatsrA)selfrHr r r __init__s  zReindenter.__init__cCst|j}x|D]}|j|qW|j}x|rF|ddkrF|q,W|j}|t|dfi}g}|_ |dd}| |d|xt t|dD]}||\}} ||dd} t ||} | d} | dkr| r| | d} | dkrPxRt |dt|dD]8} || \}}|dkr| t ||krH|d} PqW| dkrxRt |dddD]>} || \}}|dkrl| t ||dt ||} PqlW| dkr| } nd} | dkst| || <| | }|dks| dkr| ||| qxl||| D]\}|dkrN|dkr:||n|d||n"tt || }|||dqWqW|j|j kS)Nrrrr )r<generate_tokensgetline tokeneaterrUpoprXappendrKafterextendrange getlspacegetAssertionErrorminrT)rYtokensZ_tokenrUrXZ have2wantrbrMZthisstmtZ thislevelZnextstmtZhaveZwantjZjlineZjlevelZdiffrLremover r r r$sd                   zReindenter.runcCs||jdS)N) writelinesrb)rYrHr r r rszReindenter.writecCs4|jt|jkrd}n|j|j}|jd7_|S)Nr)rWrKrU)rYrLr r r r^s  zReindenter.getlinec Cs||krd|_n||kr.d|_|jd7_nr||krLd|_|jd8_nT|| krp|jr|j|ddfn0|| krzn&|jrd|_|r|j|d|jfdS)Nrrr[)rQrRrXra) rYtypetokenZslinecolr,rLINDENTDEDENTNEWLINECOMMENTNLr r r r_s"zReindenter.tokeneaterN)__name__ __module__ __qualname__rZr$rr^r<rprqrrrsrtr_r r r r r"sG r"cCs4dt|}}x ||kr.||dkr.|d7}qW|S)Nrr r)rK)rLrMnr r r reEs re__main__)N)rJ)r __version__r<r2rDrrrrrr!r rr*r&rNr"rerur r r r *s( #8