~stefanor/ibid/irc-password-789187

« back to all changes in this revision

Viewing changes to ibid/plugins/conversions.py

  • Committer: Tarmac
  • Author(s): Stefano Rivera
  • Date: 2011-10-23 21:10:52 UTC
  • mfrom: (1031.4.4 exchange-minor-units)
  • Revision ID: ibid-lp-lander@rivera.za.net-20111023211052-2gw4cpg0vy0qygco
Use ISO-4217's minor-units data, to output currency in the correct formatting.
Author: Stefano Rivera
Merge Request: http://code.launchpad.net/~stefanor/ibid/exchange-minor-units/+merge/72312
Approved by: Jonathan Hitchcock, Max Rabkin
Fixes LP: #

Show diffs side-by-side

added added

removed removed

Lines of Context:
316
316
                'http://www.currency-iso.org/dl_iso_table_a1.xml',
317
317
                'conversions/iso4217.xml')
318
318
        document = ElementTree.parse(iso4127_file)
319
 
        # Code -> [Countries..., Currency Name]
 
319
        # Code -> [Countries..., Currency Name, post-decimal digits]
320
320
        self.currencies = {}
321
321
        # Country -> Code
322
322
        self.country_currencies = {}
338
338
            code = currency.findtext('ALPHABETIC_CODE').strip()
339
339
            name = currency.findtext('CURRENCY').strip()
340
340
            place = currency.findtext('ENTITY').strip().title()
 
341
            try:
 
342
                minor_units = int(currency.findtext('MINOR_UNIT').strip())
 
343
            except ValueError:
 
344
                minor_units = 2
341
345
            if code == '' or code in non_currencies:
342
346
                continue
343
347
            # Fund codes
346
350
            if code in self.currencies:
347
351
                self.currencies[code][0].append(place)
348
352
            else:
349
 
                self.currencies[code] = [[place], name]
 
353
                self.currencies[code] = [[place], name, minor_units]
350
354
            if place in no_country_codes:
351
355
                continue
352
356
            if (code[:2] in self.country_codes
411
415
            return "USD"
412
416
        if name == u'pound':
413
417
            return "GBP"
414
 
        for code, (places, currency) in self.currencies.iteritems():
 
418
        for code, (places, currency, units) in self.currencies.iteritems():
415
419
            if name == currency.lower():
416
420
                return code
417
421
            if name.title() in places:
424
428
 
425
429
        # Second pass, not requiring exact match:
426
430
        if rough:
427
 
            for code, (places, currency) in self.currencies.iteritems():
 
431
            for code, (places, currency, units) in self.currencies.iteritems():
428
432
                if name in currency.lower():
429
433
                    return code
430
434
                if any(name in place.lower() for place in places):
473
477
                    u"Whoops, looks like I couldn't make that conversion")
474
478
            return
475
479
 
 
480
        converted = float(amount) * float(last_trade_rate)
476
481
        event.addresponse(
477
482
            u'%(fresult)s %(fcode)s (%(fcurrency)s) = '
478
 
            u'%(tresult)0.2f %(tcode)s (%(tcurrency)s) '
 
483
            u'%(tresult)s %(tcode)s (%(tcurrency)s) '
479
484
            u'(Last trade rate: %(rate)s, Bid: %(bid)s, Ask: %(ask)s)', {
480
485
                'fresult': amount,
481
 
                'tresult': float(amount) * float(last_trade_rate),
 
486
                'tresult': u'%0.*f' % (self.currencies[canonical_to][2],
 
487
                                       converted),
482
488
                'fcurrency': self.currencies[canonical_frm][1],
483
489
                'tcurrency': self.currencies[canonical_to][1],
484
490
                'fcode': canonical_frm,