93 lines
3.6 KiB
Python
93 lines
3.6 KiB
Python
"""
|
|
Parse the input file
|
|
"""
|
|
|
|
from classes import ParsingMode, Coordinate, LocationType, Location, DataSet
|
|
from args import Settings
|
|
|
|
|
|
def parse(settings: Settings) -> DataSet:
|
|
"""
|
|
Parse a given file with given format and return a DataSet containing the
|
|
parsed locations, flightschedules and wanted connection
|
|
"""
|
|
locations: dict = {}
|
|
flights: dict = {}
|
|
connection: tuple = ()
|
|
with open(settings.file, "r") as file:
|
|
for line in file.readlines():
|
|
line = line.replace("\n", "") # strip newline
|
|
line = line.replace(" ", "") # strip whitespaces
|
|
|
|
# skip empty lines
|
|
if line == "":
|
|
continue
|
|
|
|
# meta parsing
|
|
match line:
|
|
case "Locations:":
|
|
if settings.debug:
|
|
print("Parsing `Locations`...")
|
|
current_parsing = ParsingMode.LOCATIONS
|
|
continue
|
|
case "FlightSchedule:":
|
|
if settings.debug:
|
|
print("Parsing `FlightSchedule`...")
|
|
current_parsing = ParsingMode.FLIGHTSCHEDULE
|
|
continue
|
|
case "FindBestConnections:":
|
|
if settings.debug:
|
|
print("Parsing `FindBestConnections`...")
|
|
current_parsing = ParsingMode.FINDCONNECTION
|
|
continue
|
|
|
|
match current_parsing:
|
|
case ParsingMode.LOCATIONS:
|
|
if settings.debug:
|
|
print("Parsing location...")
|
|
splitted = line.split(";")
|
|
assert(len(splitted) == 6) # make sure we have a location
|
|
id = splitted[0]
|
|
type = splitted[1]
|
|
match type:
|
|
case "Location":
|
|
type = LocationType.LOCATION
|
|
case "PublicTransportStop":
|
|
type = LocationType.HALTESTELLE
|
|
case "Airport":
|
|
type = LocationType.FLUGHAFEN
|
|
lat = splitted[2]
|
|
long = splitted[3]
|
|
continent = splitted[4]
|
|
name = splitted[5]
|
|
coord = Coordinate(lat, long)
|
|
location = Location(coord, continent, name, type)
|
|
locations.update({id: location})
|
|
case ParsingMode.FLIGHTSCHEDULE:
|
|
if settings.debug:
|
|
print("Parsing flight schedule...")
|
|
splitted = line.split(";")
|
|
assert(len(splitted) >= 2)
|
|
id1 = splitted[0]
|
|
id2 = splitted[1]
|
|
if len(splitted) == 3:
|
|
stops = splitted[2]
|
|
else:
|
|
stops = 0
|
|
if len(splitted) == 4:
|
|
domestic = True
|
|
else:
|
|
domestic = False
|
|
flights.update({id1: {
|
|
"to": id2,
|
|
"stops": stops,
|
|
"domestic": domestic}})
|
|
continue
|
|
case ParsingMode.FINDCONNECTION:
|
|
if settings.debug:
|
|
print("Parsing connection...")
|
|
splitted = line.split(";")
|
|
assert(len(splitted) == 2)
|
|
connection = (splitted[0], splitted[1])
|
|
return DataSet(locations, flights, connection)
|