def write(self, queue): with open(self.queuefile, 'w') as f: for order in queue or []: recipe = Recipe.from_json(order) f.write(str(recipe)) with open(self.jsonqueuefile, 'w') as f: f.write(json.dumps(queue or []))
def recipe_from_json_object(recipe_obj): """Takes a dict decoded from a JSON recipe and returns a Recipe object.""" recipe = Recipe.from_json(recipe_obj) if not recipe.ingredients: if recipe_obj['drink_name'] == "Random Sour": recipe = RandomSourDrink() recipe.user_name = recipe_obj['user_name'] elif recipe_obj['drink_name'] == "Random Boozy": recipe = RandomSpirituousDrink() recipe.user_name = recipe_obj['user_name'] elif recipe_obj['drink_name'] == "Random Bubbly Boozy": recipe = RandomBubblySpirituousDrink() recipe.user_name = recipe_obj['user_name'] elif recipe_obj['drink_name'] == "Random Bubbly Sour": recipe = RandomBubblySourDrink() recipe.user_name = recipe_obj['user_name'] recipe = water_down_recipe(recipe) return recipe
def run(self): last_drink_id = None while True: try: queue = json.loads(self.get('next_drink')) if not self.controller and queue: json_recipe = queue[0] drink_id = json_recipe['id'] if drink_id == last_drink_id: print "Refusing to remake order %s" % last_drink_id time.sleep(20) # Sleep extra long continue # Don't make the same drink twice last_drink_id = drink_id next_recipe = water_down_recipe(Recipe.from_json(json_recipe)) raw_actions = actions_for_recipe(next_recipe) actions = [] for i, action in enumerate(raw_actions): progress = 10 + 90 * i / len(raw_actions) actions.append(UpdateProgressAction(self, drink_id, progress)) actions.append(action) actions.append(FinishDrinkAction(self, drink_id)) self.controller.EnqueueGroup(actions) else: actions = self.controller.InspectQueue() if actions: logging.info("Current action: %s", actions[0]) if (isinstance(actions[0], (WaitForGlassRemoval, WaitForGlassPlaced)) and isinstance(self.controller.robot, FakeRobot)): logging.info("Placing glass") actions[0].force = True self.write(queue) except urllib2.URLError, e: logging.warning("URLError: %s", e) except ValueError, e: print e