author | Matt Mackall <mpm@selenic.com> |
Wed, 25 Aug 2010 15:33:06 -0500 | |
changeset 12042 | 210049a8d16e |
parent 11749 | e627fef94604 |
child 19872 | 681f7b9213a4 |
permissions | -rw-r--r-- |
11749
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
1 |
"""Fixer that changes bytes % whatever to a function that actually formats |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
2 |
it.""" |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
3 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
4 |
from lib2to3 import fixer_base |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
5 |
from lib2to3.fixer_util import is_tuple, Call, Comma, Name, touch_import |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
6 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
7 |
# XXX: Implementing a blacklist in 2to3 turned out to be more troublesome than |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
8 |
# blacklisting some modules inside the fixers. So, this is what I came with. |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
9 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
10 |
blacklist = ['mercurial/demandimport.py', |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
11 |
'mercurial/py3kcompat.py', |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
12 |
'mercurial/i18n.py', |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
13 |
] |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
14 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
15 |
def isnumberremainder(formatstr, data): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
16 |
try: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
17 |
if data.value.isdigit(): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
18 |
return True |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
19 |
except AttributeError: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
20 |
return False |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
21 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
22 |
class FixBytesmod(fixer_base.BaseFix): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
23 |
# XXX: There's one case (I suppose) I can't handle: when a remainder |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
24 |
# operation like foo % bar is performed, I can't really know what the |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
25 |
# contents of foo and bar are. I believe the best approach is to "correct" |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
26 |
# the to-be-converted code and let bytesformatter handle that case in |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
27 |
# runtime. |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
28 |
PATTERN = ''' |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
29 |
term< formatstr=STRING '%' data=STRING > | |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
30 |
term< formatstr=STRING '%' data=atom > | |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
31 |
term< formatstr=NAME '%' data=any > | |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
32 |
term< formatstr=any '%' data=any > |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
33 |
''' |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
34 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
35 |
def transform(self, node, results): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
36 |
if self.filename in blacklist: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
37 |
return |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
38 |
elif self.filename == 'mercurial/util.py': |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
39 |
touch_import('.', 'py3kcompat', node=node) |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
40 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
41 |
formatstr = results['formatstr'].clone() |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
42 |
data = results['data'].clone() |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
43 |
formatstr.prefix = '' # remove spaces from start |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
44 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
45 |
if isnumberremainder(formatstr, data): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
46 |
return |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
47 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
48 |
# We have two possibilities: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
49 |
# 1- An identifier or name is passed, it is going to be a leaf, thus, we |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
50 |
# just need to copy its value as an argument to the formatter; |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
51 |
# 2- A tuple is explicitly passed. In this case, we're gonna explode it |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
52 |
# to pass to the formatter |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
53 |
# TODO: Check for normal strings. They don't need to be translated |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
54 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
55 |
if is_tuple(data): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
56 |
args = [formatstr, Comma().clone()] + \ |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
57 |
[c.clone() for c in data.children[:]] |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
58 |
else: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
59 |
args = [formatstr, Comma().clone(), data] |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
60 |
|
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
61 |
call = Call(Name('bytesformatter', prefix = ' '), args) |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
62 |
return call |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
63 |