Merge lp:~oerp.ca/account-financial-tools/add-bank-of-canada-rss into lp:~account-core-editors/account-financial-tools/7.0

Proposed by Daniel Dico (oerp.ca)
Status: Merged
Approved by: Guewen Baconnier @ Camptocamp
Approved revision: 152
Merged at revision: 164
Proposed branch: lp:~oerp.ca/account-financial-tools/add-bank-of-canada-rss
Merge into: lp:~account-core-editors/account-financial-tools/7.0
Diff against target: 124 lines (+76/-8)
1 file modified
currency_rate_update/currency_rate_update.py (+76/-8)
To merge this branch: bzr merge lp:~oerp.ca/account-financial-tools/add-bank-of-canada-rss
Reviewer Review Type Date Requested Status
Stefan Rijnhart (Opener) Approve
Pedro Manuel Baeza Approve
Review via email: mp+204377@code.launchpad.net

Commit message

Add Bank of Canada to curency_rate_update

Description of the change

Add Bank of Canada to currency_rate_update.

Suggest to add the new log "note" at the top of the log in order to allow users to see the most recent note without having to scroll down.

As this module becomes (more) international, I suggest to remove references to ECB in check_rate_date exception and log.

To post a comment you must log in.
Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

Hi, Daniel, thank you very much for the contribution.

What worries me with this new source is that you are using feedparser library. This library is not detected on module install, and it will only fail if you select this source, causing the background process to fail. The alternative is to put this library as dependency, but maybe is to force user to install another library if they are not going to use this source.

What everybody thinks about this?

Regards.

review: Needs Fixing
Revision history for this message
Daniel Dico (oerp.ca) (oerp) wrote :

Hi Pedro,

Thank you for your review.

In regards with feedparser, I was under the impression that this library is required by OpenERP. Please see /openobject-server/openerpcommand/setup.py. I hope I'm not missing something here!?

From what I see, currency_rate_update in order to address the specific needs from a global perspective, will accumulate over time a number of different bank feeds (each with their own specific format) and, most likely, a potential user will only use one of the bank feeds... sure we can add a required library to the dependency... but it could be that users in Europe will not use the Canadian rates, while they will be forced to install the dependency. Maybe a different design could be used? Let's wait for other developers to add feeds from other banks so we can see what formats are required all over the world.

Best regards,
Daniel

> Hi, Daniel, thank you very much for the contribution.
>
> What worries me with this new source is that you are using feedparser library.
> This library is not detected on module install, and it will only fail if you
> select this source, causing the background process to fail. The alternative is
> to put this library as dependency, but maybe is to force user to install
> another library if they are not going to use this source.
>
> What everybody thinks about this?
>
> Regards.

Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

Hi, Daniel, indeed, that library is a dependency of OpenERP itself, so it's not a problem in this version. In the future, this can change, so I think it's better to have declare python dependency on the module to prevent future problems. What do you think?

Regards.

Revision history for this message
Daniel Dico (oerp.ca) (oerp) wrote :

Hi Pedro,

Not sure what's best... if you look at the current code almost every implementation of the "Curreny_getter_interface" has various dependencies, and similar with feedparser they are required by OpenERP, but they are not listed as dependencies.

I've checked where feedparser is used in (7 and trunk) and there is only one place: /openobject-addons/email_template/html2text.py can this really go away? I would't know... probably not!?

It's up to you and the other reviewers... I can add it if you want me to... it's just one line of code.

Regards,
:Daniel

Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

OK, let's stay as is for the moment and see what others think.

Thank you very much for your work and feedback!

Regards

review: Approve
Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Works fine, thanks! I agree with the other changes (reference to a specific service in a generic log msg, and have the most recent log on top).

