Skip to content

Commit f9ef2f8

Browse files
authored
Merge pull request #28 from alingse/add-convert-json
add convert json
2 parents f6c36fb + c1d7179 commit f9ef2f8

9 files changed

Lines changed: 103 additions & 36 deletions

File tree

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
language: python
2+
cache: pip
23
sudo: false
34
python:
45
- "2.7"
56
- "3.5"
67
- "3.6"
7-
88
install:
99
- pip install .
1010
- pip install flake8
1111
- pip install coverage
1212
- pip install coveralls
13-
1413
script:
1514
- flake8 jsoncsv tests --max-line-length=200
1615
- nosetests --cover-package=jsoncsv --with-coverage
1716
- python setup.py install
1817
- cat fixture/files/raw.0.json|jsoncsv -e > fixture/files/tmp.expand.0.json
18+
- cat fixture/files/raw.0.json|jsoncsv -e|jsoncsv -r|jsoncsv > fixture/files/tmp.expand.0.json
1919
- cat fixture/files/raw.2.json|jsoncsv|mkexcel -t xls > fixture/files/tmp.output.2.xls
2020
- cat fixture/files/raw.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2..csv
21-
2221
after_success:
23-
- coveralls
22+
- coveralls
23+
notifications:
24+
email: false

jsoncsv/dumptool.py

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

164164

165-
def dumpexcel(fin, fout, type_, **kwargs):
165+
def dump_excel(fin, fout, type_, **kwargs):
166166
if type_ == 'csv':
167167
DumpKlass = DumpCSV
168168
elif type_ == 'xls':
169169
DumpKlass = DumpXLS
170+
else:
171+
raise ValueError("unknow dumpexcel type")
170172

171173
dump = DumpKlass(fin, fout, **kwargs)
172174
dump.dump()

jsoncsv/jsontool.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# author@alingse
33
# 2016.05.27
44
from __future__ import absolute_import
5+
from __future__ import unicode_literals
6+
7+
import json
58

69
from copy import deepcopy
710
from itertools import groupby
@@ -13,6 +16,7 @@
1316

1417

1518
__all__ = [
19+
'convert_json',
1620
'expand',
1721
'restore',
1822
'gen_leaf',
@@ -129,3 +133,22 @@ def restore(expobj, separator='.', safe=False):
129133

130134
origin = from_leaf(leafs)
131135
return origin
136+
137+
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:
144+
raise ValueError("unknow convert_json type")
145+
146+
for line in fin:
147+
obj = json.loads(line)
148+
new = func(obj, separator=separator, safe=safe)
149+
content = json.dumps(new, ensure_ascii=False)
150+
if PY3:
151+
fout.write(content)
152+
else:
153+
fout.write(content.encode('utf-8'))
154+
fout.write(str('\n'))

jsoncsv/main.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# coding=utf-8
22

33
import click
4-
import json
54
import sys
65

7-
from jsoncsv import PY3
8-
from jsoncsv.jsontool import expand, restore
9-
from jsoncsv.dumptool import dumpexcel
6+
from jsoncsv.dumptool import dump_excel
7+
from jsoncsv.jsontool import convert_json
108
from jsoncsv.utils import separator_type
119

1210

@@ -18,17 +16,20 @@
1816
type=separator_type,
1917
default='.',
2018
help='separator')
21-
@click.option('--safe', is_flag=True, help='use safe mode')
19+
@click.option(
20+
'--safe',
21+
is_flag=True,
22+
help='use safe mode')
2223
@click.option(
2324
'-r',
2425
'--restore',
25-
'restore_',
26+
'restore',
2627
is_flag=True,
2728
help='restore expanded json')
2829
@click.option(
2930
'-e',
3031
'--expand',
31-
'expand_',
32+
'expand',
3233
is_flag=True,
3334
help='expand json')
3435
@click.argument(
@@ -39,23 +40,15 @@
3940
'output',
4041
type=click.File('w'),
4142
default=sys.stdout)
42-
def jsoncsv(output, input, expand_, restore_, safe, separator):
43-
if expand_ and restore_:
43+
def jsoncsv(output, input, expand, restore, safe, separator):
44+
if expand and restore:
4445
raise click.UsageError('can not choose both, default is `-e`')
4546

46-
func = expand
47-
if restore_:
48-
func = restore
47+
type = "expand" # default
48+
if restore:
49+
type = "restore"
4950

50-
for line in input:
51-
obj = json.loads(line)
52-
new = func(obj, separator=separator, safe=safe)
53-
content = json.dumps(new, ensure_ascii=False)
54-
if PY3:
55-
output.write(content)
56-
else:
57-
output.write(content.encode('utf-8'))
58-
output.write('\n')
51+
convert_json(input, output, type, separator, safe)
5952

6053
input.close()
6154
output.close()
@@ -94,7 +87,7 @@ def mkexcel(output, input, sort_, row, type_):
9487
if output == sys.stdout and type_ == "xls":
9588
output = click.get_binary_stream('stdout')
9689

