1
0

Support more obscure danish formats
Some checks failed
Python Ruff Code Quality / ruff (push) Failing after 22s
Run Python tests (through Pytest) / Test (push) Failing after 24s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 21s

This commit is contained in:
Jon Michael Aanes 2025-05-14 21:07:12 +02:00
parent 22aa122388
commit 8baf44b909

View File

@ -16,9 +16,9 @@ RE_PRICE_RAW = r'\b(?:dkk|sek|usd|nok|eur)?\s*([1-9][\d.]*[\d](?:,\d+)?)\s*(?:,-
RE_PRICE = re.compile(RE_PRICE_RAW, flags=re.IGNORECASE)
RE_PRODUCT_PRICE_DK = r'-?\d{1,3}(?:\.?\d{3})*(?:,\d\d\.?)?'
RE_PRODUCT_PRICE_EN = r'-?\d{1,3}(?:,?\d{3})*(?:\.\d\d)?'
RE_PRODUCT_PRICE_AMOUNT = r'(' + RE_PRODUCT_PRICE_DK + '|' + RE_PRODUCT_PRICE_EN + ')'
RE_PRODUCT_PRICE_DK = r'-?[1-9]\d{0,2}(?:\.?\d{3})*(?:,\d\d\.?)?'
RE_PRODUCT_PRICE_EN = r'-?[1-9]\d{0,2}(?:,?\d{3})*(?:\.\d\d)?'
RE_PRODUCT_PRICE_AMOUNT = r'(?P<amount>' + RE_PRODUCT_PRICE_DK + '|' + RE_PRODUCT_PRICE_EN + ')'
def parse_amount(price: str) -> Decimal:
@ -39,12 +39,19 @@ RE_SYM_AMOUNT_CODE = re.compile(
RE_CURRENCY_SYMBOLS
+ r'\s*'
+ RE_PRODUCT_PRICE_AMOUNT
+ r'(?:\s+'
+ r'(?:\s*'
+ RE_CURRENCY_CODES
+ r')?',
flags=re.IGNORECASE,
)
RE_CODE_AMOUNT = re.compile(
RE_CURRENCY_CODES
+ r'\s*'
+ RE_PRODUCT_PRICE_AMOUNT,
flags=re.IGNORECASE,
)
RE_AMOUNT_SYM_CODE = re.compile(
RE_PRODUCT_PRICE_AMOUNT
+ r'\s*'
@ -70,6 +77,11 @@ RE_AMOUNT_KR = re.compile(
flags=re.IGNORECASE,
)
RE_AMOUNT_SUFFIX = re.compile(
'(' + RE_PRODUCT_PRICE_AMOUNT + r')\s*(?:;-|,-|\.-)?',
flags=re.IGNORECASE,
)
def parse_price(text: str, default_currency: Asset) -> AssetAmount | None:
"""
@ -87,13 +99,15 @@ def parse_price(text: str, default_currency: Asset) -> AssetAmount | None:
code, sym, amount_text = None, None, None
if m := RE_SYM_AMOUNT_CODE.fullmatch(text):
code, sym, amount_text = m.group('code'), m.group('sym'), m.group(2)
code, sym, amount_text = m.group('code'), m.group('sym'), m.group('amount')
elif m := RE_CODE_AMOUNT.fullmatch(text):
code, amount_text = m.group('code'), m.group('amount')
elif m := RE_AMOUNT_SYM_CODE.fullmatch(text):
code, sym, amount_text = m.group('code'), m.group('sym'), m.group(1)
code, sym, amount_text = m.group('code'), m.group('sym'), m.group('amount')
elif m := RE_AMOUNT_CODE.fullmatch(text):
code, amount_text = m.group('code'), m.group(1)
elif m := (RE_KR_AMOUNT.fullmatch(text) or RE_AMOUNT_KR.fullmatch(text)):
code, amount_text = 'DKK', m.group(1)
code, amount_text = m.group('code'), m.group('amount')
elif m := (RE_KR_AMOUNT.fullmatch(text) or RE_AMOUNT_KR.fullmatch(text) or RE_AMOUNT_SUFFIX.fullmatch(text)):
code, amount_text = 'DKK', m.group('amount')
else:
return None