def loadRecipe(self, recipeTitle): recipePath = os.path.join(self.recipeDataBasePath, recipeTitle + ".json") try: f = open(recipePath, "r") data = json.load(f) f.close() r = Recipe.fromDictionary(data) return r except IOError: print "no such file"
def loadRecipe(self, recipeTitle): recipePath = os.path.join(self.dataBasePath, recipeTitle + ".json") print "recipe path==--=", recipePath try: f = open(recipePath, "r") data = json.load(f) f.close() print print print data print print r = Recipe.fromDictionary(data) print "HERE IS THE RECIPE" theRecipeString = r.writeToReadableString() print theRecipeString return theRecipeString except IOError: print "no such file"
def handleLeftMouseClick(self, xy): for button in self.buttonGroup.sprites(): if button.getRect().collidepoint(xy): ## stupid organization leads to hacky solutions... # in groceryList viewer, the writeToDesktop button must be stored in the main # class. So thats whats going on here, we call writeToDesktop # in the scrollable window and grab the grocery list there and send that to # dbm to write it to desktop if button.getIdentity() == "writeToDesktop": theString = self.windowGroup.sprites()[0].writeToDesktop() #print #print #print theString self.dbm.writeStringToDesktop(self.windowGroup.sprites()[0].writeToDesktop(), "Grocery List") self.state = "menu" self.stateChanged = True #print "got here!" return ## we want to store the state of the stuff people are looking at # whether that is a grocery list they have increased or recipes they have added to cart # we call the "grabState()" method of the scrollableWindow to get the information #### keep in mind, this is the previous state # so if self.state == "browseAll" it means we are coming from browseAll state # to the menu if self.state == "browseAll": ## this one is tricky. We need to store a list of toggled rows, and then # if the user goes back to the viewAllBrowser we need to call "updateState()" # with this list, to ensure those buttons are toggled to reflect that they are # still in the groceryList # keep in mind this is a list of the names of the recipes that are already added # so we will need to search for them in "updateState()" allWindows = self.windowGroup.sprites() if allWindows: self.toggledRecipeNames = allWindows[0].grabState() elif self.state == "viewGroceryList": ## this one is simple, we just grabState() and store the returns in self.groceryList # all the changes the user has made will be in the new list # and new changes will happen to that list allWindows = self.windowGroup.sprites() if allWindows: self.groceryList.empty() self.groceryList.addIngredientList(allWindows[0].grabState()) self.state = button.getIdentity() self.stateChanged = True command = None if self.state == "browseAll" or self.state == "deleteRecipe": for scrollableWindow in self.windowGroup.sprites(): if scrollableWindow.getRect().collidepoint(xy): command = scrollableWindow.isClicked(xy) break if not command == None: ## command is a string, meaning there is a change of state # from the button press #print "command recieved!" #print " ", command if isinstance(command, str): self.state = command self.stateChanged = True #print "this thing" else: if len(command) == 2: #print "entered here" #print "command[0]:", command[0] if command[0] == "expand": recipeInfo = self.dbm.loadRecipe(command[1]).toReadableString() scrollableWindow.addTextBlock(command[1], recipeInfo) elif command[0] == "addToGroceryList": self.groceryList.addRecipe(self.dbm.loadRecipe(command[1])) elif command[0] == "removeFromGroceryList": self.groceryList.removeRecipe(self.dbm.loadRecipe(command[1])) elif command[0] == "writeToDesktop": #print "got into this" self.dbm.writeRecipeToDesktop(command[1]) if command[0] == "delete": #print "got here" self.dbm.deleteRecipe(command[1]) elif self.state == "menu" or self.state=="prestart": for sprite in self.buttonGroup.sprites(): if sprite.rect.collidepoint(xy): parseID = sprite.getIdentity().split() # no extra info, just an ID for the new state # if this is not true there is something wrong if len(parseID) == 1: spriteID = parseID[0] if not spriteID == self.state: self.state = spriteID self.stateChanged = True else: raise GUIError("button gives too much data") elif self.state == "addRecipe": for recipeReciever in self.windowGroup.sprites(): recipeData = recipeReciever.isClicked(xy) if not recipeData == None: #for k in recipeData.keys(): #print k, recipeData[k] self.dbm.addRecipeToDataBase(Recipe.fromDictionary(recipeData)) self.state = "menu" self.stateChanged = True elif self.state == "viewGroceryList": for scrollableWindow in self.windowGroup.sprites(): command = scrollableWindow.isClicked(xy) elif self.state == "search": ## search has 2 states, the first is a text box where the user # enters what the search ingredient is # the second the window where the results are displayed # we determine which state by what is in self.windowGroup.sprites() # a TextBox is the first state, a ScrollableWindow is the second for sprite in self.windowGroup.sprites(): pass#command = sprite.isClicked