Skip to content

Commit 468f285

Browse files
authored
Merge pull request #38 from alingse/try-fix-windows
Try fix windows
2 parents d202832 + 676be11 commit 468f285

6 files changed

Lines changed: 42 additions & 45 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cache: pip
33
sudo: false
44
python:
55
- "2.7"
6-
- "3.5"
6+
# - "3.5"
77
- "3.6"
88
- "3.7"
99
install:

jsoncsv/jsontool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,5 @@ def gen_objs_from_array():
163163
fout.write(content.encode('utf-8'))
164164
fout.write('\n'.encode('utf-8'))
165165
else:
166-
fout.write(content)
167-
fout.write('\n')
166+
fout.write(content.encode('utf-8'))
167+
fout.write(str('\n').encode('utf-8'))

jsoncsv/main.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# coding=utf-8
2-
32
import click
4-
import sys
53

64
from jsoncsv import jsontool
75
from jsoncsv import dumptool
@@ -51,12 +49,12 @@ def separator_type(sep):
5149
help='expand json')
5250
@click.argument(
5351
'input',
54-
type=click.File('r'),
55-
default=sys.stdin)
52+
type=click.File('r', encoding='utf-8'),
53+
default='-')
5654
@click.argument(
5755
'output',
58-
type=click.File('w'),
59-
default=sys.stdout)
56+
type=click.File('wb'),
57+
default='-')
6058
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
6159
if expand and restore:
6260
raise click.UsageError('can not choose both, default is `-e`')
@@ -94,16 +92,13 @@ def jsoncsv(output, input, expand, restore, safe, separator, json_array):
9492
help='enable sort the headers keys')
9593
@click.argument(
9694
'input',
97-
type=click.File('r'),
98-
default=sys.stdin)
95+
type=click.File('r', encoding='utf-8'),
96+
default='-')
9997
@click.argument(
10098
'output',
10199
type=click.File('wb'),
102-
default=sys.stdout)
100+
default='-')
103101
def mkexcel(output, input, sort_, row, type_):
104-
if output == sys.stdout:
105-
output = click.get_binary_stream('stdout')
106-
107102
klass = dumptool.DumpCSV
108103
if type_ == "xls":
109104
klass = dumptool.DumpXLS

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='jsoncsv',
15-
version='2.2.3',
15+
version='2.2.4a',
1616
url='https://github.com/alingse/jsoncsv',
1717
description='A command tool easily convert json file to csv or xlsx.',
1818
long_description=readme,
@@ -26,7 +26,7 @@
2626
'Programming Language :: Python :: 2',
2727
'Programming Language :: Python :: 2.7',
2828
'Programming Language :: Python :: 3',
29-
'Programming Language :: Python :: 3.5',
29+
# 'Programming Language :: Python :: 3.5',
3030
'Programming Language :: Python :: 3.6',
3131
'Programming Language :: Python :: 3.7',
3232
],

tests/test_jsontool.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io
77
import unittest
88

9-
from jsoncsv import PY2
109
from jsoncsv.jsontool import expand, restore
1110
from jsoncsv.jsontool import is_array_index
1211
from jsoncsv.jsontool import convert_json
@@ -105,43 +104,45 @@ def test_expand_and_restore(self):
105104
class TestConvertJSON(unittest.TestCase):
106105

107106
def test_convert_expand(self):
108-
fin = io.StringIO('{"a":{"b":3}}\n{"a":{"c":4}}\n')
109-
if PY2:
110-
fout = io.BytesIO()
111-
else:
112-
fout = io.StringIO()
107+
fin = io.BytesIO(u'{"a":{"b":3}}\n{"a":{"c":4}}\n'.encode('utf-8'))
108+
fout = io.BytesIO()
113109

114110
convert_json(fin, fout, expand)
115111

