12
12
13
13
def load_jsontool_parse ():
14
14
parser = argparse .ArgumentParser ()
15
+
16
+ def separator_type (string ):
17
+ if len (string ) > 1 :
18
+ msg = 'separator can only be a char'
19
+ raise argparse .ArgumentTypeError (msg )
20
+ if string == '\\ ' :
21
+ msg = 'separator can not be `\\ ` '
22
+ raise argparse .ArgumentTypeError (msg )
23
+ return string
24
+
15
25
parser .add_argument ('-s' ,
16
26
'--separator' ,
17
27
action = 'store' ,
18
28
help = 'the separator for join keys' ,
29
+ type = separator_type ,
19
30
default = '.' )
20
31
parser .add_argument ('--safe' ,
21
32
action = 'store_true' ,
@@ -61,26 +72,22 @@ def jsoncsv():
61
72
if args .expand and args .restore :
62
73
print ('can not choose both, default is `-e`' , file = sys .stderr )
63
74
exit ()
75
+ elif args .expand :
76
+ func = expand
77
+ elif args .restore :
78
+ func = restore
64
79
else :
65
80
func = expand
66
81
82
+ fin = sys .stdin
83
+ fout = sys .stdout
84
+
67
85
if args .input is not None :
68
86
fin = open (args .input , 'r' )
69
- else :
70
- fin = sys .stdin
71
-
72
87
if args .output is not None :
73
- fout = open (args .output , 'w' )
74
- else :
75
- fout = sys .stdout
76
-
77
- if args .expand :
78
- func = expand
79
- if args .restore :
80
- func = restore
88
+ fout = open (args .output , 'w' )
81
89
82
90
safe = args .safe
83
-
84
91
separator = args .separator
85
92
86
93
for line in fin :
@@ -94,24 +101,22 @@ def jsoncsv():
94
101
def mkexcel ():
95
102
parser = load_mkexcel_parse ()
96
103
args = parser .parse_args ()
104
+
105
+ dumpf = dump_csv
97
106
98
107
if args .type == 'csv' :
99
108
dumpf = dump_csv
100
- elif args .type == 'xls' :
109
+ if args .type == 'xls' :
101
110
dumpf = dump_xls
102
- else :
103
- # can't reach here
104
- print ('can not reach here' , file = sys .stderr )
105
- exit ()
111
+
112
+ fin = sys .stdin
113
+ fout = sys .stdout
106
114
107
115
if args .input is not None :
108
116
fin = open (args .input , 'r' )
109
- else :
110
- fin = sys .stdin
111
117
112
118
if args .output is not None :
113
119
fout = open (args .output , 'w' )
114
- else :
115
- fout = sys .stdout
120
+
116
121
117
122
dumpfile (fin , fout , dumpf )
0 commit comments