def do_geography(filename):
    # geography changed slightly with Bauble 1.0 so just copy the
    # default file and hope the ids are the same
    rootdir = os.path.split(bauble.__file__)[0]
    default_file = os.path.join(rootdir, 'plugins', 'plants', 'default',
                                'geography.txt')
    default_reader = UnicodeReader(open(default_file))
    geography_reader = UnicodeReader(open(filename))
    columns = [
        'id', 'name', 'tdwg_code', 'iso_code', 'parent_id', '_created',
        '_last_updated'
    ]

    writer = create_writer(os.path.join(dst_path, filename), columns)

    # build map of default tdwg codes to geography.id
    default_tdwg_map = {}
    for line in default_reader:
        default_tdwg_map[line['tdwg_code']] = line['id']

    # built a map of the default geography id's to 0.9 geography.id's
    for line in geography_reader:
        # TODO: this might miss some id's and might need to be
        # corrected but it works for us at the moment
        try:
            default_id = default_tdwg_map[line['tdwg_code']]
        except:
            pass
            # print line
            # print line['tdwg_code'].partition('-')
            # head, middle, tail = line['tdwg_code'].partition('-')
            # default_id = default_tdwg_map[head]
        geography_id_map[line['id']] = default_id

    # manually add 'Cultivated'
    geography_id_map[None] = default_tdwg_map[None]

    # now that the map is built just copy over the default geography
    shutil.copy(default_file, dst_path)
def do_genus(filename):
    """
    Convert genus.txt
    """

    reader = UnicodeReader(open(filename))

    genus_filename = os.path.join(dst_path, 'genus.txt')
    genus_writer = UnicodeWriter(open(genus_filename, "wb"))
    genus_columns = [
        'id', 'epithet', 'hybrid_marker', 'author', 'aggregate', 'family_id',
        '_created', '_last_updated'
    ]
    genus_writer.writerow(genus_columns)

    for line in reader:
        genus_writer.writerow([
            line['id'], line['genus'], '', line['author'], line['qualifier'],
            line['family_id'], line['_created'], line['_last_updated']
        ])
    print 'done'
def do_species(filename):
    """
    Convert species.txt
    """
    reader = UnicodeReader(open(filename))

    species_filename = os.path.join(dst_path, 'species.txt')
    writer = UnicodeWriter(open(species_filename, "wb"))
    columns = [
        'id', 'epithet', 'hybrid_marker', 'author', 'aggregate', 'cv_group',
        'trade_name', 'infrasp1', 'infrasp1_rank', 'infrasp1_author',
        'infrasp2', 'infrasp2_rank', 'infrasp2_author', 'infrasp3',
        'infrasp3_rank', 'infrasp3_author', 'infrasp4', 'infrasp4_rank',
        'infrasp4_author', 'genus_id', 'label_distribution', 'bc_distribution',
        'habit_id', 'flower_color_id', 'awards', '_created', '_last_updated'
    ]
    writer.writerow(columns)

    post_process_hyop = []
    for line in reader:
        line = dict(line)
        if line['sp'] and (line['sp'].find(u'×') > 0):
            line['hybrid_marker'] = 'H'
            line['epithet'] = ''
            post_process_hyop.append(dict(line))
        else:
            line['hybrid_marker'] = (line['hybrid'] == 'True') and u'×' or ''
            line['epithet'] = line['sp']
        line['author'] = line['sp_author']
        line['aggregate'] = line['sp_qual']
        writer.writerow([line[k] for k in columns])

    hybrid_operand = os.path.join(dst_path, 'hybrid_operands.txt')
    hyop_columns = ['child_id', 'parent_id', 'role']
    hyop_writer = create_writer(hybrid_operand, hyop_columns)
    if post_process_hyop:
        print post_process_hyop

    print 'done'