def main(): # get command line args args = cli.parse_cli_args() # print canteens if args.canteens: print(enum_json_creator.enum_to_api_representation_dict(list(Canteen))) return canteen = Canteen.get_canteen_by_str(args.canteen) # get required parser parser = get_menu_parsing_strategy(canteen) # parse menu menus = parser.parse(canteen) # if date has been explicitly specified, try to parse it menu_date = None if args.date is not None: try: menu_date = util.parse_date(args.date) except ValueError: print(f"Error during parsing date from command line: {args.date}") print(f"Required format: {util.cli_date_format}") return # print menu if menus is None: print("Error. Could not retrieve menu(s)") # optionally translate the dish titles if args.language is not None and args.language.upper() != "DE": translated = util.translate_dishes(menus, args.language) if not translated: print("Error. The translation was not successful") # jsonify argument is set if args.jsonify is not None: weeks = Week.to_weeks(menus) if not os.path.exists(args.jsonify): os.makedirs(args.jsonify) jsonify(weeks, args.jsonify, canteen, args.combine) elif args.openmensa is not None: weeks = Week.to_weeks(menus) if not os.path.exists(args.openmensa): os.makedirs(args.openmensa) openmensa(weeks, args.openmensa) # date argument is set elif args.date is not None: if menu_date not in menus: print(f"There is no menu for '{canteen}' on {menu_date}!") return menu = menus[menu_date] print(menu) # else, print weeks elif menus is not None: weeks = Week.to_weeks(menus) for calendar_week in weeks: print(weeks[calendar_week])
def main(): # get command line args args = cli.parse_cli_args() # print canteens if args.locations: with open("canteens.json", 'r') as canteens: print(json.dumps(json.load(canteens))) return # get location from args location = args.location # get required parser parser = get_menu_parsing_strategy(location) if parser is None: print("The selected location '%s' does not exist." % location) # parse menu menus = parser.parse(location) # if date has been explicitly specified, try to parse it menu_date = None if args.date is not None: try: menu_date = util.parse_date(args.date) except ValueError as e: print("Error during parsing date from command line: %s" % args.date) print("Required format: %s" % util.cli_date_format) return # print menu if menus is None: print("Error. Could not retrieve menu(s)") # jsonify argument is set elif args.jsonify is not None: weeks = Week.to_weeks(menus) if not os.path.exists(args.jsonify): os.makedirs(args.jsonify) jsonify(weeks, args.jsonify, location, args.combine) elif args.openmensa is not None: weeks = Week.to_weeks(menus) if not os.path.exists(args.openmensa): os.makedirs(args.openmensa) openmensa(weeks, args.openmensa) # date argument is set elif args.date is not None: if menu_date not in menus: print("There is no menu for '%s' on %s!" % (location, menu_date)) return menu = menus[menu_date] print(menu) # else, print weeks else: weeks = Week.to_weeks(menus) for calendar_week in weeks: print(weeks[calendar_week])
def test_Should_CreateCorrectDirectoriesAndJSONFiles(self): # parse menu menus = self.studentenwerk_menu_parser.get_menus( self.menu_html_garching, "mensa-garching") # get weeks weeks = Week.to_weeks(menus) # create temp dir for testing with tempfile.TemporaryDirectory() as temp_dir: # store output in the tempdir # location can be an empty string because combination won't get tested (combine_dishes is False) here main.jsonify(weeks, temp_dir, "", False) # check if two directories are created (one for 2016 and 2017) created_dirs = [ name for name in os.listdir(temp_dir) if os.path.isdir(os.path.join(temp_dir, name)) ] created_dirs.sort() self.assertEqual(1, len(created_dirs)) self.assertEqual("2017", created_dirs[0]) # check if the created directories contain the JSON files dir_2017 = "%s/2017" % temp_dir files_in_2017 = [ name for name in os.listdir(dir_2017) if os.path.isfile(os.path.join(dir_2017, name)) ] files_in_2017.sort() self.assertEqual( ["13.json", "14.json", "15.json", "16.json", "17.json"], files_in_2017)
def test_genFile(self): # parse the menu menus = self.mediziner_mensa_parser.get_menus( self.menu_kw_47_2018_txt, self.menu_kw_47_2018_year, self.menu_kw_47_2018_week_number) weeks = Week.to_weeks(menus) main.jsonify(weeks, "/tmp/eat-api_test_output", "mediziner-mensa", True)
def test_Should_ReturnWeeks_When_ConvertingMenuToWeekObjects(self): menus = self.studentenwerk_menu_parser.get_menus( self.menu_html_mensa_garching_old, "mensa-garching") weeks_actual = Week.to_weeks(menus) length_weeks_actual = len(weeks_actual) self.assertEqual(5, length_weeks_actual) for calendar_week in weeks_actual: week = weeks_actual[calendar_week] week_length = len(week.days) # calendar weeks 15 and 16 have one day less, because of a holiday if calendar_week == 15 or calendar_week == 16: self.assertEqual(4, week_length) else: self.assertEqual(5, week_length)
def test_Should_Add_Week_to_Canteen(self): date_mon2 = date(2017, 11, 6) date_tue2 = date(2017, 11, 7) date_wed2 = date(2017, 11, 8) date_thu2 = date(2017, 11, 9) date_fri2 = date(2017, 11, 10) dish_aktion2 = Dish("Pochiertes Lachsfilet mit Dillsoße dazu Minze-Reis", 6.5, set(["Sl", "Mi"])) dish1_mon2 = Dish("Dampfkartoffeln mit Zucchinigemüse", 3.6, set(["Sl"])) dish2_mon2 = Dish("Valess-Schnitzel mit Tomaten-Couscous", 4.3, set(["Sl", "Gl", "Ei", "Mi"])) dish3_mon2 = Dish("Kasslerpfanne mit frischen Champignons und Spätzle", 4.9, set(["Sl", "Mi"])) dish1_tue2 = Dish("Gemüsereispfanne mit geräuchertem Tofu", 3.6, set(["Sl"])) dish2_tue2 = Dish("Schweineschnitzel in Karottenpanade mit Rosmarin- Risoleekartoffeln", 5.3, set(["Sl", "Gl", "Ei"])) dish1_wed2 = Dish("Spaghetti al Pomodoro", 3.6, set(["Sl", "Gl"])) dish2_wed2 = Dish("Krustenbraten vom Schwein mit Kartoffelknödel und Krautsalat", 5.3, set(["Sl", "Gl"])) dish1_thu2 = Dish("Red-Thaicurrysuppe mit Gemüse und Kokosmilch", 2.9, set(["Sl"])) dish2_thu2 = Dish("Senf-Eier mit Salzkartoffeln", 3.8, set(["Sl", "Sf", "Mi"])) dish3_thu2 = Dish("Putengyros mit Zaziki und Tomatenreis", 5.3, set(["Sl", "Mi"])) dish1_fri2 = Dish("Spiralnudeln mit Ratatouillegemüse", 3.6, set(["Gl"])) dish2_fri2 = Dish("Milchreis mit warmen Sauerkirschen", 3, set(["Mi"])) dish3_fri2 = Dish("Lasagne aus Seelachs und Blattspinat", 5.3, set(["Sl", "Gl", "Mi"])) menu_mon2 = Menu(date_mon2, [dish_aktion2, dish1_mon2, dish2_mon2, dish3_mon2]) menu_tue2 = Menu(date_tue2, [dish_aktion2, dish1_tue2, dish2_tue2]) menu_wed2 = Menu(date_wed2, [dish_aktion2, dish1_wed2, dish2_wed2]) menu_thu2 = Menu(date_thu2, [dish_aktion2, dish1_thu2, dish2_thu2, dish3_thu2]) menu_fri2 = Menu(date_fri2, [dish_aktion2, dish1_fri2, dish2_fri2, dish3_fri2]) week = Week(45, 2017, [menu_mon2, menu_tue2, menu_wed2, menu_thu2, menu_fri2]) week = {} week[date_mon2] = menu_mon2 week[date_tue2] = menu_tue2 week[date_wed2] = menu_wed2 week[date_thu2] = menu_thu2 week[date_fri2] = menu_fri2 weeks = Week.to_weeks(week) canteen = openmensa.weeksToCanteenFeed(weeks) self.assertEqual(canteen.hasMealsFor(date_mon2), True) self.assertEqual(canteen.hasMealsFor(date_tue2), True) self.assertEqual(canteen.hasMealsFor(date_wed2), True) self.assertEqual(canteen.hasMealsFor(date_thu2), True) self.assertEqual(canteen.hasMealsFor(date_fri2), True) canteen_wed2 = canteen._days[date_wed2]['Speiseplan'] self.assertEqual(canteen_wed2[0], ("Pochiertes Lachsfilet mit Dillsoße dazu Minze-Reis", [], {'other': 650})) self.assertEqual(canteen_wed2[1], ("Spaghetti al Pomodoro", [], {'other': 360})) self.assertEqual(canteen_wed2[2], ("Krustenbraten vom Schwein mit Kartoffelknödel und Krautsalat", [], {'other': 530}))
def test_Studentenwerk_Mensa_Garching_New(self): # parse the menu menus = self.studentenwerk_menu_parser.get_menus( self.menu_html_mensa_garching_new, "mensa-garching") weeks = Week.to_weeks(menus) # create temp dir for testing with tempfile.TemporaryDirectory() as temp_dir: # store output in the tempdir main.jsonify(weeks, temp_dir, "mensa-garching", True) # open the generated file with open(os.path.join(temp_dir, "combined", "combined.json"), "r") as generated: # open the reference file with open( "src/test/assets/studentenwerk/out/speiseplan_mensa_garching_new.json", "r") as reference: self.assertEqual(json.load(generated), json.load(reference))
def test_Ipp_Bistro_Kw_22_2019_closed_thursday(self): # parse the menu menus = self.ipp_parser.get_menus(self.menu_kw_22_2019_txt, self.menu_kw_22_2019_year, self.menu_kw_22_2019_week_number) weeks = Week.to_weeks(menus) # create temp dir for testing with tempfile.TemporaryDirectory() as temp_dir: # store output in the tempdir main.jsonify(weeks, temp_dir, "ipp-bistro", True) # open the generated file with open(os.path.join(temp_dir, "combined", "combined.json"), "r") as generated: # open the reference file with open("src/test/assets/ipp/out/menu_kw_22_2019.json", "r") as reference: self.assertEqual(json.load(generated), json.load(reference))
def test_Mediziner_Mensa_Kw_47_2018(self): # parse the menu menus = self.mediziner_mensa_parser.get_menus( self.menu_kw_47_2018_txt, self.menu_kw_47_2018_year, self.menu_kw_47_2018_week_number) weeks = Week.to_weeks(menus) # create temp dir for testing with tempfile.TemporaryDirectory() as temp_dir: # store output in the tempdir main.jsonify(weeks, temp_dir, "mediziner-mensa", True) # open the generated file with open(os.path.join(temp_dir, "combined", "combined.json"), "r") as generated: # open the reference file with open( "src/test/assets/mediziner-mensa/out/menu_kw_47_2018.json", "r") as reference: self.assertEqual(json.load(generated), json.load(reference))
def test_Should_ConvertWeekToJSON(self): with open( 'src/test/assets/studentenwerk/out/speiseplan_mensa_garching_kw2017-13.json' ) as data_file: week_2017_13 = json.load(data_file) with open( 'src/test/assets/studentenwerk/out/speiseplan_mensa_garching_kw2017-14.json' ) as data_file: week_2017_14 = json.load(data_file) with open( 'src/test/assets/studentenwerk/out/speiseplan_mensa_garching_kw2017-15.json' ) as data_file: week_2017_15 = json.load(data_file) with open( 'src/test/assets/studentenwerk/out/speiseplan_mensa_garching_kw2017-16.json' ) as data_file: week_2017_16 = json.load(data_file) with open( 'src/test/assets/studentenwerk/out/speiseplan_mensa_garching_kw2017-17.json' ) as data_file: week_2017_17 = json.load(data_file) menus = self.studentenwerk_menu_parser.get_menus( self.menu_html_mensa_garching_old, "mensa-garching") weeks = Week.to_weeks(menus) week_2017_13_actual = json.loads(weeks[13].to_json()) week_2017_14_actual = json.loads(weeks[14].to_json()) week_2017_15_actual = json.loads(weeks[15].to_json()) week_2017_16_actual = json.loads(weeks[16].to_json()) week_2017_17_actual = json.loads(weeks[17].to_json()) self.assertEqual(self.order_json_objects(week_2017_13_actual), self.order_json_objects(week_2017_13)) self.assertEqual(self.order_json_objects(week_2017_14_actual), self.order_json_objects(week_2017_14)) self.assertEqual(self.order_json_objects(week_2017_15_actual), self.order_json_objects(week_2017_15)) self.assertEqual(self.order_json_objects(week_2017_16_actual), self.order_json_objects(week_2017_16)) self.assertEqual(self.order_json_objects(week_2017_17_actual), self.order_json_objects(week_2017_17))
def test_Should_ConvertWeekToJSON(self): with open('src/test/assets/speiseplan_garching_kw2017-13.json' ) as data_file: week_2017_13 = json.load(data_file) with open('src/test/assets/speiseplan_garching_kw2017-14.json' ) as data_file: week_2017_14 = json.load(data_file) with open('src/test/assets/speiseplan_garching_kw2017-15.json' ) as data_file: week_2017_15 = json.load(data_file) with open('src/test/assets/speiseplan_garching_kw2017-16.json' ) as data_file: week_2017_16 = json.load(data_file) with open('src/test/assets/speiseplan_garching_kw2017-17.json' ) as data_file: week_2017_17 = json.load(data_file) menus = self.studentenwerk_menu_parser.get_menus( self.menu_html_garching) weeks = Week.to_weeks(menus) week_2017_13_actual = json.loads(weeks[13].to_json()) week_2017_14_actual = json.loads(weeks[14].to_json()) week_2017_15_actual = json.loads(weeks[15].to_json()) week_2017_16_actual = json.loads(weeks[16].to_json()) week_2017_17_actual = json.loads(weeks[17].to_json()) self.assertEqual(sorted(week_2017_13_actual.items()), sorted(week_2017_13.items())) self.assertEqual(sorted(week_2017_14_actual.items()), sorted(week_2017_14.items())) self.assertEqual(sorted(week_2017_15_actual.items()), sorted(week_2017_15.items())) self.assertEqual(sorted(week_2017_16_actual.items()), sorted(week_2017_16.items())) self.assertEqual(sorted(week_2017_17_actual.items()), sorted(week_2017_17.items()))
def test_genFile(self): # parse the menu menus = self.studentenwerk_menu_parser.get_menus( self.menu_html_mensa_garching_old, "mensa-garching") weeks = Week.to_weeks(menus) main.jsonify(weeks, "mensa-garching.json", "mensa-garching", True)