|
1 | 1 | # coding=utf-8 |
2 | 2 |
|
3 | 3 | import click |
4 | | -import json |
5 | 4 | import sys |
6 | 5 |
|
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 |
10 | 8 | from jsoncsv.utils import separator_type |
11 | 9 |
|
12 | 10 |
|
|
18 | 16 | type=separator_type, |
19 | 17 | default='.', |
20 | 18 | 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') |
22 | 23 | @click.option( |
23 | 24 | '-r', |
24 | 25 | '--restore', |
25 | | - 'restore_', |
| 26 | + 'restore', |
26 | 27 | is_flag=True, |
27 | 28 | help='restore expanded json') |
28 | 29 | @click.option( |
29 | 30 | '-e', |
30 | 31 | '--expand', |
31 | | - 'expand_', |
| 32 | + 'expand', |
32 | 33 | is_flag=True, |
33 | 34 | help='expand json') |
34 | 35 | @click.argument( |
|
39 | 40 | 'output', |
40 | 41 | type=click.File('w'), |
41 | 42 | 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: |
44 | 45 | raise click.UsageError('can not choose both, default is `-e`') |
45 | 46 |
|
46 | | - func = expand |
47 | | - if restore_: |
48 | | - func = restore |
| 47 | + type = "expand" # default |
| 48 | + if restore: |
| 49 | + type = "restore" |
49 | 50 |
|
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) |
59 | 52 |
|
60 | 53 | input.close() |
61 | 54 | output.close() |
@@ -94,7 +87,7 @@ def mkexcel(output, input, sort_, row, type_): |
94 | 87 | if output == sys.stdout and type_ == "xls": |
95 | 88 | output = click.get_binary_stream('stdout') |
96 | 89 |
|
97 | | - dumpexcel(input, output, type_, read_row=row, sort_type=sort_) |
| 90 | + dump_excel(input, output, type_, read_row=row, sort_type=sort_) |
98 | 91 |
|
99 | 92 | input.close() |
100 | 93 | output.close() |
0 commit comments