| 1 |
#!/usr/bin/python2 |
| 2 |
|
| 3 |
""" |
| 4 |
Translate from PEP8 Python style to Mininet (i.e. Arista-like) |
| 5 |
Python style |
| 6 |
|
| 7 |
usage: unpep8 < old.py > new.py |
| 8 |
|
| 9 |
- Reinstates CapWords for methods and instance variables |
| 10 |
- Gets rid of triple single quotes |
| 11 |
- Eliminates triple quotes on single lines |
| 12 |
- Inserts extra spaces to improve readability |
| 13 |
- Fixes Doxygen (or doxypy) ugliness |
| 14 |
|
| 15 |
Does the following translations: |
| 16 |
|
| 17 |
ClassName.method_name(foo = bar) -> ClassName.methodName( foo=bar ) |
| 18 |
|
| 19 |
Triple-single-quotes -> triple-double-quotes |
| 20 |
|
| 21 |
@param foo description -> foo: description |
| 22 |
@return description -> returns: description |
| 23 |
@author me -> author: me |
| 24 |
@todo(me) -> TODO(me) |
| 25 |
|
| 26 |
Bugs/Limitations: |
| 27 |
|
| 28 |
- Hack to restore strings is ugly |
| 29 |
- Multiline strings get mangled |
| 30 |
- Comments are mangled (which is arguably the "right thing" to do, except |
| 31 |
that, for example, the left hand sides of the above would get translated!) |
| 32 |
- Doesn't eliminate unnecessary backslashes |
| 33 |
- Has no opinion on tab size |
| 34 |
- complicated indented docstrings get flattened |
| 35 |
- We don't (yet) have a filter to generate Doxygen/Doxypy |
| 36 |
- Currently leaves indents on blank comment lines |
| 37 |
- May lead to namespace collisions (e.g. some_thing and someThing) |
| 38 |
|
| 39 |
Bob Lantz, [email protected] |
| 40 |
1/24/2010 |
| 41 |
""" |
| 42 |
|
| 43 |
from __future__ import print_function |
| 44 |
import re, sys |
| 45 |
|
| 46 |
def fixUnderscoreTriplet( match ): |
| 47 |
"Translate a matched triplet of the form a_b to aB." |
| 48 |
triplet = match.group() |
| 49 |
return triplet[ :-2 ] + triplet[ -1 ].capitalize() |
| 50 |
|
| 51 |
def reinstateCapWords( text ): |
| 52 |
underscoreTriplet = re.compile( r'[A-Za-z0-9]_[A-Za-z0-9]' ) |
| 53 |
return underscoreTriplet.sub( fixUnderscoreTriplet, text ) |
| 54 |
|
| 55 |
def replaceTripleApostrophes( text ): |
| 56 |
"Replace triple apostrophes with triple quotes." |
| 57 |
return text.replace( "'''", '"""') |
| 58 |
|
| 59 |
def simplifyTripleQuotes( text ): |
| 60 |
"Fix single-line doc strings." |
| 61 |
r = re.compile( r'"""([^\"\n]+)"""' ) |
| 62 |
return r.sub( r'"\1"', text ) |
| 63 |
|
| 64 |
def insertExtraSpaces( text ): |
| 65 |
"Insert extra spaces inside of parentheses and brackets/curly braces." |
| 66 |
lparen = re.compile( r'\((?![\s\)])' ) |
| 67 |
text = lparen.sub( r'( ', text ) |
| 68 |
rparen = re.compile( r'([^\s\(])(?=\))' ) |
| 69 |
text = rparen.sub( r'\1 ', text) |
| 70 |
# brackets |
| 71 |
lbrack = re.compile( r'\[(?![\s\]])' ) |
| 72 |
text = lbrack.sub( r'[ ', text ) |
| 73 |
rbrack = re.compile( r'([^\s\[])(?=\])' ) |
| 74 |
text = rbrack.sub( r'\1 ', text) |
| 75 |
# curly braces |
| 76 |
lcurly = re.compile( r'\{(?![\s\}])' ) |
| 77 |
text = lcurly.sub( r'{ ', text ) |
| 78 |
rcurly = re.compile( r'([^\s\{])(?=\})' ) |
| 79 |
text = rcurly.sub( r'\1 ', text) |
| 80 |
return text |
| 81 |
|
| 82 |
def fixDoxygen( text ): |
| 83 |
"""Translate @param foo to foo:, @return bar to returns: bar, and |
| 84 |
@author me to author: me""" |
| 85 |
param = re.compile( r'@param (\w+)' ) |
| 86 |
text = param.sub( r'\1:', text ) |
| 87 |
returns = re.compile( r'@return' ) |
| 88 |
text = returns.sub( r'returns:', text ) |
| 89 |
author = re.compile( r'@author' ) |
| 90 |
text = author.sub( r'author:', text) |
| 91 |
# @todo -> TODO |
| 92 |
text = text.replace( '@todo', 'TODO' ) |
| 93 |
return text |
| 94 |
|
| 95 |
def removeCommentFirstBlankLine( text ): |
| 96 |
"Remove annoying blank lines after first line in comments." |
| 97 |
line = re.compile( r'("""[^\n]*\n)\s*\n', re.MULTILINE ) |
| 98 |
return line.sub( r'\1', text ) |
| 99 |
|
| 100 |
def fixArgs( match, kwarg = re.compile( r'(\w+) = ' ) ): |
| 101 |
"Replace foo = bar with foo=bar." |
| 102 |
return kwarg.sub( r'\1=', match.group() ) |
| 103 |
|
| 104 |
def fixKeywords( text ): |
| 105 |
"Change keyword argumentsfrom foo = bar to foo=bar." |
| 106 |
args = re.compile( r'\(([^\)]+)\)', re.MULTILINE ) |
| 107 |
return args.sub( fixArgs, text ) |
| 108 |
|
| 109 |
# Unfortunately, Python doesn't natively support balanced or recursive |
| 110 |
# regular expressions. We could use PyParsing, but that opens another can |
| 111 |
# of worms. For now, we just have a cheap hack to restore strings, |
| 112 |
# so we don't end up accidentally mangling things like messages, search strings, |
| 113 |
# and regular expressions. |
| 114 |
|
| 115 |
def lineIter( text ): |
| 116 |
"Simple iterator over lines in text." |
| 117 |
for line in text.splitlines(): yield line |
| 118 |
|
| 119 |
def stringIter( strList ): |
| 120 |
"Yield strings in strList." |
| 121 |
for s in strList: yield s |
| 122 |
|
| 123 |
def restoreRegex( regex, old, new ): |
| 124 |
"Find regexes in old and restore them into new." |
| 125 |
oldStrs = regex.findall( old ) |
| 126 |
# Sanity check - count should be the same! |
| 127 |
newStrs = regex.findall( new ) |
| 128 |
assert len( oldStrs ) == len( newStrs ) |
| 129 |
# Replace newStrs with oldStrs |
| 130 |
siter = stringIter( oldStrs ) |
| 131 |
reps = lambda dummy: siter.next() |
| 132 |
return regex.sub( reps, new ) |
| 133 |
|
| 134 |
# This is a cheap hack, and it may not work 100%, since |
| 135 |
# it doesn't handle multiline strings. |
| 136 |
# However, it should be mostly harmless... |
| 137 |
|
| 138 |
def restoreStrings( oldText, newText ): |
| 139 |
"Restore strings from oldText into newText, returning result." |
| 140 |
oldLines, newLines = lineIter( oldText ), lineIter( newText ) |
| 141 |
quoteStrings = re.compile( r'("[^"]*")' ) |
| 142 |
tickStrings = re.compile( r"('[^']*')" ) |
| 143 |
result = '' |
| 144 |
# It would be nice if we could blast the whole file, but for |
| 145 |
# now it seems to work line-by-line |
| 146 |
for newLine in newLines: |
| 147 |
oldLine = oldLines.next() |
| 148 |
newLine = restoreRegex( quoteStrings, oldLine, newLine ) |
| 149 |
newLine = restoreRegex( tickStrings, oldLine, newLine ) |
| 150 |
result += newLine + '\n' |
| 151 |
return result |
| 152 |
|
| 153 |
# This might be slightly controversial, since it uses |
| 154 |
# three spaces to line up multiline comments. However, |
| 155 |
# I much prefer it. Limitations: if you have deeper |
| 156 |
# indents in comments, they will be eliminated. ;-( |
| 157 |
|
| 158 |
def fixComment( match, |
| 159 |
indentExp=re.compile( r'\n([ ]*)(?=[^/s])', re.MULTILINE ), |
| 160 |
trailingQuotes=re.compile( r'\s+"""' ) ): |
| 161 |
"Re-indent comment, and join trailing quotes." |
| 162 |
originalIndent = match.group( 1 ) |
| 163 |
comment = match.group( 2 ) |
| 164 |
indent = '\n' + originalIndent |
| 165 |
# Exception: leave unindented things unindented! |
| 166 |
if len( originalIndent ) is not 0: indent += ' ' |
| 167 |
comment = indentExp.sub( indent, comment ) |
| 168 |
return originalIndent + trailingQuotes.sub( '"""', comment ) |
| 169 |
|
| 170 |
def fixCommentIndents( text ): |
| 171 |
"Fix multiline comment indentation." |
| 172 |
comments = re.compile( r'^([ ]*)("""[^"]*""")$', re.MULTILINE ) |
| 173 |
return comments.sub( fixComment, text ) |
| 174 |
|
| 175 |
def removeBogusLinefeeds( text ): |
| 176 |
"Remove extra linefeeds at the end of single-line comments." |
| 177 |
bogusLfs = re.compile( r'"([^"\n]*)\n"', re.MULTILINE ) |
| 178 |
return bogusLfs.sub( '"\1"', text) |
| 179 |
|
| 180 |
def convertFromPep8( program ): |
| 181 |
oldProgram = program |
| 182 |
# Program text transforms |
| 183 |
program = reinstateCapWords( program ) |
| 184 |
program = fixKeywords( program ) |
| 185 |
program = insertExtraSpaces( program ) |
| 186 |
# Undo string damage |
| 187 |
program = restoreStrings( oldProgram, program ) |
| 188 |
# Docstring transforms |
| 189 |
program = replaceTripleApostrophes( program ) |
| 190 |
program = simplifyTripleQuotes( program ) |
| 191 |
program = fixDoxygen( program ) |
| 192 |
program = fixCommentIndents( program ) |
| 193 |
program = removeBogusLinefeeds( program ) |
| 194 |
# Destructive transforms (these can delete lines) |
| 195 |
program = removeCommentFirstBlankLine( program ) |
| 196 |
return program |
| 197 |
|
| 198 |
if __name__ == '__main__': |
| 199 |
print( convertFromPep8( sys.stdin.read() ) ) |