fixed import failure with leading commas in input strings
This commit is contained in:
@ -232,6 +232,9 @@ class IngredientParser:
|
||||
match = re.search('\((.[^\(])+\)', ingredient)
|
||||
ingredient = ingredient[:match.start()] + ingredient[match.end():] + ' ' + ingredient[match.start():match.end()]
|
||||
|
||||
# leading spaces before commas result in extra tokens, clean them out
|
||||
ingredient = ingredient.replace(' ,', ',')
|
||||
|
||||
tokens = ingredient.split() # split at each space into tokens
|
||||
if len(tokens) == 1:
|
||||
# there only is one argument, that must be the food
|
||||
@ -303,4 +306,7 @@ class IngredientParser:
|
||||
note = food + ' ' + note
|
||||
food = food[:Food._meta.get_field('name').max_length]
|
||||
|
||||
if len(food.strip()) == 0:
|
||||
raise ValueError(f'Error parsing string {ingredient}, food cannot be empty')
|
||||
|
||||
return amount, unit, food, note[:Ingredient._meta.get_field('note').max_length].strip()
|
||||
|
@ -40,10 +40,10 @@ def test_ingredient_parser():
|
||||
"1 small sprig of fresh rosemary": (1, "small", "sprig of fresh rosemary", ""),
|
||||
# does not always work perfectly!
|
||||
"75 g fresh breadcrumbs": (75, "g", "fresh breadcrumbs", ""),
|
||||
"4 acorn squash , or onion squash (600-800g)": (4, "acorn", "squash , or onion squash", "600-800g"),
|
||||
"4 acorn squash , or onion squash (600-800g)": (4, "acorn", "squash, or onion squash", "600-800g"),
|
||||
"1 x 250 g packet of cooked mixed grains , such as spelt and wild rice": (
|
||||
1, "x", "250 g packet of cooked mixed grains", "such as spelt and wild rice"),
|
||||
"1 big bunch of fresh mint , (60g)": (1, "big", "bunch of fresh mint ,", "60g"),
|
||||
"1 big bunch of fresh mint , (60g)": (1, "big", "bunch of fresh mint,", "60g"),
|
||||
"1 large red onion": (1, "large", "red onion", ""),
|
||||
# "2-3 TL Curry": (), # idk what it should use here either
|
||||
"1 Zwiebel gehackt": (1, "Zwiebel", "gehackt", ""),
|
||||
@ -60,6 +60,7 @@ def test_ingredient_parser():
|
||||
"2-3 c Water": (2, "c", "Water", "2-3"),
|
||||
"Pane (raffermo o secco) 80 g": (80, "g", "Pane", "raffermo o secco"),
|
||||
"1 Knoblauchzehe(n), gehackt oder gepresst": (1.0, None, 'Knoblauchzehe(n)', 'gehackt oder gepresst'),
|
||||
"1 Porreestange(n) , ca. 200 g": (1.0, None, 'Porreestange(n)', 'ca. 200 g'), # leading space before comma
|
||||
# test for over long food entries to get properly split into the note field
|
||||
"1 Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l": (
|
||||
1.0, 'Lorem', 'ipsum', 'dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt ut l'),
|
||||
|
@ -637,7 +637,7 @@ def test(request):
|
||||
parser = IngredientParser(request, False)
|
||||
|
||||
data = {
|
||||
'original': '1 LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutl'
|
||||
'original': '1 Porreestange(n) , ca. 200 g'
|
||||
}
|
||||
data['parsed'] = parser.parse(data['original'])
|
||||
|
||||
|
Reference in New Issue
Block a user