Skip to content

Commit 4872114

Browse files
authored
Merge pull request #40 from alingse/use-io-textwraper
Use io textwraper
2 parents 468f285 + 4b82cf8 commit 4872114

7 files changed

Lines changed: 73 additions & 53 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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def restore(expobj, separator='.', safe=False):
135135

136136

137137
def convert_json(fin, fout, func, separator=".", safe=False, json_array=False):
138+
'''
139+
ensure fin/fout is TextIO
140+
'''
141+
138142
if func not in [expand, restore]:
139143
raise ValueError("unknow convert_json type")
140144

@@ -159,9 +163,5 @@ def gen_objs_from_array():
159163
for obj in objs:
160164
new = func(obj, separator=separator, safe=safe)
161165
content = json.dumps(new, ensure_ascii=False)
162-
if PY2:
163-
fout.write(content.encode('utf-8'))
164-
fout.write('\n'.encode('utf-8'))
165-
else:
166-
fout.write(content.encode('utf-8'))
167-
fout.write(str('\n').encode('utf-8'))
166+
fout.write(content)
167+
fout.write('\n')

jsoncsv/main.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010

1111
def separator_type(sep):
1212
if len(sep) != 1:
13-
raise click.BadOptionUsage('separator can only be a char')
13+
raise click.BadOptionUsage(
14+
option_name='separator',
15+
message='separator can only be a char')
1416
if sep == unit_char:
15-
raise click.BadOptionUsage('separator can not be `\\` ')
17+
raise click.BadOptionUsage(
18+
option_name='separator',
19+
message='separator can not be `\\` ')
1620
return sep
1721

1822

@@ -46,21 +50,22 @@ def separator_type(sep):
4650
'--expand',
4751
'expand',
4852
is_flag=True,
49-
help='expand json')
53+
help='expand json (default True)')
5054
@click.argument(
5155
'input',
5256
type=click.File('r', encoding='utf-8'),
5357
default='-')
5458
@click.argument(
5559
'output',
56-
type=click.File('wb'),
60+
type=click.File('w', encoding='utf-8'),
5761
default='-')
5862
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
5963
if expand and restore:
6064
raise click.UsageError('can not choose both, default is `-e`')
6165

62-
func = jsontool.expand
63-
if restore:
66+
if not restore:
67+
func = jsontool.expand
68+
else:
6469
func = jsontool.restore
6570

6671
convert_json(input, output, func, separator=separator, safe=safe, json_array=json_array)

setup.py

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

1313
setup(
1414
name='jsoncsv',
15-
version='2.2.4a',
15+
version='2.2.4',
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
],
@@ -42,13 +42,10 @@
4242
],
4343
},
4444
keywords=[
45+
'jsoncsv',
4546
'jsontocsv',
4647
'json2csv',
47-
'jsoncsv',
48-
'command',
4948
'convert',
50-
'json',
51-
'csv',
52-
'xls',
49+
'json2xls',
5350
],
5451
)
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# coding=utf-8
22
# author@alingse
33
# 2018.03.29
4-
4+
import io
55
import unittest
6-
from io import open
76

87
from jsoncsv.dumptool import DumpCSV
98
from jsoncsv.dumptool import DumpXLS
@@ -14,58 +13,58 @@ class TestDumpTool(unittest.TestCase):
1413

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

2019
dump_excel(fin, fout, DumpCSV)
2120
fin.close()
2221
fout.close()
2322

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')
23+
output = io.open('./fixture/files/output.1.csv', 'r', encoding='utf-8')
24+
fout = io.open('./fixture/files/tmp.output.1.csv', 'r', encoding='utf-8')
2625

2726
self.assertEqual(output.read(), fout.read())
2827

2928
output.close()
3029
fout.close()
3130

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

3635
dump_excel(fin, fout, DumpCSV, sort_type=True)
3736
fin.close()
3837
fout.close()
3938

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')
39+
output = io.open('./fixture/files/output.1.sort.csv', 'r', encoding='utf-8')
40+
fout = io.open('./fixture/files/tmp.output.1.sort.csv', 'r', encoding='utf-8')
4241

4342
self.assertEqual(output.read(), fout.read())
4443

4544
output.close()
4645
fout.close()
4746

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

5251
dump_excel(fin, fout, DumpXLS)
5352

5453
fin.close()
5554
fout.close()
5655

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

6160
dump_excel(fin, fout, DumpCSV)
6261

6362
fin.close()
6463
fout.close()
6564

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

7069
dump_excel(fin, fout, DumpXLS)
7170

tests/test_jsoncsv.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,29 @@
1010

1111

1212
class Testjsoncsv(unittest.TestCase):
13-
1413
def test_jsoncsv_expand(self):
1514
runner = CliRunner()
16-
args = ['-e', 'fixture/files/raw.0.json', 'fixture/files/tmp.expand.0.json']
15+
args = ['-e', 'fixture/files/raw.0.json',
16+
'fixture/files/tmp.expand.0.json']
1717
result = runner.invoke(jsoncsv, args=args)
1818
assert result.exit_code == 0
1919