About the feedparser dependency: while it is a dependency in OpenERP's setup.py, it is not a core library for OpenERP and may drop away in future versions. This may be easily missed during the upgrade of this module at that time. In fact, it is only used to guess encodings of emails in a piece of foreign code (by the late Aaron Schwartz no less). And in this code, a missing feedparser is caught by ImportError with a default to 'utf-8' so you can actually totally get away with running OpenERP without feedparser. But because like what has been said, only a part of the users of this module will check this particular service so you don't want to force an additional dependency on the other users. Fortunately, an ImportError because of feedparser will not crash the OpenERP server in this case, because of this module's strategy when running a service. instead, the error is logged in the update log for the user to see, which is perfectly acceptible.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'currency_rate_update/currency_rate_update.py'
--- currency_rate_update/currency_rate_update.py 2013-12-13 19:33:25 +0000
+++ currency_rate_update/currency_rate_update.py 2014-02-01 08:13:17 +0000
@@ -59,6 +59,9 @@
59 ('Yahoo_getter','Yahoo Finance '),59 ('Yahoo_getter','Yahoo Finance '),
60 ('PL_NBP_getter','Narodowy Bank Polski'), # Added for polish rates60 ('PL_NBP_getter','Narodowy Bank Polski'), # Added for polish rates
61 ('Banxico_getter', 'Banco de México'), # Added for mexican rates61 ('Banxico_getter', 'Banco de México'), # Added for mexican rates
62 # Bank of Canada is using RSS-CB http://www.cbwiki.net/wiki/index.php/Specification_1.1 :
63 # This RSS format is used by other national banks (Thailand, Malaysia, Mexico...)
64 ('CA_BOC_getter','Bank of Canada - noon rates'), # Added for canadian rates
62 ],65 ],
63 "Webservice to use",66 "Webservice to use",
64 required = True67 required = True
@@ -211,13 +214,16 @@
211 vals,214 vals,
212 )215 )
213216
214 note = note + "\n%s currency updated. "\217 # show the most recent note at the top
215 %(datetime.strftime(datetime.today(), '%Y-%m-%d %H:%M:%S'))218 note = "\n%s currency updated. "\
216 note = note + (log_info or '')219 %(datetime.strftime(datetime.today(), '%Y-%m-%d %H:%M:%S'))\
220 + note
221 note = (log_info or '') + note
217 service.write({'note':note})222 service.write({'note':note})
218 except Exception, e:223 except Exception, e:
219 error_msg = note + "\n%s ERROR : %s"\224 error_msg = "\n%s ERROR : %s"\
220 %(datetime.strftime(datetime.today(), '%Y-%m-%d %H:%M:%S'), str(e))225 %(datetime.strftime(datetime.today(), '%Y-%m-%d %H:%M:%S'), str(e))\
226 + note
221 _logger.info(str(e))227 _logger.info(str(e))
222 service.write({'note':error_msg})228 service.write({'note':error_msg})
223229
@@ -263,6 +269,7 @@
263 'Google_getter',269 'Google_getter',
264 'Yahoo_getter',270 'Yahoo_getter',
265 'Banxico_getter',271 'Banxico_getter',
272 'CA_BOC_getter',
266 ]273 ]
267 if class_name in allowed:274 if class_name in allowed:
268 class_def = eval(class_name)275 class_def = eval(class_name)
@@ -340,12 +347,12 @@
340 """Check date constrains. WARN : rate_date must be of datetime type"""347 """Check date constrains. WARN : rate_date must be of datetime type"""
341 days_delta = (datetime.today() - rate_date).days348 days_delta = (datetime.today() - rate_date).days
342 if days_delta > max_delta_days:349 if days_delta > max_delta_days:
343 raise Exception('The rate date from ECB (%s) is %d days away from today, which is over the limit (%d days). Rate not updated in OpenERP.'%(rate_date, days_delta, max_delta_days))350 raise Exception('The rate timestamp (%s) is %d days away from today, which is over the limit (%d days). Rate not updated in OpenERP.'%(rate_date, days_delta, max_delta_days))
344 # We always have a warning when rate_date <> today351 # We always have a warning when rate_date <> today
345 rate_date_str = datetime.strftime(rate_date, '%Y-%m-%d')352 rate_date_str = datetime.strftime(rate_date, '%Y-%m-%d')
346 if rate_date_str != datetime.strftime(datetime.today(), '%Y-%m-%d'):353 if rate_date_str != datetime.strftime(datetime.today(), '%Y-%m-%d'):
347 self.log_info = "WARNING : the rate date from ECB (%s) is not today's date" % rate_date_str354 self.log_info = "WARNING : the rate timestamp (%s) is not today's date" % rate_date_str
348 _logger.warning("the rate date from ECB (%s) is not today's date", rate_date_str)355 _logger.warning("the rate timestamp (%s) is not today's date", rate_date_str)
349356
350357
351#Yahoo ###################################################################################358#Yahoo ###################################################################################
@@ -595,3 +602,64 @@
595602
596 self.updated_currency[curr] = rate603 self.updated_currency[curr] = rate
597 logger.debug("Rate retrieved : " + main_currency + ' = ' + str(rate) + ' ' + curr)604 logger.debug("Rate retrieved : " + main_currency + ' = ' + str(rate) + ' ' + curr)
605
606
607##CA BOC ##### Bank of Canada ############################################################
608class CA_BOC_getter(Curreny_getter_interface) :
609 """Implementation of Curreny_getter_factory interface for Bank of Canada RSS service"""
610
611 def get_updated_currency(self, currency_array, main_currency, max_delta_days):
612 """implementation of abstract method of Curreny_getter_interface"""
613
614 # as of Jan 2014 BOC is publishing noon rates for about 60 currencies
615 url = 'http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_%s.xml'
616 # closing rates are available as well (please note there are only 12
617 # currencies reported):
618 # http://www.bankofcanada.ca/stats/assets/rates_rss/closing/en_%s.xml
619
620 #we do not want to update the main currency
621 if main_currency in currency_array:
622 currency_array.remove(main_currency)
623
624 import feedparser
625 import pytz
626 from dateutil import parser
627
628 for curr in currency_array:
629
630 _logger.debug("BOC currency rate service : connecting...")
631 dom = feedparser.parse(url % curr)
632
633 self.validate_cur(curr)
634
635 # check if BOC service is running
636 if dom.bozo and dom.status <> 404:
637 _logger.error("Bank of Canada - service is down - try again\
638 later...")
639
640 # check if BOC sent a valid response for this currency
641 if dom.status != 200:
642 _logger.error("Exchange data for %s is not reported by Bank\
643 of Canada." % curr)
644 raise osv.except_osv('Error !', 'Exchange data for %s is not\
645 reported by Bank of Canada.' % str(curr))
646
647 _logger.debug("BOC sent a valid RSS file for: " + curr)
648
649 # check for valid exchange data
650 if (dom.entries[0].cb_basecurrency == main_currency) and \
651 (dom.entries[0].cb_targetcurrency == curr):
652 rate = dom.entries[0].cb_exchangerate.split('\n', 1)[0]
653 rate_date_datetime = parser.parse(dom.entries[0].updated)\
654 .astimezone(pytz.utc).replace(tzinfo=None)
655 self.check_rate_date(rate_date_datetime, max_delta_days)
656 self.updated_currency[curr] = rate
657 _logger.debug("BOC Rate retrieved : 1 " + main_currency +
658 ' = ' + str(rate) + ' ' + curr)
659 else:
660 _logger.error("Exchange data format error for Bank of Canada -\
661 %s. Please check provider data format and/or source code." % curr)
662 raise osv.except_osv('Error !', 'Exchange data format error for\
663 Bank of Canada - %s !' % str(curr))
664
665 return self.updated_currency, self.log_info

Subscribers

People subscribed via source and target branches