initial commit
This commit is contained in:
78
parser.py
Normal file
78
parser.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""
|
||||
Parse the input file
|
||||
"""
|
||||
|
||||
from classes import ParsingMode, Coordinate, LocationType, Location, DataSet
|
||||
|
||||
|
||||
def parse(filename: str) -> 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(filename, "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:":
|
||||
print("Parsing `Locations`...")
|
||||
current_parsing = ParsingMode.LOCATIONS
|
||||
continue
|
||||
case "FlightSchedule:":
|
||||
print("Parsing `FlightSchedule`...")
|
||||
current_parsing = ParsingMode.FLIGHTSCHEDULE
|
||||
continue
|
||||
case "FindBestConnections:":
|
||||
print("Parsing `FindBestConnections`...")
|
||||
current_parsing = ParsingMode.FINDCONNECTION
|
||||
continue
|
||||
|
||||
match current_parsing:
|
||||
case ParsingMode.LOCATIONS:
|
||||
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:
|
||||
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
|
||||
flights.update({id1: {"to": id2, "stops": stops}})
|
||||
continue
|
||||
case ParsingMode.FINDCONNECTION:
|
||||
print("Parsing connection...")
|
||||
splitted = line.split(";")
|
||||
assert(len(splitted) == 2)
|
||||
connection = (splitted[0], splitted[1])
|
||||
return DataSet(locations, flights, connection)
|
||||
Reference in New Issue
Block a user