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)
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()