Example #1
0
def ingredient_from_st(st):
    tokens = tokenize(st)

    ing = Ingredient.get_ingredient(MANAGER, tokens[0])

    if ing is None:
        ing = Ingredient.register_ingredient(MANAGER, tokens[0], tokens[1], tokens[2])
    return ing
def register_ing_prompt(iname):
    """
    Prompts to see if user wants to register a new ingredient
    :param iname: name of ingredient
    :return: new ingredient or None
    """
    userin = input("ingredient '" + iname + "' does not currently exist in the database\n\nWould you like to add it? (Y/N)\n>")
    if(userin.strip() == "Y"):
        ing = Ingredient.register_ingredient(MANAGER, iname, input(
            "What unit is " + iname + " measured in?\n>").lower(), input(
            "Where is " + iname + " stored?\n>").lower())
        return ing
    else:
        return None
def increaseIngredient(tokens, optional=None):
    """
    If ingredient exists and any amount specified in tokens is within acceptable
    range ingredient is added to user's storage.
    If ingredient is not present in DB, user is prompted to define its
    properties and it is added

    :param tokens: processed input line
    :param optional: N/A
    :return: None
    """
    iname = tokens[1].lower()

    amt = 0
    try:
        amt = int(tokens[2])
    except(ValueError):
        try:
            amt = float(tokens[2])
        except:
            print("Failure to increase ingredient: amount not a number")
            return

    if(amt < 0):
        print("Amount of ingredient to be added must be positive. Please use the 'remove' command to decrease ingredients.")
        return

    ing = Ingredient.get_ingredient(MANAGER, iname)

    if ing is None:
        userin = input("ingredient '" + iname + "' does not currently exist in the database\n\nWould you like to add it? (Y/N)\n>")
        if(userin.strip() == "Y"):
            ing = Ingredient.register_ingredient(MANAGER, iname, input(
                "What unit is " + iname + " measured in?\n>").lower(), input(
                "Where is " + iname + " stored?\n>").lower())
        else:
            return

    if(ing in USER.owned_ingredients):
        USER.owned_ingredients[ing] += amt
    else:
        USER.owned_ingredients[ing] = amt
    USER.save_owned_ingredients()