Skip to content

Commit 4f82aa5

Browse files
authored
Merge pull request #29 from alingse/release-2.1.0
try Release 2.1.0
2 parents f9ef2f8 + 486973f commit 4f82aa5

9 files changed

Lines changed: 68 additions & 48 deletions

File tree

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
ignore = E501

README.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
jsoncsv : convert json to csv or xlsx
3-
==========================================
2+
jsoncsv : easily convert json to csv or xlsx
3+
==============================================
44

55
.. image:: https://img.shields.io/pypi/v/jsoncsv.svg
66
:target: https://pypi.python.org/pypi/jsoncsv
@@ -25,6 +25,20 @@ cat the raw.json to csv/xls on command line
2525
cat raw.json |jsoncsv |mkexcel > output.csv
2626
cat raw.json |jsoncsv |mkexcel -t xls > output.xls
2727
28+
or
29+
30+
.. code-block:: bash
31+
32+
jsoncsv raw.json expand.json
33+
mkexcel expand.json -t xls output.xls
34+
35+
more options see --help.
36+
37+
.. code-block:: bash
38+
39+
jsoncsv --help
40+
mkexcel --help
41+
2842
just expand/restore the json
2943

3044
.. code-block:: bash
@@ -46,8 +60,6 @@ safe mod
4660
4761
cat raw.json|jsoncsv --safe|mkexcel > output.csv
4862
49-
jsoncsv --help
50-
mkexcel --help
5163
5264
jsoncsv
5365
>>>>>>>>

jsoncsv/dumptool.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,9 @@ def on_finish(self):
162162
self.wb.save(self.fout)
163163

164164

165-
def dump_excel(fin, fout, type_, **kwargs):
166-
if type_ == 'csv':
167-
DumpKlass = DumpCSV
168-
elif type_ == 'xls':
169-
DumpKlass = DumpXLS
170-
else:
165+
def dump_excel(fin, fout, klass, **kwargs):
166+
if not issubclass(klass, DumpExcel):
171167
raise ValueError("unknow dumpexcel type")
172168

173-
dump = DumpKlass(fin, fout, **kwargs)
169+
dump = klass(fin, fout, **kwargs)
174170
dump.dump()

jsoncsv/jsontool.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
'convert_json',
2020
'expand',
2121
'restore',
22-
'gen_leaf',
23-
'from_leaf',
2422
]
2523

2624

@@ -135,12 +133,8 @@ def restore(expobj, separator='.', safe=False):
135133
return origin
136134

137135

138-
def convert_json(fin, fout, type="expand", separator=".", safe=False):
139-
if type == "expand":
140-
func = expand
141-
elif type == "restore":
142-
func = restore
143-
else:
136+
def convert_json(fin, fout, func, separator=".", safe=False):
137+
if func not in [expand, restore]:
144138
raise ValueError("unknow convert_json type")
145139

146140
for line in fin:

jsoncsv/main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import click
44
import sys
55

6+
from jsoncsv import jsontool
7+
from jsoncsv import dumptool
68
from jsoncsv.dumptool import dump_excel
79
from jsoncsv.jsontool import convert_json
810
from jsoncsv.utils import separator_type
@@ -44,11 +46,11 @@ def jsoncsv(output, input, expand, restore, safe, separator):
4446
if expand and restore:
4547
raise click.UsageError('can not choose both, default is `-e`')
4648

47-
type = "expand" # default
49+
func = jsontool.expand
4850
if restore:
49-
type = "restore"
51+
func = jsontool.restore
5052

51-
convert_json(input, output, type, separator, safe)
53+
convert_json(input, output, func, separator, safe)
5254

5355
input.close()
5456
output.close()
@@ -87,7 +89,11 @@ def mkexcel(output, input, sort_, row, type_):
8789
if output == sys.stdout and type_ == "xls":
8890
output = click.get_binary_stream('stdout')
8991

90-
dump_excel(input, output, type_, read_row=row, sort_type=sort_)
92+
klass = dumptool.DumpCSV
93+
if type_ == "xls":
94+
klass = dumptool.DumpXLS
95+
96+
dump_excel(input, output, klass, read_row=row, sort_type=sort_)
9197

9298
input.close()
9399
output.close()

jsoncsv/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
# author@alingse
33
# 2016.11.20
44

5+
import click
56

67
__all__ = []
78

89
unit_char = '\\'
910

1011

