Ejemplo n.º 1
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()
Ejemplo n.º 2
0
    def get_food(food_name, food_id):
        r = requests.get(Fooddata.URL.format(food_id),
                         headers=Fooddata.HEADERS)
        page = html.fromstring(r.content)

        food = Food(food_name)
        food.servings.update(['100g'])

        macro_table = page.xpath(
            '//th[text()="Makronæringstoffer m.m."]/ancestor::table/tbody')[0]
        food.nutrients['protein'] = Fooddata.__get_attr(macro_table, 4)
        food.nutrients['carbs'] = Fooddata.__get_attr(macro_table, 7)
        food.nutrients['sugar'] = Fooddata.__get_attr(macro_table, 8)
        food.nutrients['fiber'] = Fooddata.__get_attr(macro_table, 9)
        food.nutrients['fat'] = Fooddata.__get_attr(macro_table, 10)
        food.nutrients['alcohol'] = Fooddata.__get_attr(macro_table, 11)

        mineral_table = page.xpath(
            '//th[text()="Mineraler og uorganisk"]/ancestor::table/tbody')[0]
        food.nutrients['sodium'] = Fooddata.__get_attr(mineral_table, 1)

        fat_table = page.xpath(
            '//th[text()="Fedtsyrer, summer"]/ancestor::table/tbody')
        food.nutrients['saturated_fat'] = Fooddata.__get_attr(
            fat_table[0], 0) if fat_table else 0

        return food
Ejemplo n.º 3
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()
Ejemplo 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()
Ejemplo 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()
Ejemplo 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()
Ejemplo n.º 7
0
def get_totals(food_list, foods={}, recipes={}):
    total = Nutrients()

    for item in food_list.items:
        serving = Serving.from_string(item['serving'] if 'serving' in
                                      item else '1')
        if 'recipe' in item:
            recipe_name = item['recipe']
            if recipe_name not in recipes:
                recipe = recipes[recipe_name] = Recipe(recipe_name)
                if not recipe.exists():
                    raise KeyError(
                        "The recipe '{}' does not exist".format(recipe_name))
                recipe.load()
            else:
                recipe = recipes[recipe_name]
            factor = recipe.servings.get_factor(serving)
            child_total = factor * get_totals(recipe.food_list, foods, recipes)
        if 'food' in item:
            food_name = item['food']
            if food_name not in foods:
                food = foods[food_name] = Food(food_name)
                if not food.exists():
                    raise KeyError(
                        "The food '{}' does not exist".format(food_name))
                food.load()
            else:
                food = foods[food_name]
            factor = food.servings.get_factor(serving)
            child_total = factor * food.nutrients
        total = total + child_total

    return total
Ejemplo n.º 8
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()
Ejemplo n.º 9
0
 def add_recipe(self, recipe, serving, recursive = False):
     if not recursive:
         self.food_list.add_recipe(recipe, serving)
     else:
         factor = recipe.servings.get_factor(serving)
         for item in recipe.food_list.items:
             serving = Serving.from_string(item['serving']) if 'serving' in item else Serving('', 1)
             new_serving = factor * serving
             if 'food' in item:
                 self.food_list.add_food(Food(item['food']), new_serving)
             else:
                 self.food_list.add_recipe(Recipe(item['recipe']), new_serving)
Ejemplo n.º 10
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()
Ejemplo n.º 11
0
def list_handle(args):
    for f in Food.list():
        print(f)