byteify-strings: add basic command interface
authorYuya Nishihara <yuya@tcha.org>
Thu, 31 May 2018 22:23:30 +0900
changeset 38391 a2976c27dac4
parent 38390 1d9c97db465f
child 38392 9f42e4a83676
byteify-strings: add basic command interface
contrib/byteify-strings.py
--- a/contrib/byteify-strings.py	Thu May 31 22:07:04 2018 +0900
+++ b/contrib/byteify-strings.py	Thu May 31 22:23:30 2018 +0900
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3
+#
 # byteify-strings.py - transform string literals to be Python 3 safe
 #
 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com>
@@ -7,7 +9,9 @@
 
 from __future__ import absolute_import
 
+import argparse
 import io
+import sys
 import token
 import tokenize
 
@@ -152,3 +156,20 @@
 
             # Emit unmodified token.
             yield t
+
+def process(fin, fout):
+    tokens = tokenize.tokenize(fin.readline)
+    tokens = replacetokens(list(tokens), fullname='<dummy>')
+    fout.write(tokenize.untokenize(tokens))
+
+def main():
+    ap = argparse.ArgumentParser()
+    ap.add_argument('files', metavar='FILE', nargs='+', help='source file')
+    args = ap.parse_args()
+    for fname in args.files:
+        with open(fname, 'rb') as fin:
+            fout = sys.stdout.buffer
+            process(fin, fout)
+
+if __name__ == '__main__':
+    main()