# coding=utf-8

from __future__ import print_function

import argparse
import json
import sys

from jsoncsv.jsontool import expand, restore
from jsoncsv.dumptool import dumpfile


def load_jsontool_parse():
    parser = argparse.ArgumentParser()

    def separator_type(string):
        if len(string) > 1:
            msg = 'separator can only be a char'
            raise argparse.ArgumentTypeError(msg)
        if string == '\\':
            msg = 'separator can not be `\\` '
            raise argparse.ArgumentTypeError(msg)
        return string

    parser.add_argument('-s',
                        '--separator',
                        action='store',
                        help='the separator for join keys',
                        type=separator_type,
                        default='.')
    parser.add_argument('--safe',
                        action='store_true',
                        help='use safe mode. key1.key2 --> key1\\.key2')
    parser.add_argument('-e',
                        '--expand',
                        action='store_true',
                        help='choose `expand` a json')
    parser.add_argument('-r',
                        '--restore',
                        action='store_true',
                        help='choose `contract` a ｀expanded` json')
    parser.add_argument('-o',
                        '--output',
                        help='file for output, default is stdout')
    parser.add_argument('input',
                        nargs='?',
                        help='input file, default is stdin')

    return parser


def load_mkexcel_parse():
    parser = argparse.ArgumentParser()
    parser.add_argument('-t',
                        '--type',
                        choices=['csv', 'xls'],
                        default='csv',
                        help='choose dump format')
    parser.add_argument('input',
                        nargs='?',
                        help='input file, default is stdin')
    parser.add_argument('output',
                        nargs='?',
                        help='output file, default is stdout')
    return parser


def jsoncsv():
    parser = load_jsontool_parse()
    args = parser.parse_args()

    if args.expand and args.restore:
        print('can not choose both, default is `-e`', file=sys.stderr)
        exit()
    elif args.expand:
        func = expand
    elif args.restore:
        func = restore
    else:
        func = expand

    fin = sys.stdin
    fout = sys.stdout

    if args.input is not None:
        fin = open(args.input, 'r')
    if args.output is not None:
        fout = open(args.output, 'w')

    safe = args.safe
    separator = args.separator

    for line in fin:
        obj = json.loads(line)
        new = func(obj, separator=separator, safe=safe)
        out = json.dumps(new, ensure_ascii=False).encode('utf-8')
        fout.write(out)
        fout.write('\n')

    fin.close()
    fout.close()


def mkexcel():
    parser = load_mkexcel_parse()
    args = parser.parse_args()

    fin = sys.stdin
    fout = sys.stdout

    if args.input is not None:
        fin = open(args.input, 'r')

    if args.output is not None:
        fout = open(args.output, 'w')

    type_ = args.type

    dumpfile(fin, type_, fout)

    fin.close()
    fout.close()

@@ -1673,32 +1673,16 @@
 t('-t',%0A
-                
         
@@ -1691,32 +1691,16 @@
 -type',%0A
-                
         
@@ -1735,90 +1735,237 @@
     
-                default='csv',%0A                        help='choose dump format
+default='csv',%0A        help='choose dump format')%0A%0A    parser.add_argument(%0A        '-r',%0A        '--row',%0A        type=int,%0A        default=None,%0A        help='pre read %60row%60 lines to load %60headers%60'%0A        ' default is all
 ')%0A
+%0A
     
@@ -3346,16 +3346,35 @@
 rgs.type
+%0A    row = args.row
 %0A%0A    du
@@ -3396,16 +3396,30 @@
 e_, fout
+, read_row=row
 )%0A%0A    f

添加 预读文件行数 可控