116-
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
112+
self.assertEqual(b'{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
113+
114+
fin.close()
115+
fout.close()
116+
117+
def test_convert_with_unicode(self):
118+
fin = io.BytesIO(u'{"河流":{"长度":3}}\n{"河流":{"名字":"长江"}}\n'.encode('utf-8'))
119+
fout = io.BytesIO()
120+
121+
convert_json(fin, fout, expand)
122+
123+
self.assertEqual(u'{"河流.长度": 3}\n{"河流.名字": "长江"}\n'.encode('utf-8'), fout.getvalue())
117124

118125
fin.close()
119126
fout.close()
120127

121128
def test_convert_restore(self):
122-
fin = io.StringIO('{"a.b": 3}\n{"a.c": 4}\n')
123-
if PY2:
124-
fout = io.BytesIO()
125-
else:
126-
fout = io.StringIO()
129+
fin = io.BytesIO(u'{"a.b": 3}\n{"a.c": 4}\n'.encode('utf-8'))
130+
fout = io.BytesIO()
127131

128132
convert_json(fin, fout, restore)
129133

130-
self.assertEqual('{"a": {"b": 3}}\n{"a": {"c": 4}}\n', fout.getvalue())
134+
self.assertEqual(b'{"a": {"b": 3}}\n{"a": {"c": 4}}\n', fout.getvalue())
131135

132136
fin.close()
133137
fout.close()
134138

135139
def test_convert_expand_json_array(self):
136-
fin = io.StringIO('[{"a":{"b":3}},{"a":{"c":4}}]')
137-
if PY2:
138-
fout = io.BytesIO()
139-
else:
140-
fout = io.StringIO()
140+
fin = io.BytesIO(u'[{"a":{"b":3}},{"a":{"c":4}}]'.encode('utf-8'))
141+
fout = io.BytesIO()
141142

142143
convert_json(fin, fout, expand, json_array=True)
143144

144-
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
145+
self.assertEqual(b'{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
145146

146147
fin.close()
147148
fout.close()

tests/test_mkexcel.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# 2018.03.29
44

55
import unittest
6+
from io import open
67

78
from jsoncsv.dumptool import DumpCSV
89
from jsoncsv.dumptool import DumpXLS
@@ -13,39 +14,39 @@ class TestDumpTool(unittest.TestCase):
1314

1415
# FIXME (使用虚拟文件)
1516
def test_dumpexcel_csv(self):
16-
fin = open('./fixture/files/expand.1.json', 'r')
17+
fin = open('./fixture/files/expand.1.json', 'r', encoding='utf-8')
1718
fout = open('./fixture/files/tmp.output.1.csv', 'wb')
1819

1920
dump_excel(fin, fout, DumpCSV)
2021
fin.close()
2122
fout.close()
2223

23-
output = open('./fixture/files/output.1.csv', 'r')
24-
fout = open('./fixture/files/tmp.output.1.csv', 'r')
24+
output = open('./fixture/files/output.1.csv', 'r', encoding='utf-8')
25+
fout = open('./fixture/files/tmp.output.1.csv', 'r', encoding='utf-8')
2526

2627
self.assertEqual(output.read(), fout.read())
2728

2829
output.close()
2930
fout.close()
3031

3132
def test_dumpexcel_csv_with_sort(self):
32-
fin = open('./fixture/files/expand.1.json', 'r')
33+
fin = open('./fixture/files/expand.1.json', 'r', encoding='utf-8')
3334
fout = open('./fixture/files/tmp.output.1.sort.csv', 'wb')
3435

3536
dump_excel(fin, fout, DumpCSV, sort_type=True)
3637
fin.close()
3738
fout.close()
3839

39-
output = open('./fixture/files/output.1.sort.csv', 'r')
40-
fout = open('./fixture/files/tmp.output.1.sort.csv', 'r')
40+
output = open('./fixture/files/output.1.sort.csv', 'r', encoding='utf-8')
41+
fout = open('./fixture/files/tmp.output.1.sort.csv', 'r', encoding='utf-8')
4142

4243
self.assertEqual(output.read(), fout.read())
4344

4445
output.close()
4546
fout.close()
4647

4748
def test_dumpcexcel_xls(self):
48-
fin = open('./fixture/files/expand.1.json', 'r')
49+
fin = open('./fixture/files/expand.1.json', 'r', encoding='utf-8')
4950
fout = open('./fixture/files/tmp.output.1.xls', 'wb')
5051

5152
dump_excel(fin, fout, DumpXLS)
@@ -54,7 +55,7 @@ def test_dumpcexcel_xls(self):
5455
fout.close()
5556

5657
def test_dump_csv_with_non_ascii(self):
57-
fin = open('./fixture/files/expand.2.json', 'r')
58+
fin = open('./fixture/files/expand.2.json', 'r', encoding='utf-8')
5859
fout = open('./fixture/files/tmp.output.2.csv', 'wb')
5960

6061
dump_excel(fin, fout, DumpCSV)
@@ -63,7 +64,7 @@ def test_dump_csv_with_non_ascii(self):
6364
fout.close()
6465

6566
def test_dump_xls_with_non_ascii(self):
66-
fin = open('./fixture/files/expand.2.json', 'r')
67+
fin = open('./fixture/files/expand.2.json', 'r', encoding='utf-8')
6768
fout = open('./fixture/files/tmp.output.2.xls', 'wb')
6869

6970
dump_excel(fin, fout, DumpXLS)

0 commit comments

Comments
 (0)