def main():
    sess = GlobalDB.db().session
    now = datetime.datetime.now()
    parser = argparse.ArgumentParser(description='Pull data from the FPDS Atom Feed.')
    parser.add_argument('-a', '--all', help='Clear out the database and get historical data', action='store_true')
    parser.add_argument('-l', '--latest', help='Get by last_mod_date stored in DB', action='store_true')
    parser.add_argument('-d', '--delivery', help='Used in conjunction with -a to indicate delivery order feed', action='store_true')
    parser.add_argument('-o', '--other', help='Used in conjunction with -a to indicate all feeds other than delivery order', action='store_true')
    parser.add_argument('-da', '--dates', help='Used in conjunction with -l to specify dates to gather updates from.Should have 2 arguments, first and last day, formatted YYYY/mm/dd', nargs=2, type=str)
    parser.add_argument('-del', '--delete', help='Used to only run the delete feed. First argument must be "both", "idv", or "award". The second and third arguments must be the first and last day to run the feeds for, formatted YYYY/mm/dd', nargs=3, type=str)
    args = parser.parse_args()
    award_types_award = ['BPA Call', 'Definitive Contract', 'Purchase Order', 'Delivery Order']
    award_types_idv = ['GWAC', 'BOA', 'BPA', 'FSS', 'IDC']
    metrics_json = {'script_name': 'pull_fpds_data.py', 'start_time': str(now), 'records_received': 0, 'deletes_received': 0, 'records_deleted': 0, 'deleted_award_records_file': '', 'deleted_idv_records_file': '', 'start_date': '', 'end_date': ''}
    (sub_tier_list, country_list, state_code_list, county_by_name, county_by_code, exec_comp_dict) = create_lookups(sess)
    if args.all:
        if (((not args.delivery) and (not args.other)) or (args.delivery and args.other)):
            logger.error('When using the -a flag, please include either -d or -o (but not both) to indicate which feeds to read in')
            raise ValueError('When using the -a flag, please include either -d or -o (but not both) to indicate which feeds to read in')
        logger.info('Starting at: %s', str(datetime.datetime.now()))
        if args.other:
            for award_type in award_types_idv:
                get_data('IDV', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, metrics=metrics_json)
            for award_type in award_types_award:
                if (award_type != 'Delivery Order'):
                    get_data('award', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, metrics=metrics_json)
        elif args.delivery:
            get_data('award', 'Delivery Order', now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, metrics=metrics_json)
        last_update = sess.query(FPDSUpdate).one_or_none()
        if last_update:
            sess.query(FPDSUpdate).update({'update_date': now}, synchronize_session=False)
        else:
            sess.add(FPDSUpdate(update_date=now))
        sess.commit()
        logger.info('Ending at: %s', str(datetime.datetime.now()))
    elif args.latest:
        logger.info('Starting at: %s', str(datetime.datetime.now()))
        last_update_obj = sess.query(FPDSUpdate).one_or_none()
        if (not last_update_obj):
            logger.error('No last_update date present, please run the script with the -a flag to generate an initial dataset')
            raise ValueError('No last_update date present, please run the script with the -a flag to generate an initial dataset')
        last_update = last_update_obj.update_date
        start_date = None
        end_date = None
        if args.dates:
            start_date = args.dates[0]
            end_date = args.dates[1]
        for award_type in award_types_idv:
            get_data('IDV', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, last_update, start_date=start_date, end_date=end_date, metrics=metrics_json)
        for award_type in award_types_award:
            get_data('award', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, last_update, start_date=start_date, end_date=end_date, metrics=metrics_json)
        get_delete_data('IDV', now, sess, last_update, start_date, end_date, metrics=metrics_json)
        get_delete_data('award', now, sess, last_update, start_date, end_date, metrics=metrics_json)
        if ((not start_date) and (not end_date)):
            sess.query(FPDSUpdate).update({'update_date': now}, synchronize_session=False)
        sess.commit()
        logger.info('Ending at: %s', str(datetime.datetime.now()))
    elif args.delete:
        del_type = args.delete[0]
        if (del_type == 'award'):
            del_awards = True
            del_idvs = False
        elif (del_type == 'idv'):
            del_awards = False
            del_idvs = True
        elif (del_type == 'both'):
            del_awards = True
            del_idvs = True
        else:
            logger.error('Delete argument must be "idv", "award", or "both"')
            raise ValueError('Delete argument must be "idv", "award", or "both"')
        if del_idvs:
            get_delete_data('IDV', now, sess, now, args.delete[1], args.delete[2], metrics=metrics_json)
        if del_awards:
            get_delete_data('award', now, sess, now, args.delete[1], args.delete[2], metrics=metrics_json)
        sess.commit()
    metrics_json['duration'] = str((datetime.datetime.now() - now))
    with open('pull_fpds_data_metrics.json', 'w+') as metrics_file:
        json.dump(metrics_json, metrics_file)
