|
@@ -91,25 +91,28 @@ class Sum(ioex.calcex.Figure):
|
|
|
unit = property(get_unit, set_unit)
|
|
|
currency = property(get_unit, set_unit)
|
|
|
|
|
|
- value_regex = r"-?\d+([\.,]\d{2})?"
|
|
|
+ value_regex = r"-?\d+([\.,]\d+)?"
|
|
|
currency_regex = r"[^\d\s-]+"
|
|
|
|
|
|
+ sum_regex = [
|
|
|
+ r'^\$?(?P<value>{}) (?P<curr>{})$'.format(
|
|
|
+ value_regex, currency_regex,
|
|
|
+ ),
|
|
|
+ r'^(?P<curr>{}) ?(?P<value>{})$'.format(
|
|
|
+ currency_regex, value_regex,
|
|
|
+ ),
|
|
|
+ ]
|
|
|
+
|
|
|
@staticmethod
|
|
|
def parse_text(text):
|
|
|
- match = re.search(
|
|
|
- r'^\$?(?P<value>{}) (?P<curr>{})$'.format(
|
|
|
- Sum.value_regex, Sum.currency_regex),
|
|
|
+ for pattern in Sum.sum_regex:
|
|
|
+ match = re.search(pattern, text, re.UNICODE)
|
|
|
+ if match:
|
|
|
+ break
|
|
|
+ assert not match is None, '\n{}\ntext: {!r}'.format(
|
|
|
+ '\n'.join(Sum.sum_regex),
|
|
|
text,
|
|
|
- re.UNICODE,
|
|
|
)
|
|
|
- if not match:
|
|
|
- match = re.search(
|
|
|
- r'^(?P<curr>{}) ?(?P<value>{})$'.format(
|
|
|
- Sum.currency_regex, Sum.value_regex),
|
|
|
- text,
|
|
|
- re.UNICODE,
|
|
|
- )
|
|
|
- assert not match is None, text
|
|
|
attr = match.groupdict()
|
|
|
return Sum(
|
|
|
value=locale.atof(attr['value']),
|