CSV Reader Class

The CsvReader enables import of CSV files containing flight information into the Startkladde database.

Expected Format

The csv format expected as input to the CsvReader.__call__ is similar to the format produced by the Startkladde webinterface for csv export. Field delimiter, encoding and the date- and time format can be specified via respective arguments passed to CsvReader.__call__.

Mandatory Columns

The first line of the file must contain a header containing the columns. The given column names are first translated to internal column names using the dictionary CsvReader.columnMap (allows to support several languages). At least the aliases for the columns defined in the set CsvReader.mandatoryColumns must be defined. Optionally the columns defined in the set CsvReader.optionalColumns may be present.

Date and Time Format

Fields requiring date and time information must be formatted according to the format string passed to CsvReader.__call__

Special Values

The columns flight type and mode require special values. Accepted are the strings defined in the dictionaries CsvReader.flightTypeMap and CsvReader.flightModeMap.

Interface

class pysk.db.CsvReader(logStream=<open file '<stderr>', mode 'w'>, verbose=1, debug=False)

Csv Reader for Startkladde flights

Parameters:
  • logStream – Stream used for log messages
  • verbose (int) – Verbose mode setting.
  • debug (bool) – Enable debug mode.
__call__(path, delimiter=';', encoding='utf8', dateformat='%Y-%m-%d', timeformat='%H:%M', mergeTowflights=True)

Read input file and return a list of flights

Parameters:
  • path (str) – Path to input file
  • delimiter (str) – Field delimiter. Defaults to ‘;’
  • encoding (str) – Encoding of input file (e.g. ‘utf8’ or ‘windows-1252’). Defaults to ‘utf8’
  • dateformat (str) – Date format used in csv (notation as in strptime). Defaults to ‘%Y-%m-%d’
  • timeformat (str) – Time format used in csv (notation as in strptime). Defaults to ‘%H:%M’
  • mergeTowflights (bool) – If towflights are listed in a separate line, merge respective flights with towflights. Defaults to True
Returns:

List of Record instances with one record per line in input file

analyseHeader(header)

Analyse header string of csv file to identify needed columns

Parameters:header – Header object passed by csv reader.
error(message)

Print error message

Parameters:message (str) – Error message to print
get(field)

Get field from current record by name

Raises a KeyError if mandatory field is not found

Parameters:field (str) – Name of field to return
Returns:value or None if no such field exists
getCopilot()

Extract copilot from current record

Returns:db.model.Pilot instance or None
getDepartureTime()

Extract departure time from record

Returns:datetime.datetime instance
getFlight()

Extract flight information from current record

Returns:pysk.db.model.Flight instance
getFlightType()

Get flight type

Returns:Flight type as defined in flightTypeMap
getLandingTime()

Extract landing time from current record

Returns:datetime.datetime instance
getLaunchMethod()

Extract launch method from current record

Returns:db.model.LaunchMethod instance or None
getMode()

Get flight mode of current record

Returns:Flight mode as defined in flightModeMap
getPilot()

Extract pilot from current record

Returns:db.model.Pilot instance
getPlane()

Extract plane information from current record

Returns:db.model.Airplane instance
getTowflightLandingTime()

Extract towflight landing time from current record

Returns:datetime.datetime instance
getTowflightMode()

Get towflight mode of current record

Returns:Flight mode as defined in flightModeMap
getTowpilot()

Extract towpilot from current record

Returns:db.model.Pilot instance or None
getTowplane()

Extract towplane information from current record

Returns:db.model.Airplane instance or None if no towplane registration exists
importRecord(record)

Convert csv record to database Record

Parameters:record – csv record as passed by python csv reader
Returns:pysk.db.Record instance
log(message, verbose=0)

Print log message

Parameters:
  • message (str) – Log message to print
  • verbose (int) – Verbose mode from which on the message is printed
warn(message)

Print warning message

Parameters:message (str) – Warning to print (regardless of verbose mode setting)
columnMap = {'landung schleppflugzeug': 'towflight_landing_time', 'zielort schleppflugzeug': 'towflight_landing_location', 'flugtyp': 'flight_type', 'pilot nachname': 'pilot_last_name', 'zielort': 'landing_location', 'dbid': 'flight_id', 'modus schleppflugzeug': 'towflight_mode', 'datum': 'date', 'startort': 'departure_location', 'begleiter vorname': 'copilot_first_name', 'anzahl landungen': 'num_landings', 'startart': 'launch_method', 'modus': 'flight_mode', 'abrechnungshinweis': 'accounting_notes', 'kennzeichen': 'plane_registration', 'startzeit': 'departure_time', 'begleiter nachname': 'copilot_last_name', 'pilot vorname': 'pilot_first_name', 'kennzeichen schleppflugzeug': 'towplane_registration', 'bemerkungen': 'comments', 'landezeit': 'landing_time'}

Column map from csv to internal names

flightModeMap = {'geht': 'outbound', 'kommt': 'inbound', 'lokal': 'local'}

Map for flight types

flightTypeMap = {'schulung (1)': 'training_1', 'gastflug (p)': 'guest_private', 'schulung (2)': 'training_2', 'schlepp': 'towflight', 'gastflug': 'guest_external', 'gastflug (e)': 'guest_external', 'normalflug': 'normal'}

Map for flight types

mandatoryColumns = set(['copilot_first_name', 'launch_method', 'landing_time', 'pilot_first_name', 'plane_registration', 'comments', 'copilot_last_name', 'departure_location', 'flight_mode', 'date', 'landing_location', 'flight_type', 'num_landings', 'accounting_notes', 'departure_time'])

Mandatory columns in input files

optionalColumns = set(['towflight_mode', 'towflight_landing_location', 'towflight_pilot', 'towflight_landing_time', 'towplane_registration'])

Optional columns in input files

towflightKeys = ['schleppflug', 'towflight']

Map for tow flight comments