Show More
@@ -19,7 +19,7 b' import token' | |||
|
19 | 19 | import tokenize |
|
20 | 20 | |
|
21 | 21 | if True: |
|
22 | def replacetokens(tokens): | |
|
22 | def replacetokens(tokens, opts): | |
|
23 | 23 | """Transform a stream of tokens from raw to Python 3. |
|
24 | 24 | |
|
25 | 25 | Returns a generator of possibly rewritten tokens. |
@@ -129,16 +129,16 b' if True:' | |||
|
129 | 129 | |
|
130 | 130 | # It changes iteritems/values to items/values as they are not |
|
131 | 131 | # present in Python 3 world. |
|
132 | elif fn in ('iteritems', 'itervalues'): | |
|
132 | elif opts['dictiter'] and fn in ('iteritems', 'itervalues'): | |
|
133 | 133 | yield t._replace(string=fn[4:]) |
|
134 | 134 | continue |
|
135 | 135 | |
|
136 | 136 | # Emit unmodified token. |
|
137 | 137 | yield t |
|
138 | 138 | |
|
139 | def process(fin, fout): | |
|
139 | def process(fin, fout, opts): | |
|
140 | 140 | tokens = tokenize.tokenize(fin.readline) |
|
141 | tokens = replacetokens(list(tokens)) | |
|
141 | tokens = replacetokens(list(tokens), opts) | |
|
142 | 142 | fout.write(tokenize.untokenize(tokens)) |
|
143 | 143 | |
|
144 | 144 | def tryunlink(fname): |
@@ -168,17 +168,22 b' def main():' | |||
|
168 | 168 | ap = argparse.ArgumentParser() |
|
169 | 169 | ap.add_argument('-i', '--inplace', action='store_true', default=False, |
|
170 | 170 | help='edit files in place') |
|
171 | ap.add_argument('--dictiter', action='store_true', default=False, | |
|
172 | help='rewrite iteritems() and itervalues()'), | |
|
171 | 173 | ap.add_argument('files', metavar='FILE', nargs='+', help='source file') |
|
172 | 174 | args = ap.parse_args() |
|
175 | opts = { | |
|
176 | 'dictiter': args.dictiter, | |
|
177 | } | |
|
173 | 178 | for fname in args.files: |
|
174 | 179 | if args.inplace: |
|
175 | 180 | with editinplace(fname) as fout: |
|
176 | 181 | with open(fname, 'rb') as fin: |
|
177 | process(fin, fout) | |
|
182 | process(fin, fout, opts) | |
|
178 | 183 | else: |
|
179 | 184 | with open(fname, 'rb') as fin: |
|
180 | 185 | fout = sys.stdout.buffer |
|
181 | process(fin, fout) | |
|
186 | process(fin, fout, opts) | |
|
182 | 187 | |
|
183 | 188 | if __name__ == '__main__': |
|
184 | 189 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now