def main(args):
    with DwCAReader(args.source_file) as dwca:
        if valid_dwca(dwca):
            ordered_fields = list(dwca.core_terms)
            # Only last part as Shapefiles field names are limited to 10 chars
            ordered_fields_truncated = [
                f.rsplit('/')[-1] for f in ordered_fields
            ]
            import pdb
            pdb.set_trace()

            with ShapefileOutput(args.destination, args.crs,
                                 ordered_fields_truncated) as out:
                for line in dwca.each_line():
                    try:
                        gis_data = dwcaline_to_epsg4326(line)
                        additional_values = [
                            unicode_to_ascii(line.data[f])
                            for f in ordered_fields
                        ]

                        out.insert_line(gis_data['lat'], gis_data['lon'],
                                        additional_values)
                        sys.stdout.write('.')
                    except CannotConvertException:
                        pass
def main():
    # TODO: Better CLI Help
    # TODO: split into parse_args() and main(args) (see dwca2shp)
    parser = argparse.ArgumentParser(
        description="Import a DarwinCore Archive file into CartDB.")

    parser.add_argument('--domain',
                        help='Your CartoDB domain (without .cartodb.com).',
                        required=True)
    parser.add_argument('--api-key', 
                        dest='apikey',
                        help='Your CartoDB API key.',
                        required=True)
    parser.add_argument('--table',
                        help="CartoDB destination table name",
                        required=True)
    parser.add_argument('--truncate-table',
                        action='store_true',
                        dest='truncate',
                        help="Truncate destination table prior to import.")
    parser.add_argument('source_file',
                        help="Source DwC-A file", 
                        type=argparse.FileType('r'))

    args = parser.parse_args()

    target_table = args.table

    out = CartoDBOutput(args.apikey, args.domain, args.table)

    if args.truncate:
        if query_yes_no("Are you sure you want to truncate the database ? Data will be LOST !", default="no"):
            out.truncate_table()

    with DwCAReader(args.source_file) as dwca:
        if valid_dwca(dwca):    
            for line in dwca.each_line():
                try:
                    out.insert_line(**dwcaline_to_epsg4326(line))
                    sys.stdout.write('.')
                except CartoDBException as e:
                    print ("CartoDB error: ", e)
        else:
            # TODO: more detailed message
            print "Invalid source DwC-A file."
def main(args):
    with DwCAReader(args.source_file) as dwca:
        if valid_dwca(dwca):
            ordered_fields = list(dwca.core_terms)
            # Only last part as Shapefiles field names are limited to 10 chars
            ordered_fields_truncated = [f.rsplit('/')[-1] for f in ordered_fields]
            import pdb; pdb.set_trace()

            with ShapefileOutput(args.destination, args.crs, ordered_fields_truncated) as out:
                for line in dwca.each_line():
                    try:
                        gis_data = dwcaline_to_epsg4326(line)
                        additional_values = [unicode_to_ascii(line.data[f]) for f in ordered_fields]

                        out.insert_line(gis_data['lat'], gis_data['lon'], additional_values)
                        sys.stdout.write('.')
                    except CannotConvertException:
                        pass