def main():
    sess = GlobalDB.db().session
    now = datetime.datetime.now()
    parser = argparse.ArgumentParser(description='Pull data from the FPDS Atom Feed.')
    parser.add_argument('-a', '--all', help='Clear out the database and get historical data', action='store_true')
    parser.add_argument('-l', '--latest', help='Get by last_mod_date stored in DB', action='store_true')
    parser.add_argument('-d', '--delivery', help='Used in conjunction with -a to indicate delivery order feed', action='store_true')
    parser.add_argument('-o', '--other', help='Used in conjunction with -a to indicate all feeds other than delivery order', action='store_true')
    parser.add_argument('-da', '--dates', help='Used in conjunction with -l to specify dates to gather updates from.Should have 2 arguments, first and last day, formatted YYYY/mm/dd', nargs=2, type=str)
    parser.add_argument('-del', '--delete', help='Used to only run the delete feed. First argument must be "both", "idv", or "award". The second and third arguments must be the first and last day to run the feeds for, formatted YYYY/mm/dd', nargs=3, type=str)
    args = parser.parse_args()
    award_types_award = ['BPA Call', 'Definitive Contract', 'Purchase Order', 'Delivery Order']
    award_types_idv = ['GWAC', 'BOA', 'BPA', 'FSS', 'IDC']
    metrics_json = {'script_name': 'pull_fpds_data.py', 'start_time': str(now), 'records_received': 0, 'deletes_received': 0, 'records_deleted': 0, 'deleted_award_records_file': , 'deleted_idv_records_file': , 'start_date': , 'end_date': }
    (sub_tier_list, country_list, state_code_list, county_by_name, county_by_code, exec_comp_dict) = create_lookups(sess)
    if args.all:
        if (((not args.delivery) and (not args.other)) or (args.delivery and args.other)):
            logger.error('When using the -a flag, please include either -d or -o (but not both) to indicate which feeds to read in')
            raise ValueError('When using the -a flag, please include either -d or -o (but not both) to indicate which feeds to read in')
        logger.info('Starting at: %s', str(datetime.datetime.now()))
        if args.other:
            for award_type in award_types_idv:
                get_data('IDV', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, metrics=metrics_json)
            for award_type in award_types_award:
                if (award_type != 'Delivery Order'):
                    get_data('award', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, metrics=metrics_json)
        elif args.delivery:
            get_data('award', 'Delivery Order', now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, metrics=metrics_json)
        last_update = sess.query(FPDSUpdate).one_or_none()
        if last_update:
            sess.query(FPDSUpdate).update({'update_date': now}, synchronize_session=False)
        else:
            sess.add(FPDSUpdate(update_date=now))
        sess.commit()
        logger.info('Ending at: %s', str(datetime.datetime.now()))
    elif args.latest:
        logger.info('Starting at: %s', str(datetime.datetime.now()))
        last_update_obj = sess.query(FPDSUpdate).one_or_none()
        if (not last_update_obj):
            logger.error('No last_update date present, please run the script with the -a flag to generate an initial dataset')
            raise ValueError('No last_update date present, please run the script with the -a flag to generate an initial dataset')
        last_update = last_update_obj.update_date
        start_date = None
        end_date = None
        if args.dates:
            start_date = args.dates[0]
            end_date = args.dates[1]
        for award_type in award_types_idv:
            get_data('IDV', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, last_update, start_date=start_date, end_date=end_date, metrics=metrics_json)
        for award_type in award_types_award:
            get_data('award', award_type, now, sess, sub_tier_list, county_by_name, county_by_code, state_code_list, country_list, exec_comp_dict, last_update, start_date=start_date, end_date=end_date, metrics=metrics_json)
        get_delete_data('IDV', now, sess, last_update, start_date, end_date, metrics=metrics_json)
        get_delete_data('award', now, sess, last_update, start_date, end_date, metrics=metrics_json)
        if ((not start_date) and (not end_date)):
            sess.query(FPDSUpdate).update({'update_date': now}, synchronize_session=False)
        sess.commit()
        logger.info('Ending at: %s', str(datetime.datetime.now()))
    elif args.delete:
        del_type = args.delete[0]
        if (del_type == 'award'):
            del_awards = True
            del_idvs = False
        elif (del_type == 'idv'):
            del_awards = False
            del_idvs = True
        elif (del_type == 'both'):
            del_awards = True
            del_idvs = True
        else:
            logger.error('Delete argument must be "idv", "award", or "both"')
            raise ValueError('Delete argument must be "idv", "award", or "both"')
        if del_idvs:
            get_delete_data('IDV', now, sess, now, args.delete[1], args.delete[2], metrics=metrics_json)
        if del_awards:
            get_delete_data('award', now, sess, now, args.delete[1], args.delete[2], metrics=metrics_json)
        sess.commit()
    metrics_json['duration'] = str((datetime.datetime.now() - now))
    with open('pull_fpds_data_metrics.json', 'w+') as metrics_file:
        json.dump(metrics_json, metrics_file)