Example #1
0
    def test_layover_split(self):
        zero_layover = Parser()
        layover_time = '0h00'
        zero_result_hours = zero_layover.layover_split(layover_time)[0]
        zero_result_minutes = zero_layover.layover_split(layover_time)[1]

        layover = Parser()
        layover_time = '23h50'
        result_hours = layover.layover_split(layover_time)[0]
        result_minutes = layover.layover_split(layover_time)[1]

        assert zero_result_hours == 0
        assert zero_result_minutes == 0

        assert result_hours == 23
        assert result_minutes == 50
 def test_trip_parser_dictionary(self):
     line = 'TRIP #174  174 [1CPT,1FO] CHC  Type: 4: _____6_ effective JUN 01-JUN 01 no exceptions.'
     trip = Parser.trip_parser(line)
     assert trip == {'trip_number': 174, 'base': 'CHC', 'number_of_days': 4}
 def test_in_sector_false(self):
     sector = Parser()
     result = sector.in_sector(
         '                                                     00:55  3h30  0h30  5h00'
         '          ')
     assert result is False
 def test_in_sector_true(self):
     sector = Parser()
     result = sector.in_sector(
         '                      We 2    00059    BNE 10:15 VLI 14:00  2h45  0h50'
         '                         _73C       [1,1,0,1,3,0,0]')
     assert result is True
 def test_new_day_false(self):
     day = Parser()
     result = day.new_day(
         '                      We 2    00163    SYD 10:40 ZQN 15:40  3h00  0h45'
         '                         _73C       [1,1,0,1,3,0,0]')
     assert result is False
 def test_new_day_true(self):
     day = Parser()
     result = day.new_day('M04 -- -- -- -- -- --      Sign_on         17:00'
                          '                  1h00                   0h00')
     assert result is True
Example #7
0
    def test_sector_parser(self):
        sector_one = Parser()
        result_sector_one = sector_one.sector_parser(
            '                      Tu 2    00094    TBU 07:00 SYD 09:45'
            '  5h45  2h15                         _73C       '
            '[1,1,0,1,3,0,0]')
        sector_two = Parser()
        result_sector_two = sector_two.sector_parser(
            '                      We 3 VA_00331    MEL 14:00 BNE 16:10'
            '  2h10  2h00                         _73H                ')

        sector_three = Parser()
        result_sector_three = sector_three.sector_parser(
            'U-- -- -- -- -- -- -- Mo 1    00156'
            '    AKL 18:00 BNE 19:50  3h50'
            '                               _73C'
            '       [1,1,0,1,3,0,0]')

        sector_four = Parser()
        result_sector_four = sector_four.sector_parser(
            'O-- -- -- -- -- -- -- Tu 1 NZ_00296'
            '    AKL 08:45 APW 13:30  3h45'
            '                               _772                ')

        assert result_sector_one == {
            'flight_number': '00094',
            'departure_port': 'TBU',
            'departure_time': '07:00',
            'destination_port': 'SYD',
            'arrival_time': '09:45',
            'scheduled_time': '5h45',
            'turn_around_time': datetime.time(2, 15),
            'is_position_flight': False
        }

        assert result_sector_two == {
            'flight_number': '00331',
            'departure_port': 'MEL',
            'departure_time': '14:00',
            'destination_port': 'BNE',
            'arrival_time': '16:10',
            'scheduled_time': '2h10',
            'turn_around_time': datetime.time(2, 0),
            'is_position_flight': True
        }

        assert result_sector_three == {
            'flight_number': '00156',
            'departure_port': 'AKL',
            'departure_time': '18:00',
            'destination_port': 'BNE',
            'arrival_time': '19:50',
            'scheduled_time': '3h50',
            'turn_around_time': None,
            'is_position_flight': False
        }

        assert result_sector_four == {
            'flight_number': '00296',
            'departure_port': 'AKL',
            'departure_time': '08:45',
            'destination_port': 'APW',
            'arrival_time': '13:30',
            'scheduled_time': '3h45',
            'turn_around_time': None,
            'is_position_flight': True
        }
Example #8
0
 def test_end_day_false(self):
     day = Parser()
     result = day.end_day(
         'P16 17 18 19 20 21 --    1    00113    OOL 12:15 AKL 17:15  3h00'
         '                               _73C       [1,1,0,1,3,0,0]')
     assert result is False
Example #9
0
 def test_end_day_true(self):
     day = Parser()
     result = day.end_day(
         ' -- -- -- -- -- -- --                                15:25  7h20  0h30  9h55          '
     )
     assert result is True
