3
3
# this one's short). See -h output for instructions
4
4
5
5
from __future__ import print_function
6
- from __future__ import unicode_literals
7
6
8
7
import re
9
8
try :
10
- from StringIO import StringIO
9
+ from cStringIO import StringIO
11
10
except ImportError :
12
11
from io import StringIO
13
12
import tokenize
14
13
import textwrap
15
14
from bisect import bisect
16
15
import os
17
- from io import open
18
16
19
17
def getLineStarts (s ):
20
18
"""Return a list containing the start index of each line in s.
@@ -373,7 +371,7 @@ class ParseContext:
373
371
def __init__ (self , filename , template = None ):
374
372
self .filename = os .path .abspath (filename )
375
373
if template is None :
376
- with open (filename , 'r' , encoding = 'utf-8' ) as f :
374
+ with open (filename ) as f :
377
375
self .template = f .read ()
378
376
else :
379
377
self .template = template
@@ -1047,8 +1045,8 @@ def succ(a):
1047
1045
help = '''Bindings to be set in the template's execution context'''
1048
1046
)
1049
1047
1050
- parser .add_argument ('file' , help = 'Path to GYB template file (defaults to stdin)' , nargs = '?' , default = sys .stdin . fileno () )
1051
- parser .add_argument ('-o' , dest = 'target' , help = 'Output file (defaults to stdout)' , default = sys .stdout . fileno () )
1048
+ parser .add_argument ('file' , type = argparse . FileType (), help = 'Path to GYB template file (defaults to stdin)' , nargs = '?' , default = sys .stdin )
1049
+ parser .add_argument ('-o' , dest = 'target' , type = argparse . FileType ( 'w' ), help = 'Output file (defaults to stdout)' , default = sys .stdout )
1052
1050
parser .add_argument ('--test' , action = 'store_true' , default = False , help = 'Run a self-test' )
1053
1051
parser .add_argument ('--verbose-test' , action = 'store_true' , default = False , help = 'Run a verbose self-test' )
1054
1052
parser .add_argument ('--dump' , action = 'store_true' , default = False , help = 'Dump the parsed template to stdout' )
@@ -1063,14 +1061,14 @@ def succ(a):
1063
1061
sys .exit (1 )
1064
1062
1065
1063
bindings = dict ( x .split ('=' , 1 ) for x in args .defines )
1066
- ast = parseTemplate (str ( args .file ), open ( args .file , 'r' , encoding = 'utf-8' ) .read ())
1064
+ ast = parseTemplate (args .file . name , args .file .read ())
1067
1065
if args .dump :
1068
1066
1069
1067
print (ast )
1070
1068
# Allow the template to import .py files from its own directory
1071
- sys .path = [os .path .split (str ( args .file ) )[0 ] or '.' ] + sys .path
1072
-
1073
- open ( args .target , 'w+' , encoding = 'utf-8' ) .write (executeTemplate (ast , args .line_directive , ** bindings ))
1069
+ sys .path = [os .path .split (args .file . name )[0 ] or '.' ] + sys .path
1070
+
1071
+ args .target .write (executeTemplate (ast , args .line_directive , ** bindings ))
1074
1072
1075
1073
if __name__ == '__main__' :
1076
1074
main ()
0 commit comments