def do_open_locations(self): """Open the locations file and extract city locations.""" # Get the filename. newfilename = tkfile.askopenfilename() # If they press "cancel", skip the rest. if not newfilename: return # Set the filenames; setting location should clear connections. self.location_file_name = newfilename self.connection_file_name = None # Try to open the file. try: locationsfile = open(self.location_file_name) # Complain if the file can't be opened. except IOError: print(repr(self.location_file_name)) tkmsg.showerror("Error", "Error loading locations file.") return # Read the cities from the file. self.cities = parse_locations_file(locationsfile) # Clear the old cities from the canvas. self.canvas.delete(tk.ALL) # Draw all of the cities. for city in self.cities: city.draw(self.canvas) # Show some log information. self.log_message("Loaded " + str(len(self.cities)) + " cities.") # Set states of menu items. self.filemenu.entryconfig(1, state=tk.NORMAL) self.searchmenu.entryconfig(0, state=tk.DISABLED) self.searchmenu.entryconfig(1, state=tk.DISABLED) # Add cities to the start and end city option menus citynames = sorted([x.name for x in self.cities]) # Remove 'dummy' menu items self.startoptionmenu['menu'].delete(0, 'end') self.endoptionmenu['menu'].delete(0, 'end') # Add each city as a menu item for city in citynames: self.startoptionmenu['menu'].add_command(label=city, \ command=lambda item=city: self.do_set_start_city(item)) self.endoptionmenu['menu'].add_command(label=city, \ command=lambda item=city: self.do_set_end_city(item)) # Set default start and end cities self.startcity.set(citynames[0]) self.endcity.set(citynames[-1]) self.do_set_endpoint_cities()
print("Usage: python {} <locations> <connections> <origin> <destination> [<avoid>]".format(sys.argv[0])) exit(1) try: locationsfile = open(sys.argv[1]) except IOError: print("Could not open file", sys.argv[1]) exit(1) try: connectionsfile = open(sys.argv[2]) except IOError: print("Could not open file", sys.argv[2]) exit(1) locations = parse_connections_file(connectionsfile, parse_locations_file(locationsfile)) try: startcity = [x for x in locations if x.name == sys.argv[3]][0] except IndexError: print("Could not find origin city:", sys.argv[3]) exit(1) try: destcity = [x for x in locations if x.name == sys.argv[4]][0] except IndexError: print("Could not find destination city:", sys.argv[4]) exit(1) avoidcities = [x for x in locations if x.name in sys.argv[5:]]