fixed import failure with leading commas in input strings

This commit is contained in:
vabene1111
2022-05-18 15:29:03 +02:00
parent 27bb4c9bb8
commit f872f994f1
3 changed files with 12 additions and 5 deletions

View File

@ -232,6 +232,9 @@ class IngredientParser:
match = re.search('\((.[^\(])+\)', ingredient) match = re.search('\((.[^\(])+\)', ingredient)
ingredient = ingredient[:match.start()] + ingredient[match.end():] + ' ' + ingredient[match.start():match.end()] 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 tokens = ingredient.split() # split at each space into tokens
if len(tokens) == 1: if len(tokens) == 1:
# there only is one argument, that must be the food # there only is one argument, that must be the food
@ -303,4 +306,7 @@ class IngredientParser:
note = food + ' ' + note note = food + ' ' + note
food = food[:Food._meta.get_field('name').max_length] 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() return amount, unit, food, note[:Ingredient._meta.get_field('note').max_length].strip()

View File

@ -60,6 +60,7 @@ def test_ingredient_parser():
"2-3 c Water": (2, "c", "Water", "2-3"), "2-3 c Water": (2, "c", "Water", "2-3"),
"Pane (raffermo o secco) 80 g": (80, "g", "Pane", "raffermo o secco"), "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 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 # 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 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'), 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'),

View File

@ -637,7 +637,7 @@ def test(request):
parser = IngredientParser(request, False) parser = IngredientParser(request, False)
data = { data = {
'original': '1 LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutl' 'original': '1 Porreestange(n) , ca. 200 g'
} }
data['parsed'] = parser.parse(data['original']) data['parsed'] = parser.parse(data['original'])