1112
def separator_type(sep):
12-
import click
13-
1413
if len(sep) > 1:
1514
raise click.BadOptionUsage('separator can only be a char')
1615
if sep == unit_char:
@@ -19,7 +18,7 @@ def separator_type(sep):
1918

2019

2120
def encode_safe_key(path, separator):
22-
path = [p.replace(unit_char, unit_char*2) for p in path]
21+
path = [p.replace(unit_char, unit_char * 2) for p in path]
2322
separator = unit_char + separator
2423
return separator.join(path)
2524

setup.py

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

1313
setup(
1414
name='jsoncsv',
15-
version='2.1.0a',
15+
version='2.1.0b',
1616
url='https://github.com/alingse/jsoncsv',
1717
description='A command tool easily convert json file to csv or xlsx.',
1818
long_description=readme,
@@ -38,5 +38,14 @@
3838
'jsoncsv = jsoncsv.main:jsoncsv',
3939
'mkexcel = jsoncsv.main:mkexcel',
4040
],
41-
}
41+
},
42+
keywords=[
43+
'jsontocsv',
44+
'jsoncsv',
45+
'json',
46+
'csv',
47+
'xls'
48+
'convert',
49+
'command',
50+
],
4251
)

tests/test_jsontool.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ def test_list(self):
2929

3030
def test_dict(self):
3131
s = {
32-
"s": 1,
33-
"w": 5,
34-
"t": {
35-
"m": 0,
36-
"x": {
37-
"y": "z"
38-
}
39-
}
40-
}
32+
"s": 1,
33+
"w": 5,
34+
"t": {
35+
"m": 0,
36+
"x": {
37+
"y": "z"
38+
},
39+
},
40+
}
4141

4242
exp = expand(s)
4343
_s = restore(exp)
@@ -46,12 +46,12 @@ def test_dict(self):
4646

4747
def test_complex(self):
4848
s = [
49-
{"s": 0},
50-
{"t": ["2", {"x": "z"}]},
51-
0,
52-
"w",
53-
["x", "g", 1]
54-
]
49+
{"s": 0},
50+
{"t": ["2", {"x": "z"}]},
51+
0,
52+
"w",
53+
["x", "g", 1]
54+
]
5555
exp = expand(s)
5656
_s = restore(exp)
5757

@@ -100,7 +100,7 @@ def test_convert_expand(self):
100100
else:
101101
fout = io.BytesIO()
102102

103-
convert_json(fin, fout)
103+
convert_json(fin, fout, expand)
104104

105105
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
106106

@@ -114,7 +114,7 @@ def test_convert_restore(self):
114114
else:
115115
fout = io.BytesIO()
116116

117-
convert_json(fin, fout, type="restore")
117+
convert_json(fin, fout, restore)
118118

119119
self.assertEqual('{"a": {"b": 3}}\n{"a": {"c": 4}}\n', fout.getvalue())
120120

tests/test_mkexcel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import unittest
66

7+
from jsoncsv.dumptool import DumpCSV
8+
from jsoncsv.dumptool import DumpXLS
79
from jsoncsv.dumptool import dump_excel
810

911

@@ -14,7 +16,7 @@ def test_dumpexcel_csv(self):
1416
fin = open('./fixture/files/expand.1.json', 'r')
1517
fout = open('./fixture/files/tmp.output.1.csv', 'w')
1618

17-
dump_excel(fin, fout, 'csv')
19+
dump_excel(fin, fout, DumpCSV)
1820
fin.close()
1921
fout.close()
2022

@@ -30,7 +32,7 @@ def test_dumpexcel_csv_with_sort(self):
3032
fin = open('./fixture/files/expand.1.json', 'r')
3133
fout = open('./fixture/files/tmp.output.1.sort.csv', 'w')
3234

33-
dump_excel(fin, fout, 'csv', sort_type=True)
35+
dump_excel(fin, fout, DumpCSV, sort_type=True)
3436
fin.close()
3537
fout.close()
3638

@@ -46,7 +48,7 @@ def test_dumpcexcel_xls(self):
4648
fin = open('./fixture/files/expand.1.json', 'r')
4749
fout = open('./fixture/files/tmp.output.1.xls', 'wb')
4850

49-
dump_excel(fin, fout, 'xls')
51+
dump_excel(fin, fout, DumpXLS)
5052

5153
fin.close()
5254
fout.close()

0 commit comments

Comments
 (0)