More robust currency guessing
This allows to parse PayPal balances which includes both symbol and letters, for instance "$ 42,00 USD". Also, it does not require adding any new symbols to the regular expression. It might break modules, though numerous cases were tested.
This commit is contained in:
parent
c76cd0c1c5
commit
81e3da8bb0
2 changed files with 27 additions and 4 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
[nosetests]
|
[nosetests]
|
||||||
verbosity = 2
|
verbosity = 2
|
||||||
detailed-errors = 1
|
detailed-errors = 1
|
||||||
|
with-doctest = 1
|
||||||
where = weboob
|
where = weboob
|
||||||
tests = weboob.tools.capabilities.paste,weboob.tools.path
|
tests = weboob.tools.capabilities.paste,weboob.tools.path,weboob.capabilities.bank
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ class TransferError(UserError):
|
||||||
A transfer has failed.
|
A transfer has failed.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Currency:
|
class Currency(object):
|
||||||
CUR_UNKNOWN = 0
|
CUR_UNKNOWN = 0
|
||||||
CUR_EUR = 1
|
CUR_EUR = 1
|
||||||
CUR_CHF = 2
|
CUR_CHF = 2
|
||||||
|
|
@ -54,10 +54,32 @@ class Currency:
|
||||||
u'USD': CUR_USD,
|
u'USD': CUR_USD,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EXTRACTOR = re.compile(r'[\d\s,\.\-]', re.UNICODE)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_currency(klass, text):
|
def get_currency(klass, text):
|
||||||
text = re.sub(u'[^A-Z€]', '', text.upper())
|
u"""
|
||||||
return klass.TXT2CUR.get(text, klass.CUR_UNKNOWN)
|
>>> Currency.get_currency(u'42')
|
||||||
|
0
|
||||||
|
>>> Currency.get_currency(u'42 €')
|
||||||
|
1
|
||||||
|
>>> Currency.get_currency(u'$42')
|
||||||
|
3
|
||||||
|
>>> Currency.get_currency(u'42.000,00€')
|
||||||
|
1
|
||||||
|
>>> Currency.get_currency(u'$42 USD')
|
||||||
|
3
|
||||||
|
>>> Currency.get_currency(u'%42 USD')
|
||||||
|
3
|
||||||
|
>>> Currency.get_currency(u'US1D')
|
||||||
|
0
|
||||||
|
"""
|
||||||
|
curtexts = klass.EXTRACTOR.sub(' ', text.upper()).split()
|
||||||
|
for curtext in curtexts:
|
||||||
|
cur = klass.TXT2CUR.get(curtext)
|
||||||
|
if cur is not None:
|
||||||
|
return cur
|
||||||
|
return klass.CUR_UNKNOWN
|
||||||
|
|
||||||
|
|
||||||
class Recipient(CapBaseObject):
|
class Recipient(CapBaseObject):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue