

def main(**kwargs):
    if (len(kwargs) == 0):
        args = parser.parse_args(sys.argv[1:])
    else:
        args = parser.parse_args(kwargs)
    try:
        (project, folderpath, entity_result) = resolve_existing_path(args.path, expected='entity')
    except ResolutionError as details:
        parser.exit(1, (fill(str(details)) + '\n'))
    if (entity_result is None):
        parser.exit(1, (fill((('Could not resolve ' + args.path) + ' to a data object')) + '\n'))
    filename = args.output
    if (filename is None):
        filename = entity_result['describe']['name'].replace('/', '%2F')
    dxtable = dxpy.get_handler(entity_result['id'])
    delimiter = (',' if args.csv else '\t')
    if (args.output == '-'):
        writer = csv.writer(sys.stdout, delimiter=delimiter)
    else:
        if ((args.output is None) and (not args.no_ext)):
            filename += ('.csv' if args.csv else '.tsv')
        if ((not args.overwrite) and os.path.exists(filename)):
            parser.exit(1, (fill((('Error: path "' + filename) + '" already exists but -f/--overwrite was not set')) + '\n'))
        writer = csv.writer(open(filename, 'wb'), delimiter=delimiter)
    if (not args.no_header):
        writer.writerow(((['__id__:int'] if args.rowid else []) + [((col['name'] + ':') + col['type']) for col in dxtable.describe()['columns']]))
    if (args.gri is not None):
        try:
            lo = int(args.gri[1])
            hi = int(args.gri[2])
        except:
            parser.exit(1, (fill('Error: the LO and HI arguments to --gri must be integers') + '\n'))
        gri_query = dxpy.DXGTable.genomic_range_query(args.gri[0], lo, hi, args.gri_mode, args.gri_name)
        iterator = dxtable.iterate_query_rows(query=gri_query, limit=args.limit)
    else:
        iterator = dxtable.iterate_rows(start=args.starting, end=(None if (args.limit is None) else (args.starting + args.limit)))
    for row in iterator:
        writer.writerow([str(item).encode('utf-8') for item in row[(0 if args.rowid else 1):]])

dataset/ETHPy150Open dnanexus/dx-toolkit/src/python/dxpy/scripts/dx_gtable_to_tsv.py main/original