1515
1616def separator_type (sep : str ) -> str :
1717 if len (sep ) != 1 :
18- raise click .BadOptionUsage (option_name = 'separator' ,
19- message = 'separator can only be a char' )
18+ raise click .BadOptionUsage (option_name = "separator" , message = "separator can only be a char" )
2019 if sep == unit_char :
21- raise click .BadOptionUsage (option_name = 'separator' ,
22- message = 'separator can not be `\\ ` ' )
20+ raise click .BadOptionUsage (option_name = "separator" , message = "separator can not be `\\ ` " )
2321 return sep
2422
2523
2624@click .command ()
27- @click .option ('-A' ,
28- '--array' ,
29- 'json_array' ,
30- is_flag = True ,
31- default = False ,
32- help = 'read input file as json array' )
33- @click .option ('-s' ,
34- '--sep' ,
35- 'separator' ,
36- type = separator_type ,
37- default = '.' ,
38- help = 'separator' )
39- @click .option ('--safe' , is_flag = True , help = 'use safe mode' )
40- @click .option ('-r' ,
41- '--restore' ,
42- 'restore' ,
43- is_flag = True ,
44- help = 'restore expanded json' )
45- @click .option ('-e' ,
46- '--expand' ,
47- 'expand' ,
48- is_flag = True ,
49- help = 'expand json (default True)' )
50- @click .argument ('input' , type = click .File ('r' , encoding = 'utf-8' ), default = '-' )
51- @click .argument ('output' , type = click .File ('w' , encoding = 'utf-8' ), default = '-' )
25+ @click .option (
26+ "-A" , "--array" , "json_array" , is_flag = True , default = False , help = "read input file as json array"
27+ )
28+ @click .option ("-s" , "--sep" , "separator" , type = separator_type , default = "." , help = "separator" )
29+ @click .option ("--safe" , is_flag = True , help = "use safe mode" )
30+ @click .option ("-r" , "--restore" , "restore" , is_flag = True , help = "restore expanded json" )
31+ @click .option ("-e" , "--expand" , "expand" , is_flag = True , help = "expand json (default True)" )
32+ @click .argument ("input" , type = click .File ("r" , encoding = "utf-8" ), default = "-" )
33+ @click .argument ("output" , type = click .File ("w" , encoding = "utf-8" ), default = "-" )
5234def jsoncsv (
5335 output : io .TextIOBase ,
5436 input : io .TextIOBase ,
@@ -59,42 +41,34 @@ def jsoncsv(
5941 json_array : bool ,
6042) -> None :
6143 if expand and restore :
62- raise click .UsageError (' can not choose both, default is `-e`' )
44+ raise click .UsageError (" can not choose both, default is `-e`" )
6345
6446 func : Callable [..., Any ]
6547 func = expand_fn if not restore else restore_fn
6648
67- convert_json (input ,
68- output ,
69- func ,
70- separator = separator ,
71- safe = safe ,
72- json_array = json_array )
49+ convert_json (input , output , func , separator = separator , safe = safe , json_array = json_array )
7350
7451 input .close ()
7552 output .close ()
7653
7754
7855@click .command ()
79- @click .option ('-t' ,
80- '--type' ,
81- 'type_' ,
82- type = click .Choice (['csv' , 'xls' ]),
83- default = 'csv' ,
84- help = 'choose dump format' )
85- @click .option ('-r' ,
86- '--row' ,
87- type = int ,
88- default = None ,
89- help = 'number of pre-read `row` lines to load `headers`' )
90- @click .option ('-s' ,
91- '--sort' ,
92- 'sort_' ,
93- is_flag = True ,
94- default = False ,
95- help = 'enable sort the headers keys' )
96- @click .argument ('input' , type = click .File ('r' , encoding = 'utf-8' ), default = '-' )
97- @click .argument ('output' , type = click .Path (), default = '-' )
56+ @click .option (
57+ "-t" ,
58+ "--type" ,
59+ "type_" ,
60+ type = click .Choice (["csv" , "xls" ]),
61+ default = "csv" ,
62+ help = "choose dump format" ,
63+ )
64+ @click .option (
65+ "-r" , "--row" , type = int , default = None , help = "number of pre-read `row` lines to load `headers`"
66+ )
67+ @click .option (
68+ "-s" , "--sort" , "sort_" , is_flag = True , default = False , help = "enable sort the headers keys"
69+ )
70+ @click .argument ("input" , type = click .File ("r" , encoding = "utf-8" ), default = "-" )
71+ @click .argument ("output" , type = click .Path (), default = "-" )
9872def mkexcel (
9973 output : str ,
10074 input : io .TextIOBase ,
@@ -107,13 +81,13 @@ def mkexcel(
10781 klass = dumptool .DumpXLS
10882
10983 # Open file in appropriate mode based on type
110- if output == '-' :
111- fout : Any = sys .stdout .buffer if type_ == ' xls' else sys .stdout
84+ if output == "-" :
85+ fout : Any = sys .stdout .buffer if type_ == " xls" else sys .stdout
11286 dump_excel (input , fout , klass , read_row = row , sort_type = sort_ )
11387 else :
114- mode = 'wb' if type_ == ' xls' else 'w'
115- encoding = None if type_ == ' xls' else ' utf-8'
116- newline = '' if type_ == ' csv' else None
88+ mode = "wb" if type_ == " xls" else "w"
89+ encoding = None if type_ == " xls" else " utf-8"
90+ newline = "" if type_ == " csv" else None
11791 with open (output , mode , encoding = encoding , newline = newline ) as fout :
11892 dump_excel (input , fout , klass , read_row = row , sort_type = sort_ )
11993
0 commit comments