Example #10
0
    def test_ordered_day(self):
        day_one = Parser()
        result = day_one.order_day({
            "flight_duty_period_minutes":
            0,
            "day_number":
            2,
            "sign_on":
            "05:15",
            "lay_over":
            "18h50",
            "day_sectors": [{
                "flight_number": "00094",
                "departure_port": "TBU",
                "departure_time": "06:15",
                "destination_port": "SYD",
                "arrival_time": "09:45",
                "scheduled_time": "5h30",
                "turn_around_time": None,
                "is_position_flight": False
            }],
            "lay_over_minutes":
            50,
            "sign_off":
            "10:15",
            "flight_duty_period_hours":
            7,
            "flight_duty_period":
            "7h00",
            "lay_over_hours":
            18
        })

        assert result == {
            "day_number":
            2,
            "sign_on":
            "05:15",
            "day_sectors": [{
                "flight_number": "00094",
                "departure_port": "TBU",
                "departure_time": "06:15",
                "destination_port": "SYD",
                "arrival_time": "09:45",
                "scheduled_time": "5h30",
                "turn_around_time": None,
                "is_position_flight": False
            }],
            "sign_off":
            "10:15",
            "flight_duty_period":
            "7h00",
            "flight_duty_period_hours":
            7,
            "flight_duty_period_minutes":
            0,
            "lay_over":
            "18h50",
            "lay_over_hours":
            18,
            "lay_over_minutes":
            50
        }
Example #11
0
def main():
    """ See 'main.py' docstring description. """
    parser = argparse.ArgumentParser(
        description='Process input file and save to output file.')

    parser.add_argument('-i',
                        '--input',
                        help='Trip report file (txt file) to process.',
                        action='store')

    parser.add_argument('-o',
                        '--output',
                        help='Output file (json).',
                        action='store')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()

    if not args.input:
        print('Please specify the trip report input text file.')
        sys.exit(1)

    if args.output:
        output_file = args.output
    else:
        output_file = OUTPUT_DEFAULT
        print(f'No output file specified. Using the default: {OUTPUT_DEFAULT}')

    trip_list = [
    ]  # the list of trips(dictionaries) to output to the data file
    trip_started = False
    day_started = False

    with open(args.input, 'r') as fh:

        while True:

            line = fh.readline()

            if not line:
                break

            if line.isspace():
                continue

            if '________' in line:
                continue

            if line.startswith(
                    'TRIP'
            ):  # detect start of a trip (anywhere from 1-5 days long)
                # print('Start trip' + line)

                trip = Parser.trip_parser(line)  # create a trip(dictionary)
                trip['days'] = [
                ]  # adds a day list (a list of sectors(dictionaries)) to the trip list

                trip_started = True  # sets trip started to true
                continue

            # if new day has not started, create a new day
            if trip_started and not day_started and Parser.new_day(line):
                day = {'sign_on': line[43:48], 'day_sectors': []}
                day_started = True  # sets day started to true

            if trip_started and day_started:
                if not Parser.end_day(line):
                    # print('During day: ' + line)
                    day['day_number'] = line[24:26].strip(
                    )  # assign a day number to that day

                    if Parser.in_sector(line):  # check if a sector has started
                        day['day_sectors'].append(
                            Parser.sector_parser(line))  # append sector to day

                else:
                    day['sign_off'] = line[53:58].strip(
                    )  # get sign off time from line
                    day['flight_duty_period'] = line[71:76].strip(
                    )  # get flight duty period
                    flight_duty_split = day['flight_duty_period'].split(
                        'h')  # split flight duty period on 'h'
                    day['flight_duty_period_hours'] = int(
                        flight_duty_split[0])  # convert to hours
                    day['flight_duty_period_minutes'] = int(
                        flight_duty_split[1])  # convert to minutes

                    day_started = False  # sets day started to false

            if not day_started:
                if 'Sign_off' in line:  # indicated the day is finished and its only a single day trip
                    day['lay_over'] = '0h00'  # hard coded 0h00 layover as this is return flight from home base
                    day['lay_over_hours'] = 0  # hard coded 0 hours
                    day['lay_over_minutes'] = 0  # hard coded 0 minutes

                    # order the day using an OrderedDict, before adding it to the trip dict
                    day_ordered = Parser.order_day(day)
                    trip['days'].append(day_ordered)

                elif '--------------------------------' in line:  # the day is over and now layover
                    lay_over = line[88:93].strip()  # get layover from line
                    day['lay_over'] = lay_over  # add to day dictionary
                    day['lay_over_hours'] = Parser.layover_split(lay_over)[
                        0]  # split and convert to int
                    day['lay_over_minutes'] = Parser.layover_split(lay_over)[
                        1]  # split and convert to int

                    # order the day using an OrderedDict, before adding it to the trip dict
                    day_ordered = Parser.order_day(day)
                    trip['days'].append(day_ordered)

            if not line[28:36].isspace(
            ) and line[27:35] == 'Sign_off':  # detect end of a trip
                trip_started = False  # set trip started to False

                trip_list.append(
                    trip)  # append the trip(dictionary) to the trip list

    with open(
            output_file, 'w'
    ) as fh:  # Convert everything (including datetime object) to string
        fh.write(json.dumps(trip_list, default=str,
                            indent=4))  # write to json file for output

    Display.display_data(
        output_file)  # display analytics data within the terminal

    return output_file