|
4 | 4 |
|
5 | 5 | from __future__ import print_function |
6 | 6 |
|
| 7 | +import io |
7 | 8 | import os |
8 | 9 | import re |
9 | 10 | import sys |
|
13 | 14 |
|
14 | 15 |
|
15 | 16 | try: |
16 | | - from cStringIO import StringIO |
| 17 | + from StringIO import StringIO |
17 | 18 | except ImportError: |
18 | 19 | from io import StringIO |
19 | 20 |
|
@@ -401,7 +402,7 @@ def __init__(self, filename, template=None): |
401 | 402 | if sys.platform == 'win32': |
402 | 403 | self.filename = self.filename.replace('\\', '/') |
403 | 404 | if template is None: |
404 | | - with open(filename) as f: |
| 405 | + with io.open(filename, encoding='utf-8') as f: |
405 | 406 | self.template = f.read() |
406 | 407 | else: |
407 | 408 | self.template = template |
@@ -736,8 +737,10 @@ def execute(self, context): |
736 | 737 | result_string = None |
737 | 738 | if isinstance(result, Number) and not isinstance(result, Integral): |
738 | 739 | result_string = repr(result) |
739 | | - else: |
| 740 | + elif isinstance(result, Integral): |
740 | 741 | result_string = str(result) |
| 742 | + else: |
| 743 | + result_string = StringIO(result).read() |
741 | 744 | context.append_text( |
742 | 745 | result_string, self.filename, self.start_line_number) |
743 | 746 |
|
@@ -803,7 +806,7 @@ def expand(filename, line_directive=_default_line_directive, **local_bindings): |
803 | 806 | >>> f.close() |
804 | 807 | >>> os.remove(f.name) |
805 | 808 | """ |
806 | | - with open(filename) as f: |
| 809 | + with io.open(filename, encoding='utf-8') as f: |
807 | 810 | t = parse_template(filename, f.read()) |
808 | 811 | d = os.getcwd() |
809 | 812 | os.chdir(os.path.dirname(os.path.abspath(filename))) |
@@ -1208,12 +1211,12 @@ def succ(a): |
1208 | 1211 | help='''Bindings to be set in the template's execution context''') |
1209 | 1212 |
|
1210 | 1213 | parser.add_argument( |
1211 | | - 'file', type=argparse.FileType(), |
| 1214 | + 'file', type=str, |
1212 | 1215 | help='Path to GYB template file (defaults to stdin)', nargs='?', |
1213 | | - default=sys.stdin) |
| 1216 | + default='-') |
1214 | 1217 | parser.add_argument( |
1215 | | - '-o', dest='target', type=argparse.FileType('w'), |
1216 | | - help='Output file (defaults to stdout)', default=sys.stdout) |
| 1218 | + '-o', dest='target', type=str, |
| 1219 | + help='Output file (defaults to stdout)', default='-') |
1217 | 1220 | parser.add_argument( |
1218 | 1221 | '--test', action='store_true', |
1219 | 1222 | default=False, help='Run a self-test') |
@@ -1245,15 +1248,23 @@ def succ(a): |
1245 | 1248 | sys.exit(1) |
1246 | 1249 |
|
1247 | 1250 | bindings = dict(x.split('=', 1) for x in args.defines) |
1248 | | - ast = parse_template(args.file.name, args.file.read()) |
| 1251 | + if args.file == '-': |
| 1252 | + ast = parse_template('stdin', sys.stdin.read()) |
| 1253 | + else: |
| 1254 | + with io.open(args.file, 'r', encoding='utf-8') as f: |
| 1255 | + ast = parse_template(args.file, f.read()) |
1249 | 1256 | if args.dump: |
1250 | 1257 | print(ast) |
1251 | 1258 | # Allow the template to open files and import .py files relative to its own |
1252 | 1259 | # directory |
1253 | | - os.chdir(os.path.dirname(os.path.abspath(args.file.name))) |
| 1260 | + os.chdir(os.path.dirname(os.path.abspath(args.file))) |
1254 | 1261 | sys.path = ['.'] + sys.path |
1255 | 1262 |
|
1256 | | - args.target.write(execute_template(ast, args.line_directive, **bindings)) |
| 1263 | + if args.target == '-': |
| 1264 | + sys.stdout.write(execute_template(ast, args.line_directive, **bindings)) |
| 1265 | + else: |
| 1266 | + with io.open(args.target, 'w', encoding='utf-8', newline='\n') as f: |
| 1267 | + f.write(execute_template(ast, args.line_directive, **bindings)) |
1257 | 1268 |
|
1258 | 1269 |
|
1259 | 1270 | if __name__ == '__main__': |
|
0 commit comments