Skip to content

Commit cd370e2

Browse files
committed
添加一些限制, 精简函数
1 parent bf2b221 commit cd370e2

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

jsoncsv/main.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212

1313
def load_jsontool_parse():
1414
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+
1525
parser.add_argument('-s',
1626
'--separator',
1727
action='store',
1828
help='the separator for join keys',
29+
type=separator_type,
1930
default='.')
2031
parser.add_argument('--safe',
2132
action='store_true',
@@ -61,26 +72,22 @@ def jsoncsv():
6172
if args.expand and args.restore:
6273
print('can not choose both, default is `-e`', file=sys.stderr)
6374
exit()
75+
elif args.expand:
76+
func = expand
77+
elif args.restore:
78+
func = restore
6479
else:
6580
func = expand
6681

82+
fin = sys.stdin
83+
fout = sys.stdout
84+
6785
if args.input is not None:
6886
fin = open(args.input, 'r')
69-
else:
70-
fin = sys.stdin
71-
7287
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')
8189

8290
safe = args.safe
83-
8491
separator = args.separator
8592

8693
for line in fin:
@@ -94,24 +101,22 @@ def jsoncsv():
94101
def mkexcel():
95102
parser = load_mkexcel_parse()
96103
args = parser.parse_args()
104+
105+
dumpf = dump_csv
97106

98107
if args.type == 'csv':
99108
dumpf = dump_csv
100-
elif args.type == 'xls':
109+
if args.type == 'xls':
101110
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
106114

107115
if args.input is not None:
108116
fin = open(args.input, 'r')
109-
else:
110-
fin = sys.stdin
111117

112118
if args.output is not None:
113119
fout = open(args.output, 'w')
114-
else:
115-
fout = sys.stdout
120+
116121

117122
dumpfile(fin, fout, dumpf)

0 commit comments

Comments
 (0)