argparser: add argparser and nice print
This commit is contained in:
23
parser.py
23
parser.py
@@ -3,9 +3,10 @@ Parse the input file
|
||||
"""
|
||||
|
||||
from classes import ParsingMode, Coordinate, LocationType, Location, DataSet
|
||||
from args import Settings
|
||||
|
||||
|
||||
def parse(filename: str) -> DataSet:
|
||||
def parse(settings: Settings) -> DataSet:
|
||||
"""
|
||||
Parse a given file with given format and return a DataSet containing the
|
||||
parsed locations, flightschedules and wanted connection
|
||||
@@ -13,7 +14,7 @@ def parse(filename: str) -> DataSet:
|
||||
locations: dict = {}
|
||||
flights: dict = {}
|
||||
connection: tuple = ()
|
||||
with open(filename, "r") as file:
|
||||
with open(settings.file, "r") as file:
|
||||
for line in file.readlines():
|
||||
line = line.replace("\n", "") # strip newline
|
||||
line = line.replace(" ", "") # strip whitespaces
|
||||
@@ -25,21 +26,25 @@ def parse(filename: str) -> DataSet:
|
||||
# meta parsing
|
||||
match line:
|
||||
case "Locations:":
|
||||
print("Parsing `Locations`...")
|
||||
if settings.debug:
|
||||
print("Parsing `Locations`...")
|
||||
current_parsing = ParsingMode.LOCATIONS
|
||||
continue
|
||||
case "FlightSchedule:":
|
||||
print("Parsing `FlightSchedule`...")
|
||||
if settings.debug:
|
||||
print("Parsing `FlightSchedule`...")
|
||||
current_parsing = ParsingMode.FLIGHTSCHEDULE
|
||||
continue
|
||||
case "FindBestConnections:":
|
||||
print("Parsing `FindBestConnections`...")
|
||||
if settings.debug:
|
||||
print("Parsing `FindBestConnections`...")
|
||||
current_parsing = ParsingMode.FINDCONNECTION
|
||||
continue
|
||||
|
||||
match current_parsing:
|
||||
case ParsingMode.LOCATIONS:
|
||||
print("Parsing location...")
|
||||
if settings.debug:
|
||||
print("Parsing location...")
|
||||
splitted = line.split(";")
|
||||
assert(len(splitted) == 6) # make sure we have a location
|
||||
id = splitted[0]
|
||||
@@ -59,7 +64,8 @@ def parse(filename: str) -> DataSet:
|
||||
location = Location(coord, continent, name, type)
|
||||
locations.update({id: location})
|
||||
case ParsingMode.FLIGHTSCHEDULE:
|
||||
print("Parsing flight schedule...")
|
||||
if settings.debug:
|
||||
print("Parsing flight schedule...")
|
||||
splitted = line.split(";")
|
||||
assert(len(splitted) >= 2)
|
||||
id1 = splitted[0]
|
||||
@@ -78,7 +84,8 @@ def parse(filename: str) -> DataSet:
|
||||
"domestic": domestic}})
|
||||
continue
|
||||
case ParsingMode.FINDCONNECTION:
|
||||
print("Parsing connection...")
|
||||
if settings.debug:
|
||||
print("Parsing connection...")
|
||||
splitted = line.split(";")
|
||||
assert(len(splitted) == 2)
|
||||
connection = (splitted[0], splitted[1])
|
||||
|
||||
Reference in New Issue
Block a user