2020
def test_jsoncsv_expand_with_json_array(self):
2121
runner = CliRunner()
22-
args = ['-e', 'fixture/files/raw.1.json', 'fixture/files/tmp.expand.1.json', '-A']
22+
args = ['-e', 'fixture/files/raw.1.json',
23+
'fixture/files/tmp.expand.1.json', '-A']
2324
result = runner.invoke(jsoncsv, args=args)
2425
assert result.exit_code == 0
2526

2627
def test_jsoncsv_expand_restore(self):
2728
runner = CliRunner(echo_stdin=True)
28-
result = runner.invoke(jsoncsv, args=['-e', 'fixture/files/raw.2.json', 'fixture/files/tmp.expand.2.json'])
29+
result = runner.invoke(jsoncsv,
30+
args=['-e', 'fixture/files/raw.2.json',
31+
'fixture/files/tmp.expand.2.json'])
2932
assert result.exit_code == 0
30-
result = runner.invoke(jsoncsv, args=['-r', 'fixture/files/tmp.expand.2.json', 'fixture/files/tmp.restore.2.json'])
33+
result = runner.invoke(jsoncsv,
34+
args=['-r', 'fixture/files/tmp.expand.2.json',
35+
'fixture/files/tmp.restore.2.json'])
3136
assert result.exit_code == 0
3237

3338
with io.open('fixture/files/raw.2.json', 'r') as f:
@@ -37,3 +42,17 @@ def test_jsoncsv_expand_restore(self):
3742
resotre_data = [json.loads(line) for line in f]
3843

3944
self.assertEqual(input_data, resotre_data)
45+
46+
def test_jsoncsv_with_error_args(self):
47+
runner = CliRunner()
48+
args = ['-s', 'aa', '-e', 'fixture/files/raw.0.json',
49+
'fixture/files/tmp.expand.0.json']
50+
result = runner.invoke(jsoncsv, args=args)
51+
assert result.exit_code != 0
52+
53+
def test_jsoncsv_with_error_args_expand_and_restore(self):
54+
runner = CliRunner()
55+
args = ['-r', '-e', 'fixture/files/raw.0.json',
56+
'fixture/files/tmp.expand.0.json']
57+
result = runner.invoke(jsoncsv, args=args)
58+
assert result.exit_code != 0

tests/test_jsontool.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,45 +104,45 @@ def test_expand_and_restore(self):
104104
class TestConvertJSON(unittest.TestCase):
105105

106106
def test_convert_expand(self):
107-
fin = io.BytesIO(u'{"a":{"b":3}}\n{"a":{"c":4}}\n'.encode('utf-8'))
108-
fout = io.BytesIO()
107+
fin = io.StringIO(u'{"a":{"b":3}}\n{"a":{"c":4}}\n')
108+
fout = io.StringIO()
109109

110110
convert_json(fin, fout, expand)
111111

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

114114
fin.close()
115115
fout.close()
116116

117117
def test_convert_with_unicode(self):
118-
fin = io.BytesIO(u'{"河流":{"长度":3}}\n{"河流":{"名字":"长江"}}\n'.encode('utf-8'))
119-
fout = io.BytesIO()
118+
fin = io.StringIO(u'{"河流":{"长度":3}}\n{"河流":{"名字":"长江"}}\n')
119+
fout = io.StringIO()
120120

121121
convert_json(fin, fout, expand)
122122

123-
self.assertEqual(u'{"河流.长度": 3}\n{"河流.名字": "长江"}\n'.encode('utf-8'), fout.getvalue())
123+
self.assertEqual(u'{"河流.长度": 3}\n{"河流.名字": "长江"}\n', fout.getvalue())
124124

125125
fin.close()
126126
fout.close()
127127

128128
def test_convert_restore(self):
129-
fin = io.BytesIO(u'{"a.b": 3}\n{"a.c": 4}\n'.encode('utf-8'))
130-
fout = io.BytesIO()
129+
fin = io.StringIO(u'{"a.b": 3}\n{"a.c": 4}\n')
130+
fout = io.StringIO()
131131

132132
convert_json(fin, fout, restore)
133133

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

136136
fin.close()
137137
fout.close()
138138

139139
def test_convert_expand_json_array(self):
140-
fin = io.BytesIO(u'[{"a":{"b":3}},{"a":{"c":4}}]'.encode('utf-8'))
141-
fout = io.BytesIO()
140+
fin = io.StringIO(u'[{"a":{"b":3}},{"a":{"c":4}}]')
141+
fout = io.StringIO()
142142

143143
convert_json(fin, fout, expand, json_array=True)
144144

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

147147
fin.close()
148148
fout.close()

0 commit comments

Comments
 (0)