11# coding=utf-8
22import click
33
4- from jsoncsv import jsontool
5- from jsoncsv import dumptool
4+ from jsoncsv import dumptool , jsontool
65from jsoncsv .dumptool import dump_excel
76from jsoncsv .jsontool import convert_json
87from jsoncsv .utils import unit_char
98
109
1110def separator_type (sep ):
1211 if len (sep ) != 1 :
13- raise click .BadOptionUsage (
14- option_name = 'separator' ,
15- message = 'separator can only be a char' )
12+ raise click .BadOptionUsage (option_name = 'separator' ,
13+ message = 'separator can only be a char' )
1614 if sep == unit_char :
17- raise click .BadOptionUsage (
18- option_name = 'separator' ,
19- message = 'separator can not be `\\ ` ' )
15+ raise click .BadOptionUsage (option_name = 'separator' ,
16+ message = 'separator can not be `\\ ` ' )
2017 return sep
2118
2219
2320@click .command ()
24- @click .option (
25- '-A' ,
26- '--array' ,
27- 'json_array' ,
28- is_flag = True ,
29- default = False ,
30- help = 'read input file as json array' )
31- @click .option (
32- '-s' ,
33- '--sep' ,
34- 'separator' ,
35- type = separator_type ,
36- default = '.' ,
37- help = 'separator' )
38- @click .option (
39- '--safe' ,
40- is_flag = True ,
41- help = 'use safe mode' )
42- @click .option (
43- '-r' ,
44- '--restore' ,
45- 'restore' ,
46- is_flag = True ,
47- help = 'restore expanded json' )
48- @click .option (
49- '-e' ,
50- '--expand' ,
51- 'expand' ,
52- is_flag = True ,
53- help = 'expand json (default True)' )
54- @click .argument (
55- 'input' ,
56- type = click .File ('r' , encoding = 'utf-8' ),
57- default = '-' )
58- @click .argument (
59- 'output' ,
60- type = click .File ('w' , encoding = 'utf-8' ),
61- default = '-' )
21+ @click .option ('-A' ,
22+ '--array' ,
23+ 'json_array' ,
24+ is_flag = True ,
25+ default = False ,
26+ help = 'read input file as json array' )
27+ @click .option ('-s' ,
28+ '--sep' ,
29+ 'separator' ,
30+ type = separator_type ,
31+ default = '.' ,
32+ help = 'separator' )
33+ @click .option ('--safe' , is_flag = True , help = 'use safe mode' )
34+ @click .option ('-r' ,
35+ '--restore' ,
36+ 'restore' ,
37+ is_flag = True ,
38+ help = 'restore expanded json' )
39+ @click .option ('-e' ,
40+ '--expand' ,
41+ 'expand' ,
42+ is_flag = True ,
43+ help = 'expand json (default True)' )
44+ @click .argument ('input' , type = click .File ('r' , encoding = 'utf-8' ), default = '-' )
45+ @click .argument ('output' , type = click .File ('w' , encoding = 'utf-8' ), default = '-' )
6246def jsoncsv (output , input , expand , restore , safe , separator , json_array ):
6347 if expand and restore :
6448 raise click .UsageError ('can not choose both, default is `-e`' )
@@ -68,41 +52,37 @@ def jsoncsv(output, input, expand, restore, safe, separator, json_array):
6852 else :
6953 func = jsontool .restore
7054
71- convert_json (input , output , func , separator = separator , safe = safe , json_array = json_array )
55+ convert_json (input ,
56+ output ,
57+ func ,
58+ separator = separator ,
59+ safe = safe ,
60+ json_array = json_array )
7261
7362 input .close ()
7463 output .close ()
7564
7665
7766@click .command ()
78- @click .option (
79- '-t' ,
80- '--type' ,
81- 'type_' ,
82- type = click .Choice (['csv' , 'xls' ]),
83- default = 'csv' ,
84- help = 'choose dump format' )
85- @click .option (
86- '-r' ,
87- '--row' ,
88- type = int ,
89- default = None ,
90- help = 'number of pre-read `row` lines to load `headers`' )
91- @click .option (
92- '-s' ,
93- '--sort' ,
94- 'sort_' ,
95- is_flag = True ,
96- default = False ,
97- help = 'enable sort the headers keys' )
98- @click .argument (
99- 'input' ,
100- type = click .File ('r' , encoding = 'utf-8' ),
101- default = '-' )
102- @click .argument (
103- 'output' ,
104- type = click .File ('wb' ),
105- default = '-' )
67+ @click .option ('-t' ,
68+ '--type' ,
69+ 'type_' ,
70+ type = click .Choice (['csv' , 'xls' ]),
71+ default = 'csv' ,
72+ help = 'choose dump format' )
73+ @click .option ('-r' ,
74+ '--row' ,
75+ type = int ,
76+ default = None ,
77+ help = 'number of pre-read `row` lines to load `headers`' )
78+ @click .option ('-s' ,
79+ '--sort' ,
80+ 'sort_' ,
81+ is_flag = True ,
82+ default = False ,
83+ help = 'enable sort the headers keys' )
84+ @click .argument ('input' , type = click .File ('r' , encoding = 'utf-8' ), default = '-' )
85+ @click .argument ('output' , type = click .File ('wb' ), default = '-' )
10686def mkexcel (output , input , sort_ , row , type_ ):
10787 klass = dumptool .DumpCSV
10888 if type_ == "xls" :
0 commit comments