Example #1
0
def parse_athlete(tokens):
    """
    Parses an "I" line record from a Hytek meet entry file that contains
    athlete information, and adds the athlete to our database.

    SIDE EFFECTS: Adds athlete to the Database. Also adds the athlete's school
    to the database if it isn't already in it.

    I LINE FORMAT:
    <token #> <data> <numchars>  <notes>
    0   I           1   Information Record
    1   Last Name   20  (Required)
    2   First Name  20  (Required)
    3   Initial     1
    4   Gender      1   M = Male, F = Female (Required)
    5   Birth Date  10  MM/DD/YYYY (Optional)
    6   Team Code   4   4 character max; use UNA if unknown (Required)
    7   Team Name   30  Use Unattached if unknown (Required)
    8   Age         3   Optional if birth date provided
    9  School Year 2   (Optional according to HyTek, but TMS requires it)
    17  Home Phone  20  (Optional)

    NOTE: We REQUIRE School Year, even though valid HyTek files might not.
    """

    tokens = tokens[:]  # make a copy so we don't mutate the original list
    if len(tokens) < 10:
        error(f"I-Line requires at least 10 fields. <{tokens}>")

    (_junk1, last_name, first_name, middle, gender, _junk2, team_code,
     team_name, _junk3, grade) = tokens[0:10]

    athlete = Athlete.add_athlete_to_db(first_name, middle, last_name, gender,
                                        grade, team_code, team_name)
    if athlete is None:
        # athlete might be null because there were issues with the file's
        # record (such as no matching division. if that's the case, just move
        # on to the next record
        # TODO - message user about problem with adding user
        return

    # If present in this I-record, add the athlete's phone (for SMS) to db
    # This will override any previous phone numbers for this athlete
    if len(tokens) > 17:
        if tokens[17] != "":
            athlete.phone = tokens[17]
        db.session.commit()
Example #2
0
def parse_entry(tokens, meet):
    """
    Parses a "D-Line" in a Hytek entry file that corresponds to an athlete's
    entry into a particular event within a meet.

    D-Line format
    <Token#> <Data> <MaxChar>  <Description>
    0   D           1   Individual Entry Record
    1   Last Name   20  (Required)
    2   First Name  20  (Required)
    3   Initial     1   (Optional)
    4   Gender      1   M = Male, F = Female (Required)
    5   Birth Date  10  MM/DD/YYYY (Optional)
    6   Team Code   4   4 characters max; use UNA if unknown (Required)
    7   Team Name   30  Use Unattached if unknown (Required)
    8   Age         3   Age is optional if birth date provided
    9  School Year  2   (Optional for HyTek, but not for TMS)
    10  Event Code  10  Examples: 100, 5000S, 10000W, SP, HJ, DEC
    11  Entry Mark  11  Time: hh:mm:ss.tt (1:23.44.55, 1:19.14, 58.83, 13.4h)
                        Field Metric: 12.33, 1233;
                        Field English: 12-10.25", 12', 121025, 12' 10
                        Combined-event: 3020 (points)
    12  Mark measure 1  M for Metric, E for English (Required if Entry Mark
                        provided)
    """

    if len(tokens) < 11:
        error(f"HyTek D-Line requires at least 11 fields. <{tokens}>")

    (_junk1, last_name, first_name, middle, gender, _junk2, team_code,
     team_name, _junk3, grade, ht_event_code) = tokens[0:11]

    athlete = Athlete.add_athlete_to_db(first_name, middle, last_name, gender,
                                        grade, team_code, team_name)

    # If the athlete's record in file was bad, add_athlete_to_db returns None.
    if athlete is None:
        warning(f"Skipping athlete {first_name} {last_name}")
        return

    # translate the HyTek event names into TMS event codes
    event_code = ht_event_translator.get(ht_event_code, ht_event_code)

    q = MeetDivisionEvent.query
    try:
        mde = q.filter_by(meet_id=meet.id,
                          event_code=event_code,
                          div_id=athlete.division.id).one()
    except NoResultFound:
        raise TmsError("MDE doesn't exist: meet #{}, event {}, div {}".format(
            meet.id, event_code, athlete.division.code))

    entry = Entry(athlete=athlete, mde=mde)
    # we need to commit here, or else we can't see the event in the below call
    # of entry.set_mark method.
    db.session.add(entry)
    db.session.commit()
    info(f"Added entry: {entry}")

    # If the athlete's entry includes a seed mark for this event, set it
    if len(tokens[11:13]) == 2:
        entry.set_mark(mark_string=tokens[11], mark_measure_type=tokens[12])

    # I don't understand why, but at this point the entry thinks its "event"
    # attribute is "None", even after I setup the relationship with the mde,
    # which should also get me the event. I believe this shoudl be possible
    # without adding the entry to the session and commiting.
    db.session.commit()
    info("Set entry's mark. Entry: {}. Mark:{}".format(entry.event.code,
                                                       entry.mark_to_string()))