97-
dumpexcel(input, output, type_, read_row=row, sort_type=sort_)
90+
dump_excel(input, output, type_, read_row=row, sort_type=sort_)
9891

9992
input.close()
10093
output.close()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='jsoncsv',
15-
version='2.0.9',
15+
version='2.1.0a',
1616
url='https://github.com/alingse/jsoncsv',
1717
description='A command tool easily convert json file to csv or xlsx.',
1818
long_description=readme,

tests/test_escape.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Testescape(unittest.TestCase):
1111

1212
def test_all(self):
13-
path = ['A', 'B', '..', '\.\\ww']
13+
path = ['A', 'B', '..', '\\.\\ww']
1414

1515
for sep in 'AB.w':
1616
key = encode_safe_key(path, sep)
@@ -23,10 +23,10 @@ def test_encode(self):
2323
sep = '.'
2424
key = encode_safe_key(path, sep)
2525

26-
self.assertEqual(key, 'A\.B\.C\.www.xxx.com')
26+
self.assertEqual(key, 'A\\.B\\.C\\.www.xxx.com')
2727

2828
def test_decode(self):
29-
key = 'A\.B\.C\.www.xxx.com'
29+
key = 'A\\.B\\.C\\.www.xxx.com'
3030
sep = '.'
3131
path = decode_safe_key(key, sep)
3232

tests/test_jsoncsv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ def test_jsoncsv_expand(self):
1414
runner = CliRunner()
1515
args = ['-e', 'fixture/files/raw.0.json', 'fixture/files/tmp.expand.0.json']
1616
result = runner.invoke(jsoncsv, args=args)
17+
print(result)
1718
assert result.exit_code == 0

tests/test_jsontool.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# coding=utf-8
22
# author@alingse
33
# 2016.08.09
4+
from __future__ import unicode_literals
45

6+
import io
57
import unittest
68

9+
from jsoncsv import PY3
710
from jsoncsv.jsontool import expand, restore
811
from jsoncsv.jsontool import is_array
12+
from jsoncsv.jsontool import convert_json
913

1014

11-
class Testjsontool(unittest.TestCase):
15+
class TestJSONTool(unittest.TestCase):
1216

1317
def test_string(self):
1418
s = "sss"
@@ -73,3 +77,46 @@ def test_unicode(self):
7377

7478
expobj = expand(data)
7579
assert expobj
80+
81+
def test_expand_with_safe(self):
82+
data = {
83+
"www.a.com": {"qps": 100, "p95": 20},
84+
"api.a.com": {"qps": 100, "p95": 20, "p99": 100},
85+
}
86+
expobj = expand(data, safe=True)
87+
self.assertEqual(expobj['api.a.com\\.p95'], 20)
88+
self.assertEqual(expobj['api.a.com\\.p99'], 100)
89+
90+
origin = restore(expobj, safe=True)
91+
self.assertEqual(origin, data)
92+
93+
94+
class TestConvertJSON(unittest.TestCase):
95+
96+
def test_convert_expand(self):
97+
fin = io.StringIO('{"a":{"b":3}}\n{"a":{"c":4}}\n')
98+
if PY3:
99+
fout = io.StringIO()
100+
else:
101+
fout = io.BytesIO()
102+
103+
convert_json(fin, fout)
104+
105+
self.assertEqual('{"a.b": 3}\n{"a.c": 4}\n', fout.getvalue())
106+
107+
fin.close()
108+
fout.close()
109+
110+
def test_convert_restore(self):
111+
fin = io.StringIO('{"a.b": 3}\n{"a.c": 4}\n')
112+
if PY3:
113+
fout = io.StringIO()
114+
else:
115+
fout = io.BytesIO()
116+
117+
convert_json(fin, fout, type="restore")
118+
119+
self.assertEqual('{"a": {"b": 3}}\n{"a": {"c": 4}}\n', fout.getvalue())
120+
121+
fin.close()
122+
fout.close()

tests/test_mkexcel.py

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

55
import unittest
66

7-
from jsoncsv.dumptool import dumpexcel
7+
from jsoncsv.dumptool import dump_excel
88

99

1010
class Testdumptool(unittest.TestCase):
@@ -14,7 +14,7 @@ def test_dumpexcel_csv(self):
1414
fin = open('./fixture/files/expand.1.json', 'r')
1515
fout = open('./fixture/files/tmp.output.1.csv', 'w')
1616

17-
dumpexcel(fin, fout, 'csv')
17+
dump_excel(fin, fout, 'csv')
1818
fin.close()
1919
fout.close()
2020

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

33-
dumpexcel(fin, fout, 'csv', sort_type=True)
33+
dump_excel(fin, fout, 'csv', sort_type=True)
3434
fin.close()
3535
fout.close()
3636

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

49-
dumpexcel(fin, fout, 'xls')
49+
dump_excel(fin, fout, 'xls')
5050

5151
fin.close()
5252
fout.close()

0 commit comments

Comments
 (0)