Exemplo n.º 1
0
def add_food_handle(args):
    recipe = Recipe(args.recipe)
    food = Food(args.food)
    serving = Serving.from_string(args.serving)
    vargs = vars(args)

    if not recipe.exists():
        print("The recipe '{}' does not exist.".format(args.recipe),
              file=sys.stderr)
        return

    if not food.exists():
        print("The food '{}' does not exist.".format(args.food),
              file=sys.stderr)
        return

    food.load()

    if not food.servings.compatible(serving):
        print("The serving '{}' is not a valid serving for the given food.".
              format(serving),
              file=sys.stderr)
        return

    recipe.load()
    recipe.add_food(food, serving)
    recipe.save()
Exemplo n.º 2
0
def add_food_handle(args):
    date = parse_date(args.date)
    if date is None:
        return

    diary = Diary(date)
    food = Food(args.food)

    if not food.exists():
        print("The food '{}' does not exist.".format(args.food),
              file=sys.stderr)
        return

    food.load()

    serving = Serving.from_string(args.serving)
    if not food.servings.compatible(serving):
        print("The serving '{}' is not a valid serving for the given food.".
              format(args.serving),
              file=sys.stderr)
        return

    if diary.exists():
        diary.load()

    diary.add_food(food, serving)
    diary.save()
Exemplo n.º 3
0
def copy_handle(args):
    food = Food(args.food)
    new_food = Food(args.new_name)

    if not food.exists():
        print("The food '{}' does not exist.".format(args.food),
              file=sys.stderr)
        return

    if new_food.exists():
        print("The food '{}' already exists.".format(args.new_name),
              file=sys.stderr)
        return

    food.load()
    food.name = args.new_name
    food.save()
Exemplo n.º 4
0
def remove_handle(args):
    food = Food(args.food)

    if not food.exists():
        print("The food '{}' does not exist.".format(args.food),
              file=sys.stderr)
    else:
        food.remove()
Exemplo n.º 5
0
def import_handle(args):
    from bolognese.databases.fooddata import Fooddata

    food = Food(args.food)
    if food.exists():
        print("The food '{}' already exists.".format(args.food),
              file=sys.stderr)
        return

    food = Fooddata.get_food(args.food, args.food_id)

    food.save()
Exemplo n.º 6
0
def edit_handle(args):
    food = Food(args.food)
    vargs = vars(args)

    if all(vargs[nutr] is None
           for nutr in Nutrients.NUTRIENTS) and args.servings is None:
        subprocess.call([EDITOR, food.path()])
    else:
        if food.exists():
            food.load()
        food.update(vargs)
        food.save()
Exemplo n.º 7
0
def add_handle(args):
    food = Food(args.food)
    vargs = vars(args)

    if food.exists():
        print("The food '{}' already exists.".format(args.food),
              file=sys.stderr)
        return

    if all(vargs[nutr] is None
           for nutr in Nutrients.NUTRIENTS) and args.servings is None:
        food.save()  # First create the new file
        subprocess.call([EDITOR, food.path()])
    else:
        food.update(vargs)
        food.save()