Skip to content

Commit f6e9141

Browse files
committed
CLI: Add --encoding option
* Add `--encoding` option with default utf-8 * Make sure input and output are in same encoding * Add test cases Signed-off-by: Tao Wang <[email protected]>
1 parent c92e281 commit f6e9141

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

sqlparse/cli.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import argparse
2323
import sys
24+
from io import TextIOWrapper
25+
from codecs import open, getreader
2426

2527
import sqlparse
2628
from sqlparse.compat import PY2
@@ -125,6 +127,12 @@ def create_parser():
125127
type=bool,
126128
help='Insert linebreak before comma (default False)')
127129

130+
group.add_argument(
131+
'--encoding',
132+
dest='encoding',
133+
default='utf-8',
134+
help='Specify the input encoding (default utf-8)')
135+
128136
return parser
129137

130138

@@ -139,18 +147,21 @@ def main(args=None):
139147
args = parser.parse_args(args)
140148

141149
if args.filename == '-': # read from stdin
142-
data = sys.stdin.read()
150+
if PY2:
151+
data = getreader(args.encoding)(sys.stdin).read()
152+
else:
153+
data = TextIOWrapper(
154+
sys.stdin.buffer, encoding=args.encoding).read()
143155
else:
144156
try:
145-
# TODO: Needs to deal with encoding
146-
data = ''.join(open(args.filename).readlines())
157+
data = ''.join(open(args.filename, 'r', args.encoding).readlines())
147158
except IOError as e:
148159
return _error(
149160
u'Failed to read {0}: {1}'.format(args.filename, e))
150161

151162
if args.outfile:
152163
try:
153-
stream = open(args.outfile, 'w')
164+
stream = open(args.outfile, 'w', args.encoding)
154165
except IOError as e:
155166
return _error(u'Failed to open {0}: {1}'.format(args.outfile, e))
156167
else:
@@ -163,8 +174,6 @@ def main(args=None):
163174
return _error(u'Invalid options: {0}'.format(e))
164175

165176
s = sqlparse.format(data, **formatter_opts)
166-
if PY2:
167-
s = s.encode('utf-8', 'replace')
168177
stream.write(s)
169178
stream.flush()
170179
return 0

tests/files/encoding_gbk.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
select *
2+
from foo
3+
where bar = '²»ÒÔÎïϲ£¬²»ÒÔ¼º±¯'

tests/files/encoding_utf8.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
select *
2+
from foo
3+
where bar = '齐天大圣.カラフルな雲.사랑해요'

tests/test_cli.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,63 @@ def test_script():
7373
# Call with the --help option as a basic sanity check.
7474
cmd = "{0:s} -m sqlparse.cli --help".format(sys.executable)
7575
assert subprocess.call(cmd.split()) == 0
76+
77+
78+
def test_encoding_utf8_stdout(filepath, load_file, capfd):
79+
path = filepath('encoding_utf8.sql')
80+
expected = load_file('encoding_utf8.sql', 'utf-8')
81+
sys.stdout.encoding = 'utf-8'
82+
sqlparse.cli.main([path])
83+
out, _ = capfd.readouterr()
84+
assert out == expected
85+
86+
87+
def test_encoding_utf8_output_file(filepath, load_file, tmpdir):
88+
in_path = filepath('encoding_utf8.sql')
89+
expected = load_file('encoding_utf8.sql', 'utf-8')
90+
out_path = tmpdir.dirname + '/encoding_utf8.out.sql'
91+
sqlparse.cli.main([in_path, '-o', out_path])
92+
out = load_file(out_path, 'utf-8')
93+
assert out == expected
94+
95+
96+
def test_encoding_gbk_stdout(filepath, load_file, capfd):
97+
path = filepath('encoding_gbk.sql')
98+
expected = load_file('encoding_gbk.sql', 'gbk')
99+
sys.stdout.encoding = 'gbk'
100+
sqlparse.cli.main([path, '--encoding', 'gbk'])
101+
out, _ = capfd.readouterr()
102+
assert out == expected
103+
104+
105+
def test_encoding_gbk_output_file(filepath, load_file, tmpdir):
106+
in_path = filepath('encoding_gbk.sql')
107+
expected = load_file('encoding_gbk.sql', 'gbk')
108+
out_path = tmpdir.dirname + '/encoding_gbk.out.sql'
109+
sqlparse.cli.main([in_path, '--encoding', 'gbk', '-o', out_path])
110+
out = load_file(out_path, 'gbk')
111+
assert out == expected
112+
113+
114+
def test_encoding_stdin_utf8(filepath, load_file, capfd):
115+
path = filepath('encoding_utf8.sql')
116+
expected = load_file('encoding_utf8.sql', 'utf-8')
117+
old_stdin = sys.stdin
118+
sys.stdin = open(path, 'r')
119+
sys.stdout.encoding = 'utf-8'
120+
sqlparse.cli.main(['-'])
121+
sys.stdin = old_stdin
122+
out, _ = capfd.readouterr()
123+
assert out == expected
124+
125+
126+
def test_encoding_stdin_gbk(filepath, load_file, capfd):
127+
path = filepath('encoding_gbk.sql')
128+
expected = load_file('encoding_gbk.sql', 'gbk')
129+
old_stdin = sys.stdin
130+
sys.stdin = open(path, 'r')
131+
sys.stdout.encoding = 'gbk'
132+
sqlparse.cli.main(['-', '--encoding', 'gbk'])
133+
sys.stdin = old_stdin
134+
out, _ = capfd.readouterr()
135+
assert out == expected

0 commit comments

Comments
 (0)