mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
104 lines
4.4 KiB
Python
104 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
#
|
|
# Copyright (c) 2009 CamptoCamp. All rights reserved.
|
|
# @author Nicolas Bessi
|
|
#
|
|
# Abstract class to fetch rates from European Central Bank
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
##############################################################################
|
|
from .currency_getter_interface import Currency_getter_interface
|
|
|
|
from datetime import datetime
|
|
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
|
|
|
|
import logging
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ECB_getter(Currency_getter_interface):
|
|
"""Implementation of Currency_getter_factory interface
|
|
for ECB service
|
|
"""
|
|
|
|
def rate_retrieve(self, dom, ns, curr):
|
|
"""Parse a dom node to retrieve-
|
|
currencies data
|
|
|
|
"""
|
|
res = {}
|
|
xpath_curr_rate = ("/gesmes:Envelope/def:Cube/def:Cube/"
|
|
"def:Cube[@currency='%s']/@rate") % (curr.upper())
|
|
res['rate_currency'] = float(
|
|
dom.xpath(xpath_curr_rate, namespaces=ns)[0]
|
|
)
|
|
return res
|
|
|
|
def get_updated_currency(self, currency_array, main_currency,
|
|
max_delta_days):
|
|
"""implementation of abstract method of Curreny_getter_interface"""
|
|
url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
|
|
# Important : as explained on the ECB web site, the currencies are
|
|
# at the beginning of the afternoon ; so, until 3 p.m. Paris time
|
|
# the currency rates are the ones of trading day N-1
|
|
# http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html
|
|
|
|
# We do not want to update the main currency
|
|
if main_currency in currency_array:
|
|
currency_array.remove(main_currency)
|
|
# Move to new XML lib cf Launchpad bug #645263
|
|
from lxml import etree
|
|
_logger.debug("ECB currency rate service : connecting...")
|
|
rawfile = self.get_url(url)
|
|
dom = etree.fromstring(rawfile)
|
|
_logger.debug("ECB sent a valid XML file")
|
|
ecb_ns = {
|
|
'gesmes': 'http://www.gesmes.org/xml/2002-08-01',
|
|
'def': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
|
|
}
|
|
rate_date = dom.xpath('/gesmes:Envelope/def:Cube/def:Cube/@time',
|
|
namespaces=ecb_ns)[0]
|
|
rate_date_datetime = datetime.strptime(rate_date,
|
|
DEFAULT_SERVER_DATE_FORMAT)
|
|
self.check_rate_date(rate_date_datetime, max_delta_days)
|
|
# We dynamically update supported currencies
|
|
self.supported_currency_array = dom.xpath(
|
|
"/gesmes:Envelope/def:Cube/def:Cube/def:Cube/@currency",
|
|
namespaces=ecb_ns
|
|
)
|
|
self.supported_currency_array.append('EUR')
|
|
_logger.debug("Supported currencies = %s " %
|
|
self.supported_currency_array)
|
|
self.validate_cur(main_currency)
|
|
if main_currency != 'EUR':
|
|
main_curr_data = self.rate_retrieve(dom, ecb_ns, main_currency)
|
|
for curr in currency_array:
|
|
self.validate_cur(curr)
|
|
if curr == 'EUR':
|
|
rate = 1 / main_curr_data['rate_currency']
|
|
else:
|
|
curr_data = self.rate_retrieve(dom, ecb_ns, curr)
|
|
if main_currency == 'EUR':
|
|
rate = curr_data['rate_currency']
|
|
else:
|
|
rate = (curr_data['rate_currency'] /
|
|
main_curr_data['rate_currency'])
|
|
self.updated_currency[curr] = rate
|
|
_logger.debug(
|
|
"Rate retrieved : 1 %s = %s %s" % (main_currency, rate, curr)
|
|
)
|
|
return self.updated_currency, self.log_info
|