Patchwork [2,of,9] byteify-strings: add basic command interface

login
register
mail settings
Submitter Yuya Nishihara
Date June 18, 2018, 1:17 p.m.
Message ID <27476a7a2bbcd1608579.1529327820@mimosa>
Download mbox | patch
Permalink /patch/32270/
State Accepted
Headers show

Comments

Yuya Nishihara - June 18, 2018, 1:17 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1527773010 -32400
#      Thu May 31 22:23:30 2018 +0900
# Node ID 27476a7a2bbcd16085794977001a189114a114f0
# Parent  1daa265ac761404a6aec3404dabdad01aac1ab76
byteify-strings: add basic command interface

Patch

diff --git a/contrib/byteify-strings.py b/contrib/byteify-strings.py
old mode 100644
new mode 100755
--- a/contrib/byteify-strings.py
+++ b/contrib/byteify-strings.py
@@ -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 @@  if True:
 
             # 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()